@app-swiss-knife/react-native-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +182 -0
- package/SwissKnifePurchases.podspec +40 -0
- package/android/build.gradle +89 -0
- package/android/gradle.properties +3 -0
- package/android/src/main/AndroidManifest.xml +26 -0
- package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingModule.kt +216 -0
- package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingPackage.kt +17 -0
- package/android/src/main/java/com/swissknife/purchases/Conversions.kt +15 -0
- package/android/src/main/java/com/swissknife/purchases/SwissKnifeJson.kt +371 -0
- package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +497 -0
- package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesPackage.kt +25 -0
- package/android/vendor/GENERATED.md +10 -0
- package/android/vendor/java/com/appswissknife/SwissKnife.kt +533 -0
- package/android/vendor/java/com/appswissknife/analytics/EventQueueStore.kt +326 -0
- package/android/vendor/java/com/appswissknife/analytics/FlushEventsWorker.kt +296 -0
- package/android/vendor/java/com/appswissknife/analytics/SKAnalytics.kt +503 -0
- package/android/vendor/java/com/appswissknife/attributes/AttributesClient.kt +188 -0
- package/android/vendor/java/com/appswissknife/attributes/SetAttributesWorker.kt +204 -0
- package/android/vendor/java/com/appswissknife/attribution/Attribution.kt +447 -0
- package/android/vendor/java/com/appswissknife/context/AppLifecycle.kt +120 -0
- package/android/vendor/java/com/appswissknife/context/Clock.kt +70 -0
- package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +138 -0
- package/android/vendor/java/com/appswissknife/context/IDs.kt +55 -0
- package/android/vendor/java/com/appswissknife/context/Reachability.kt +145 -0
- package/android/vendor/java/com/appswissknife/context/Session.kt +192 -0
- package/android/vendor/java/com/appswissknife/crashreporting/SKCrashReporting.kt +945 -0
- package/android/vendor/java/com/appswissknife/crashreporting/SKScreenshot.kt +201 -0
- package/android/vendor/java/com/appswissknife/device/DeviceID.kt +172 -0
- package/android/vendor/java/com/appswissknife/diagnostics/SKLog.kt +97 -0
- package/android/vendor/java/com/appswissknife/entitlements/EntitlementsRepository.kt +495 -0
- package/android/vendor/java/com/appswissknife/identity/Identity.kt +522 -0
- package/android/vendor/java/com/appswissknife/models/CustomerInfo.kt +67 -0
- package/android/vendor/java/com/appswissknife/models/Enums.kt +35 -0
- package/android/vendor/java/com/appswissknife/models/Offerings.kt +80 -0
- package/android/vendor/java/com/appswissknife/models/PurchasesError.kt +89 -0
- package/android/vendor/java/com/appswissknife/models/StoreTransaction.kt +17 -0
- package/android/vendor/java/com/appswissknife/net/BackendHttpClient.kt +507 -0
- package/android/vendor/java/com/appswissknife/net/ExponentialBackoff.kt +69 -0
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +582 -0
- package/android/vendor/java/com/appswissknife/net/OfflineQueueStore.kt +320 -0
- package/android/vendor/java/com/appswissknife/offerings/OfferingsRepository.kt +219 -0
- package/android/vendor/java/com/appswissknife/performance/SKFrameTracker.kt +94 -0
- package/android/vendor/java/com/appswissknife/performance/SKPerformance.kt +428 -0
- package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +560 -0
- package/android/vendor/java/com/appswissknife/purchases/PlayPriceEnrichmentProvider.kt +100 -0
- package/android/vendor/java/com/appswissknife/purchases/PriceEnrichmentProvider.kt +36 -0
- package/android/vendor/java/com/appswissknife/purchases/PurchasesGateway.kt +120 -0
- package/android/vendor/res/xml/swissknife_data_extraction_rules.xml +20 -0
- package/ios/SwissKnifeCrashReporting.m +32 -0
- package/ios/SwissKnifeCrashReporting.swift +186 -0
- package/ios/SwissKnifeJSON.swift +347 -0
- package/ios/SwissKnifePurchases.m +124 -0
- package/ios/SwissKnifePurchases.swift +523 -0
- package/ios/vendor/GENERATED.md +10 -0
- package/ios/vendor/SwissKnifeiOS/Analytics/SKAnalytics.swift +488 -0
- package/ios/vendor/SwissKnifeiOS/AttributesClient.swift +460 -0
- package/ios/vendor/SwissKnifeiOS/Attribution/Attribution.swift +307 -0
- package/ios/vendor/SwissKnifeiOS/Attribution/AttributionNetwork.swift +92 -0
- package/ios/vendor/SwissKnifeiOS/Attribution/AttributionState.swift +141 -0
- package/ios/vendor/SwissKnifeiOS/Context/AppLifecycle.swift +130 -0
- package/ios/vendor/SwissKnifeiOS/Context/Clock.swift +100 -0
- package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +190 -0
- package/ios/vendor/SwissKnifeiOS/Context/IDs.swift +64 -0
- package/ios/vendor/SwissKnifeiOS/Context/Reachability.swift +121 -0
- package/ios/vendor/SwissKnifeiOS/Context/Session.swift +223 -0
- package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +1287 -0
- package/ios/vendor/SwissKnifeiOS/CrashReporting/SKScreenshot.swift +175 -0
- package/ios/vendor/SwissKnifeiOS/DeviceID.swift +161 -0
- package/ios/vendor/SwissKnifeiOS/Diagnostics/SKLog.swift +128 -0
- package/ios/vendor/SwissKnifeiOS/Identity/Identity.swift +323 -0
- package/ios/vendor/SwissKnifeiOS/Identity/IdentityNetwork.swift +70 -0
- package/ios/vendor/SwissKnifeiOS/Net/BackendHttpClient.swift +329 -0
- package/ios/vendor/SwissKnifeiOS/Net/ExponentialBackoff.swift +87 -0
- package/ios/vendor/SwissKnifeiOS/Net/IngestClient.swift +681 -0
- package/ios/vendor/SwissKnifeiOS/Net/OfflineQueueStore.swift +409 -0
- package/ios/vendor/SwissKnifeiOS/Performance/SKFrameTracker.swift +89 -0
- package/ios/vendor/SwissKnifeiOS/Performance/SKPerformance.swift +440 -0
- package/ios/vendor/SwissKnifeiOS/Performance/SKURLSessionDelegate.swift +87 -0
- package/ios/vendor/SwissKnifeiOS/Services/EntitlementsService.swift +646 -0
- package/ios/vendor/SwissKnifeiOS/Services/OfferingsService.swift +458 -0
- package/ios/vendor/SwissKnifeiOS/Services/PurchaseService.swift +374 -0
- package/ios/vendor/SwissKnifeiOS/SwissKnife+Analytics.swift +46 -0
- package/ios/vendor/SwissKnifeiOS/SwissKnife+DeviceID.swift +30 -0
- package/ios/vendor/SwissKnifeiOS/SwissKnife+Session.swift +47 -0
- package/ios/vendor/SwissKnifeiOS/SwissKnife.swift +417 -0
- package/ios/vendor/SwissKnifeiOS/Types.swift +496 -0
- package/lib/commonjs/attribution.js +55 -0
- package/lib/commonjs/attribution.js.map +1 -0
- package/lib/commonjs/identity.js +126 -0
- package/lib/commonjs/identity.js.map +1 -0
- package/lib/commonjs/index.js +118 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/modules/analytics.js +646 -0
- package/lib/commonjs/modules/analytics.js.map +1 -0
- package/lib/commonjs/modules/attachments.js +169 -0
- package/lib/commonjs/modules/attachments.js.map +1 -0
- package/lib/commonjs/modules/crash-reporting.js +1327 -0
- package/lib/commonjs/modules/crash-reporting.js.map +1 -0
- package/lib/commonjs/modules/ids.js +50 -0
- package/lib/commonjs/modules/ids.js.map +1 -0
- package/lib/commonjs/modules/log.js +59 -0
- package/lib/commonjs/modules/log.js.map +1 -0
- package/lib/commonjs/modules/offline-store.js +44 -0
- package/lib/commonjs/modules/offline-store.js.map +1 -0
- package/lib/commonjs/modules/performance.js +494 -0
- package/lib/commonjs/modules/performance.js.map +1 -0
- package/lib/commonjs/native/module.js +41 -0
- package/lib/commonjs/native/module.js.map +1 -0
- package/lib/commonjs/native/spec.js +26 -0
- package/lib/commonjs/native/spec.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/purchases.js +194 -0
- package/lib/commonjs/purchases.js.map +1 -0
- package/lib/commonjs/types.js +236 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/attribution.js +51 -0
- package/lib/module/attribution.js.map +1 -0
- package/lib/module/identity.js +122 -0
- package/lib/module/identity.js.map +1 -0
- package/lib/module/index.js +36 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/modules/analytics.js +642 -0
- package/lib/module/modules/analytics.js.map +1 -0
- package/lib/module/modules/attachments.js +162 -0
- package/lib/module/modules/attachments.js.map +1 -0
- package/lib/module/modules/crash-reporting.js +1320 -0
- package/lib/module/modules/crash-reporting.js.map +1 -0
- package/lib/module/modules/ids.js +44 -0
- package/lib/module/modules/ids.js.map +1 -0
- package/lib/module/modules/log.js +54 -0
- package/lib/module/modules/log.js.map +1 -0
- package/lib/module/modules/offline-store.js +39 -0
- package/lib/module/modules/offline-store.js.map +1 -0
- package/lib/module/modules/performance.js +491 -0
- package/lib/module/modules/performance.js.map +1 -0
- package/lib/module/native/module.js +34 -0
- package/lib/module/native/module.js.map +1 -0
- package/lib/module/native/spec.js +22 -0
- package/lib/module/native/spec.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/purchases.js +187 -0
- package/lib/module/purchases.js.map +1 -0
- package/lib/module/types.js +230 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/commonjs/attribution.d.ts +24 -0
- package/lib/typescript/commonjs/attribution.d.ts.map +1 -0
- package/lib/typescript/commonjs/identity.d.ts +37 -0
- package/lib/typescript/commonjs/identity.d.ts.map +1 -0
- package/lib/typescript/commonjs/index.d.ts +15 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/analytics.d.ts +127 -0
- package/lib/typescript/commonjs/modules/analytics.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/attachments.d.ts +50 -0
- package/lib/typescript/commonjs/modules/attachments.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts +175 -0
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/ids.d.ts +17 -0
- package/lib/typescript/commonjs/modules/ids.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/log.d.ts +12 -0
- package/lib/typescript/commonjs/modules/log.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/offline-store.d.ts +9 -0
- package/lib/typescript/commonjs/modules/offline-store.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/performance.d.ts +75 -0
- package/lib/typescript/commonjs/modules/performance.d.ts.map +1 -0
- package/lib/typescript/commonjs/native/module.d.ts +6 -0
- package/lib/typescript/commonjs/native/module.d.ts.map +1 -0
- package/lib/typescript/commonjs/native/spec.d.ts +119 -0
- package/lib/typescript/commonjs/native/spec.d.ts.map +1 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/purchases.d.ts +68 -0
- package/lib/typescript/commonjs/purchases.d.ts.map +1 -0
- package/lib/typescript/commonjs/types.d.ts +384 -0
- package/lib/typescript/commonjs/types.d.ts.map +1 -0
- package/lib/typescript/module/attribution.d.ts +24 -0
- package/lib/typescript/module/attribution.d.ts.map +1 -0
- package/lib/typescript/module/identity.d.ts +37 -0
- package/lib/typescript/module/identity.d.ts.map +1 -0
- package/lib/typescript/module/index.d.ts +15 -0
- package/lib/typescript/module/index.d.ts.map +1 -0
- package/lib/typescript/module/modules/analytics.d.ts +127 -0
- package/lib/typescript/module/modules/analytics.d.ts.map +1 -0
- package/lib/typescript/module/modules/attachments.d.ts +50 -0
- package/lib/typescript/module/modules/attachments.d.ts.map +1 -0
- package/lib/typescript/module/modules/crash-reporting.d.ts +175 -0
- package/lib/typescript/module/modules/crash-reporting.d.ts.map +1 -0
- package/lib/typescript/module/modules/ids.d.ts +17 -0
- package/lib/typescript/module/modules/ids.d.ts.map +1 -0
- package/lib/typescript/module/modules/log.d.ts +12 -0
- package/lib/typescript/module/modules/log.d.ts.map +1 -0
- package/lib/typescript/module/modules/offline-store.d.ts +9 -0
- package/lib/typescript/module/modules/offline-store.d.ts.map +1 -0
- package/lib/typescript/module/modules/performance.d.ts +75 -0
- package/lib/typescript/module/modules/performance.d.ts.map +1 -0
- package/lib/typescript/module/native/module.d.ts +6 -0
- package/lib/typescript/module/native/module.d.ts.map +1 -0
- package/lib/typescript/module/native/spec.d.ts +119 -0
- package/lib/typescript/module/native/spec.d.ts.map +1 -0
- package/lib/typescript/module/package.json +1 -0
- package/lib/typescript/module/purchases.d.ts +68 -0
- package/lib/typescript/module/purchases.d.ts.map +1 -0
- package/lib/typescript/module/types.d.ts +384 -0
- package/lib/typescript/module/types.d.ts.map +1 -0
- package/package.json +115 -0
- package/react-native.config.js +23 -0
- package/src/attribution.ts +56 -0
- package/src/identity.ts +133 -0
- package/src/index.ts +54 -0
- package/src/modules/analytics.ts +689 -0
- package/src/modules/attachments.ts +222 -0
- package/src/modules/crash-reporting.ts +1577 -0
- package/src/modules/ids.ts +41 -0
- package/src/modules/log.ts +56 -0
- package/src/modules/offline-store.ts +51 -0
- package/src/modules/performance.ts +589 -0
- package/src/native/module.ts +47 -0
- package/src/native/spec.ts +183 -0
- package/src/purchases.ts +214 -0
- package/src/types.ts +470 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// packages/swiss-knife-ios/Sources/SwissKnifeiOS/Context/Session.swift
|
|
2
|
+
//
|
|
3
|
+
// Round-A cross-cutting primitive — Session lifecycle management.
|
|
4
|
+
//
|
|
5
|
+
// Defines the SDK's notion of a "session" — a contiguous slice of foreground
|
|
6
|
+
// time. Before this primitive each module had its own ad-hoc session id:
|
|
7
|
+
// - SKAnalytics used `Int64(Date().timeIntervalSince1970 * 1000)` set at
|
|
8
|
+
// `initialize()` and never rotated.
|
|
9
|
+
// - SKCrashReporting used `UUID().uuidString` rotated only on `start()`.
|
|
10
|
+
// - Performance had no concept of sessions at all.
|
|
11
|
+
// Now every IngestItem carries the same `session_id` + `session_number`
|
|
12
|
+
// stamped by IngestClient at flush time.
|
|
13
|
+
//
|
|
14
|
+
// Restart rules
|
|
15
|
+
// -------------
|
|
16
|
+
// - **First configure() call seeds the initial session.** Achieved
|
|
17
|
+
// externally — the Session.shared.current accessor returns whatever
|
|
18
|
+
// was set last. We initialize lazily in init so `current` always
|
|
19
|
+
// returns a valid snapshot.
|
|
20
|
+
// - **Foreground after >5 min in background → new session.** We track
|
|
21
|
+
// the last-background timestamp and on `willEnterForeground` compare
|
|
22
|
+
// to now; if the gap exceeds `backgroundTimeout` (default 300s), we
|
|
23
|
+
// restart. Mirrors Amplitude / Mixpanel semantics.
|
|
24
|
+
// - **Manual restart.** Hosts that own their own session concept (e.g.
|
|
25
|
+
// after a logout) can call `Session.shared.restart()`.
|
|
26
|
+
//
|
|
27
|
+
// Persistence
|
|
28
|
+
// -----------
|
|
29
|
+
// Only `sessionNumber` is persisted — it's an ordinal counter we want to
|
|
30
|
+
// survive process restarts ("this is the 42nd time the user opened the
|
|
31
|
+
// app"). The id itself is process-local; persisting it would defeat the
|
|
32
|
+
// "new session on cold start" semantic.
|
|
33
|
+
|
|
34
|
+
import Foundation
|
|
35
|
+
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// MARK: - Public types
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
|
|
40
|
+
/// Immutable session snapshot. Stamped onto every IngestItem envelope.
|
|
41
|
+
internal struct SessionSnapshot: Codable, Sendable, Equatable {
|
|
42
|
+
/// UUIDv4 — fresh on every restart.
|
|
43
|
+
let id: String
|
|
44
|
+
/// Wall-clock instant the session began.
|
|
45
|
+
let startedAt: Date
|
|
46
|
+
/// Monotonically increasing ordinal across process launches.
|
|
47
|
+
/// Persisted in UserDefaults.
|
|
48
|
+
let sessionNumber: Int
|
|
49
|
+
|
|
50
|
+
enum CodingKeys: String, CodingKey {
|
|
51
|
+
case id
|
|
52
|
+
case startedAt = "started_at"
|
|
53
|
+
case sessionNumber = "session_number"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/// Event delivered to `Session.addObserver` when the session rotates.
|
|
58
|
+
internal struct SessionChange: Sendable, Equatable {
|
|
59
|
+
let previous: SessionSnapshot
|
|
60
|
+
let current: SessionSnapshot
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
// MARK: - Session
|
|
65
|
+
// ---------------------------------------------------------------------------
|
|
66
|
+
|
|
67
|
+
/// Global session manager. Production callers go through `Session.shared`;
|
|
68
|
+
/// tests can construct a private instance via `init(defaults:)` to avoid
|
|
69
|
+
/// stepping on the shared UserDefaults suite.
|
|
70
|
+
internal final class Session: @unchecked Sendable {
|
|
71
|
+
|
|
72
|
+
/// Shared production instance. Uses `UserDefaults.standard` for
|
|
73
|
+
/// counter persistence and a 5-minute background timeout.
|
|
74
|
+
static let shared: Session = Session(
|
|
75
|
+
defaults: UserDefaults.standard,
|
|
76
|
+
backgroundTimeout: 300
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
/// Background-timeout window. After this much time in the background,
|
|
80
|
+
/// the next foreground triggers a new session.
|
|
81
|
+
let backgroundTimeout: TimeInterval
|
|
82
|
+
|
|
83
|
+
private let queue = DispatchQueue(label: "com.swissknife.context.session")
|
|
84
|
+
private let defaults: UserDefaults
|
|
85
|
+
private var snapshot: SessionSnapshot
|
|
86
|
+
private var observers: [UUID: (SessionChange) -> Void] = [:]
|
|
87
|
+
private var backgroundedAt: Date?
|
|
88
|
+
private var lifecycleToken: UUID?
|
|
89
|
+
|
|
90
|
+
/// Construct a Session with explicit dependencies — tests use this to
|
|
91
|
+
/// hand in an isolated UserDefaults and a tight timeout.
|
|
92
|
+
init(
|
|
93
|
+
defaults: UserDefaults,
|
|
94
|
+
backgroundTimeout: TimeInterval
|
|
95
|
+
) {
|
|
96
|
+
self.defaults = defaults
|
|
97
|
+
self.backgroundTimeout = backgroundTimeout
|
|
98
|
+
// Bootstrap eagerly — observers and ingest call sites can read
|
|
99
|
+
// `current` from the moment the instance exists, no nil-handling.
|
|
100
|
+
let initialNumber = defaults.integer(forKey: Self.counterKey) + 1
|
|
101
|
+
defaults.set(initialNumber, forKey: Self.counterKey)
|
|
102
|
+
self.snapshot = SessionSnapshot(
|
|
103
|
+
id: IDs.sessionId(),
|
|
104
|
+
startedAt: Date(),
|
|
105
|
+
sessionNumber: initialNumber
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// MARK: - Public surface
|
|
110
|
+
|
|
111
|
+
/// Current session snapshot.
|
|
112
|
+
var current: SessionSnapshot {
|
|
113
|
+
queue.sync { snapshot }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/// Force a new session right now. Used by host code on explicit
|
|
117
|
+
/// logout, or internally after the background-timeout heuristic
|
|
118
|
+
/// fires.
|
|
119
|
+
func restart() {
|
|
120
|
+
rotate()
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/// Subscribe to session changes. Returns a token; pass to
|
|
124
|
+
/// `removeObserver` to unsubscribe.
|
|
125
|
+
@discardableResult
|
|
126
|
+
func addObserver(_ block: @escaping (SessionChange) -> Void) -> UUID {
|
|
127
|
+
let token = UUID()
|
|
128
|
+
queue.sync { observers[token] = block }
|
|
129
|
+
return token
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
func removeObserver(_ token: UUID) {
|
|
133
|
+
queue.sync { _ = observers.removeValue(forKey: token) }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// MARK: - Lifecycle wiring
|
|
137
|
+
|
|
138
|
+
/// Hook into `AppLifecycle` events so background-timeout-based
|
|
139
|
+
/// restarts happen automatically. Called once from
|
|
140
|
+
/// `SwissKnife.configure(...)`. Idempotent — calling twice replaces
|
|
141
|
+
/// the previous subscription so we don't double-fire.
|
|
142
|
+
@MainActor
|
|
143
|
+
func startObservingLifecycle() {
|
|
144
|
+
if let token = lifecycleToken {
|
|
145
|
+
AppLifecycle.unsubscribe(token)
|
|
146
|
+
}
|
|
147
|
+
lifecycleToken = AppLifecycle.subscribe { [weak self] event in
|
|
148
|
+
self?.handle(event: event)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private func handle(event: AppLifecycle.Event) {
|
|
153
|
+
switch event {
|
|
154
|
+
case .didEnterBackground:
|
|
155
|
+
queue.sync { backgroundedAt = Date() }
|
|
156
|
+
case .willEnterForeground:
|
|
157
|
+
let shouldRotate: Bool = queue.sync {
|
|
158
|
+
guard let bgAt = backgroundedAt else { return false }
|
|
159
|
+
backgroundedAt = nil
|
|
160
|
+
return Date().timeIntervalSince(bgAt) >= backgroundTimeout
|
|
161
|
+
}
|
|
162
|
+
if shouldRotate { rotate() }
|
|
163
|
+
case .didBecomeActive, .willResignActive:
|
|
164
|
+
// No-op — we drive the restart decision off background/foreground
|
|
165
|
+
// boundaries, not active state. Otherwise transient interruptions
|
|
166
|
+
// (incoming call, control-center pull) would rotate sessions.
|
|
167
|
+
break
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// MARK: - Internal
|
|
172
|
+
|
|
173
|
+
private func rotate() {
|
|
174
|
+
let previous = current
|
|
175
|
+
let nextNumber: Int = queue.sync {
|
|
176
|
+
let n = defaults.integer(forKey: Self.counterKey) + 1
|
|
177
|
+
defaults.set(n, forKey: Self.counterKey)
|
|
178
|
+
return n
|
|
179
|
+
}
|
|
180
|
+
let next = SessionSnapshot(
|
|
181
|
+
id: IDs.sessionId(),
|
|
182
|
+
startedAt: Date(),
|
|
183
|
+
sessionNumber: nextNumber
|
|
184
|
+
)
|
|
185
|
+
let observerSnapshot: [(SessionChange) -> Void] = queue.sync {
|
|
186
|
+
self.snapshot = next
|
|
187
|
+
return self.observers.values.map { $0 }
|
|
188
|
+
}
|
|
189
|
+
let change = SessionChange(previous: previous, current: next)
|
|
190
|
+
for observer in observerSnapshot { observer(change) }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// MARK: - Test seams
|
|
194
|
+
|
|
195
|
+
/// Drop the lifecycle subscription and reset internal observer state.
|
|
196
|
+
/// Production MUST NOT call.
|
|
197
|
+
@MainActor
|
|
198
|
+
internal func _resetForTesting() {
|
|
199
|
+
if let token = lifecycleToken {
|
|
200
|
+
AppLifecycle.unsubscribe(token)
|
|
201
|
+
}
|
|
202
|
+
queue.sync {
|
|
203
|
+
observers.removeAll()
|
|
204
|
+
backgroundedAt = nil
|
|
205
|
+
lifecycleToken = nil
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/// Inject a backgrounded timestamp so tests can simulate the timeout
|
|
210
|
+
/// path without sleeping.
|
|
211
|
+
internal func _setBackgroundedAtForTesting(_ date: Date?) {
|
|
212
|
+
queue.sync { backgroundedAt = date }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/// Drive a synthetic lifecycle event for tests.
|
|
216
|
+
internal func _handleEventForTesting(_ event: AppLifecycle.Event) {
|
|
217
|
+
handle(event: event)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// MARK: - Storage keys
|
|
221
|
+
|
|
222
|
+
private static let counterKey = "com.swissknife.context.session.counter"
|
|
223
|
+
}
|