Skip to content

Commit

Permalink
refactor(ios): replace AnalyticsService with swift-utilities Analytic…
Browse files Browse the repository at this point in the history
…sService
  • Loading branch information
autoreleasefool committed May 20, 2024
1 parent 10b37f7 commit 3eca943
Show file tree
Hide file tree
Showing 27 changed files with 216 additions and 247 deletions.
8 changes: 4 additions & 4 deletions ios/Approach/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,14 @@ let package = Package(
.target(
name: "AppFeature",
dependencies: [
.product(name: "AnalyticsPackageService", package: "swift-utilities"),
.product(name: "AppInfoPackageService", package: "swift-utilities"),
.product(name: "BundlePackageService", package: "swift-utilities"),
.product(name: "FileManagerPackageService", package: "swift-utilities"),
.product(name: "PasteboardPackageService", package: "swift-utilities"),
.product(name: "SentryErrorReportingPackageService", package: "swift-utilities"),
.product(name: "StoreReviewPackageService", package: "swift-utilities"),
.product(name: "TelemetryDeckAnalyticsPackageService", package: "swift-utilities"),
.product(name: "UserDefaultsPackageService", package: "swift-utilities"),
"AccessoriesOverviewFeature",
"BowlersListFeature",
Expand Down Expand Up @@ -1129,19 +1131,17 @@ let package = Package(
.target(
name: "AnalyticsService",
dependencies: [
.product(name: "BundlePackageServiceInterface", package: "swift-utilities"),
.product(name: "Sentry", package: "sentry-cocoa"),
.product(name: "TelemetryClient", package: "SwiftClient"),
"AnalyticsServiceInterface",
"ConstantsLibrary",
"PreferenceServiceInterface",
]
),
.target(
name: "AnalyticsServiceInterface",
dependencies: [
.product(name: "AnalyticsPackageServiceInterface", package: "swift-utilities"),
.product(name: "ComposableArchitecture", package: "swift-composable-architecture"),
.product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "DependenciesMacros", package: "swift-dependencies"),
.product(name: "ErrorReportingClientPackageLibrary", package: "swift-utilities"),
]
),
Expand Down
17 changes: 12 additions & 5 deletions ios/Approach/Package.swift.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ supported = [ "\"17.0\"" ]
[features.App]
features = [ "AccessoriesOverview", "BowlersList", "Onboarding", "Settings", "StatisticsOverview" ]
services = [ "Launch" ]
dependencies = [ "AppInfoPackageService", "BundlePackageService", "SentryErrorReportingPackageService", "FileManagerPackageService", "PasteboardPackageService", "StoreReviewPackageService", "UserDefaultsPackageService" ]
dependencies = [ "AnalyticsPackageService", "AppInfoPackageService", "BundlePackageService", "FileManagerPackageService", "PasteboardPackageService", "SentryErrorReportingPackageService", "StoreReviewPackageService", "TelemetryDeckAnalyticsPackageService", "UserDefaultsPackageService" ]

[features.AccessoriesOverview]
features = [ "AlleysList", "GearList" ]
Expand Down Expand Up @@ -226,11 +226,9 @@ libraries = [ "Models" ]
dependencies = [ "EquatablePackageLibrary", "XCTestDynamicOverlay" ]

[services.Analytics]
services = [ "Preference" ]
libraries = [ "Constants" ]
dependencies = [ "BundlePackageServiceInterface", "Sentry", "TelemetryClient" ]
dependencies = [ "Sentry" ]
[services.Analytics.interface]
dependencies = [ "ComposableArchitecture", "ErrorReportingClientPackageLibrary" ]
dependencies = [ "AnalyticsPackageServiceInterface", "ComposableArchitecture", "DependenciesMacros", "ErrorReportingClientPackageLibrary" ]

[services.Announcements]
dependencies = [ "UserDefaultsPackageServiceInterface" ]
Expand Down Expand Up @@ -511,6 +509,12 @@ from = "1.1.0"
url = "https://github.com/weichsel/ZIPFoundation.git"
from = "0.9.18"

[dependencies.AnalyticsPackageService]
sharedRef = "SwiftUtilities"

[dependencies.AnalyticsPackageServiceInterface]
sharedRef = "SwiftUtilities"

[dependencies.AppInfoPackageService]
sharedRef = "SwiftUtilities"

Expand Down Expand Up @@ -556,6 +560,9 @@ sharedRef = "SwiftUtilities"
[dependencies.SwiftUIExtensionsPackageLibrary]
sharedRef = "SwiftUtilities"

[dependencies.TelemetryDeckAnalyticsPackageService]
sharedRef = "SwiftUtilities"

[dependencies.TestUtilitiesPackageLibrary]
sharedRef = "SwiftUtilities"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public struct AccessoriesOverview: Reducer {
public init() {}

@Dependency(AlleysRepository.self) var alleys
@Dependency(AnalyticsService.self) var analytics
@Dependency(\.analytics) var analytics
@Dependency(GearRepository.self) var gear
@Dependency(\.uuid) var uuid

Expand Down
102 changes: 0 additions & 102 deletions ios/Approach/Sources/AnalyticsService/AnalyticsService+Live.swift

This file was deleted.

15 changes: 15 additions & 0 deletions ios/Approach/Sources/AnalyticsService/BreadcrumbService+Live.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import AnalyticsServiceInterface
import Dependencies
import Sentry

extension BreadcrumbService: DependencyKey {
public static var liveValue: Self {
Self(
drop: { breadcrumb in
let crumb = Sentry.Breadcrumb(level: .info, category: breadcrumb.category.rawValue)
crumb.message = breadcrumb.message
SentrySDK.addBreadcrumb(crumb)
}
)
}
}
9 changes: 9 additions & 0 deletions ios/Approach/Sources/AnalyticsService/Crash+Live.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import AnalyticsServiceInterface
import Dependencies
import Sentry

extension CrashGenerator: DependencyKey {
public static var liveValue: Self {
return Self { SentrySDK.crash() }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import AnalyticsPackageServiceInterface
import AnalyticsServiceInterface
import Dependencies
import Foundation

extension GameAnalyticsService: DependencyKey {
public static var liveValue: Self {
let sessionID = ActorIsolated<UUID?>(nil)
let sessions = ActorIsolated<[UUID: Set<UUID>]>([:])

return Self(
trackEvent: { event in
if let session = await sessionID.value {
let inserted = await sessions.withValue {
$0[session, default: []].insert(event.eventId).inserted
}

if !inserted {
return
}
}

@Dependency(\.analytics) var analytics
let basicEvent = BasicEvent(name: event.name, payload: event.payload)
try? await analytics.trackEvent(basicEvent)
},
resetGameSessionID: {
@Dependency(\.uuid) var uuid
await sessionID.setValue(uuid())
}
)
}
}

private struct BasicEvent: TrackableEvent {
let name: String
let payload: [String: String]?
}
15 changes: 2 additions & 13 deletions ios/Approach/Sources/AnalyticsServiceInterface/Analytics.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
@_exported import AnalyticsPackageServiceInterface
import Foundation

public enum Analytics {}

extension Analytics {
public enum OptInStatus: String {
case optedIn
case optedOut
}
}

public protocol TrackableEvent {
public protocol GameSessionTrackableEvent {
var name: String { get }
var payload: [String: String]? { get }
}

public protocol GameSessionTrackableEvent: TrackableEvent {
var eventId: UUID { get }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,30 @@ public struct AnalyticsReducer<State, Action>: Reducer {
self.reducer = reducer
}

@Dependency(AnalyticsService.self) var analytics
@Dependency(\.analytics) var analytics

public var body: some Reducer<State, Action> {
Reduce { state, action in
guard let event = reducer(state, action) else { return .none }
return .run { _ in await analytics.trackEvent(event) }
return .run { _ in try? await analytics.trackEvent(event) }
}
}
}

@Reducer
public struct GameAnalyticsReducer<State, Action>: Reducer {
let reducer: (State, Action) -> GameSessionTrackableEvent?

public init(reducer: @escaping (_ state: State, _ action: Action) -> GameSessionTrackableEvent?) {
self.reducer = reducer
}

@Dependency(\.gameAnalytics) var gameAnalytics

public var body: some Reducer<State, Action> {
Reduce { state, action in
guard let event = reducer(state, action) else { return .none }
return .run { _ in await gameAnalytics.trackEvent(event) }
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AnalyticsPackageServiceInterface
import ComposableArchitecture

@Reducer
Expand All @@ -8,12 +9,12 @@ public struct BreadcrumbReducer<State, Action>: Reducer {
self.reducer = reducer
}

@Dependency(AnalyticsService.self) var analytics
@Dependency(\.breadcrumbs) var breadcrumbs

public var body: some Reducer<State, Action> {
Reduce { state, action in
guard let breadcrumb = reducer(state, action) else { return .none }
return .run { _ in await analytics.breadcrumb(breadcrumb) }
return .run { _ in await breadcrumbs.drop(breadcrumb) }
}
}
}
Loading

0 comments on commit 3eca943

Please sign in to comment.