-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathAdapty+Activate.swift
More file actions
107 lines (88 loc) · 4.28 KB
/
Adapty+Activate.swift
File metadata and controls
107 lines (88 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// Adapty+Activate.swift
// AdaptySDK
//
// Created by Aleksei Valiano on 22.09.2024
//
import Foundation
private let log = Log.default
public extension Adapty {
/// Use this method to initialize the Adapty SDK.
///
/// Call this method in the `application(_:didFinishLaunchingWithOptions:)`.
///
/// - Parameter apiKey: You can find it in your app settings in [Adapty Dashboard](https://app.adapty.io/) *App settings* > *General*.
/// - Parameter observerMode: A boolean value controlling [Observer mode](https://docs.adapty.io/v2.0.0/docs/observer-vs-full-mode). Turn it on if you handle purchases and subscription status yourself and use Adapty for sending subscription events and analytics
/// - Parameter customerUserId: User identifier in your system
nonisolated static func activate(
_ apiKey: String,
observerMode: Bool = false,
customerUserId: String? = nil
) async throws(AdaptyError) {
try await activate(
with: AdaptyConfiguration
.builder(withAPIKey: apiKey)
.with(customerUserId: customerUserId)
.with(observerMode: observerMode)
.build()
)
}
/// Use this method to initialize the Adapty SDK.
///
/// Call this method in the `application(_:didFinishLaunchingWithOptions:)`.
///
/// - Parameter configuration: `AdaptyConfiguration` which allows to configure Adapty SDK
static func activate(
with configuration: AdaptyConfiguration
) async throws(AdaptyError) {
let stamp = Log.stamp
#if ADAPTY_KIDS_MODE
let kidsModeEnabled = true
#else
let kidsModeEnabled = false
#endif
let logParams: EventParameters? = [
"observer_mode": configuration.observerMode,
"customer_user_id": configuration.customerUserId,
"app_account_token": configuration.appAccountToken?.uuidString,
"idfa_collection_disabled": configuration.idfaCollectionDisabled,
"ip_address_collection_disabled": configuration.ipAddressCollectionDisabled,
"clear_data_on_backup": configuration.clearDataOnBackup,
"kids_mode_enabled": kidsModeEnabled,
]
trackSystemEvent(AdaptySDKMethodRequestParameters(methodName: .activate, stamp: stamp, params: logParams))
log.verbose("Calling Adapty activate [\(stamp)] with params: \(logParams?.description ?? "nil")")
guard !isActivated else {
let error = AdaptyError.activateOnceError()
trackSystemEvent(AdaptySDKMethodResponseParameters(methodName: .activate, stamp: stamp, error: error.localizedDescription))
log.error("Adapty activate [\(stamp)] encountered an error: \(error).")
throw error
}
let task = Task<Adapty, Never>.detached { @AdaptyActor @Sendable () async -> Adapty in
if let logLevel = configuration.logLevel { Adapty.logLevel = logLevel }
await Storage.clearAllDataIf(
differentApiKey: configuration.apiKey,
onRestoreFromBackup: configuration.clearDataOnBackup
)
AdaptyConfiguration.transactionFinishBehavior = configuration.transactionFinishBehavior
AdaptyConfiguration.callbackDispatchQueue = configuration.callbackDispatchQueue // TODO: Refactoring
AdaptyConfiguration.idfaCollectionDisabled = configuration.idfaCollectionDisabled // TODO: Refactoring
AdaptyConfiguration.ipAddressCollectionDisabled = configuration.ipAddressCollectionDisabled // TODO: Refactoring
let environment = await Environment.instance
let backend = await Backend(with: configuration, environment: environment)
await eventsManager.set(backend: backend)
let sdk = await Adapty(
configuration: configuration,
backend: backend
)
trackSystemEvent(AdaptySDKMethodResponseParameters(methodName: .activate, stamp: stamp))
log.info("Adapty activated successfully. [\(stamp)]")
set(shared: sdk)
UserAcquisitionManager.activate(sdk)
LifecycleManager.shared.initialize()
return sdk
}
set(activatingSDK: task)
_ = await task.value
}
}