@altcraft/react-native-sdk 0.0.1 → 0.0.3
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/LICENSE +20 -20
- package/README.md +5 -2
- package/android/build.gradle +2 -3
- package/android/src/main/java/com/altcraft/sdk/SdkModule.kt +493 -896
- package/android/src/main/java/com/altcraft/sdk/utilities/Converter.kt +326 -0
- package/android/src/main/java/com/altcraft/sdk/utilities/Extension.kt +72 -0
- package/ios/Converter.swift +640 -0
- package/ios/Sdk.h +4 -3
- package/ios/Sdk.mm +124 -370
- package/ios/SdkModule.swift +436 -653
- package/lib/module/NativeSdk.js +35 -0
- package/lib/module/NativeSdk.js.map +1 -1
- package/lib/module/Utilities.js +94 -0
- package/lib/module/Utilities.js.map +1 -0
- package/lib/module/index.js +180 -419
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/NativeSdk.d.ts +117 -32
- package/lib/typescript/src/NativeSdk.d.ts.map +1 -1
- package/lib/typescript/src/Utilities.d.ts +15 -0
- package/lib/typescript/src/Utilities.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +113 -308
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/NativeSdk.ts +156 -46
- package/src/Utilities.ts +116 -0
- package/src/index.tsx +255 -483
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnFCMProvider.kt +0 -27
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnHMSProvider.kt +0 -32
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnJWTProvider.kt +0 -21
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnRustoreProvider.kt +0 -53
- package/ios/protocols/RNAPNSProvider.swift +0 -15
- package/ios/protocols/RNFCMProvider.swift +0 -24
- package/ios/protocols/RNHMSProvider.swift +0 -24
- package/ios/protocols/RNJWTProvider.swift +0 -15
package/ios/SdkModule.swift
CHANGED
|
@@ -1,235 +1,117 @@
|
|
|
1
|
+
//
|
|
2
|
+
// SdkModule.swift
|
|
3
|
+
// AltcraftRnBridge
|
|
4
|
+
//
|
|
5
|
+
// Created by Andrey Pogodin.
|
|
6
|
+
//
|
|
7
|
+
// Copyright © 2026 Altcraft. All rights reserved.
|
|
8
|
+
|
|
9
|
+
|
|
1
10
|
import Foundation
|
|
2
11
|
import Altcraft
|
|
3
12
|
import React
|
|
4
13
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
// MARK: - Constants
|
|
15
|
+
|
|
16
|
+
private enum RnConstants {
|
|
17
|
+
|
|
18
|
+
// Module / events
|
|
19
|
+
static let jsEventName: String = "AltcraftSdkEvent"
|
|
20
|
+
|
|
21
|
+
// Promise reject codes
|
|
22
|
+
static let errInitInvalidConfig: String = "ALTCRAFT_INIT_INVALID_CONFIG"
|
|
23
|
+
static let errInitError: String = "ALTCRAFT_INIT_ERROR"
|
|
24
|
+
|
|
25
|
+
// Promise reject messages
|
|
26
|
+
static let msgApiUrlRequired: String = "apiUrl is required"
|
|
27
|
+
static let msgInvalidConfiguration: String = "Invalid Altcraft configuration"
|
|
28
|
+
static let msgInitializationFailed: String = "Altcraft initialization failed"
|
|
29
|
+
|
|
30
|
+
// Config keys
|
|
31
|
+
static let keyApiUrl: String = "apiUrl"
|
|
32
|
+
static let keyRToken: String = "rToken"
|
|
33
|
+
static let keyEnableLogging: String = "enableLogging"
|
|
34
|
+
static let keyProviderPriorityList: String = "providerPriorityList"
|
|
35
|
+
static let keyAppInfo: String = "appInfo"
|
|
36
|
+
|
|
37
|
+
// TokenData keys
|
|
38
|
+
static let keyProvider: String = "provider"
|
|
39
|
+
static let keyToken: String = "token"
|
|
40
|
+
|
|
41
|
+
// SDK event payload keys
|
|
42
|
+
static let keyFunction: String = "function"
|
|
43
|
+
static let keyCode: String = "code"
|
|
44
|
+
static let keyMessage: String = "message"
|
|
45
|
+
static let keyType: String = "type"
|
|
46
|
+
static let keyValue: String = "value"
|
|
47
|
+
|
|
48
|
+
// Event types for JS
|
|
49
|
+
static let typeEvent: String = "event"
|
|
50
|
+
static let typeError: String = "error"
|
|
51
|
+
static let typeRetryError: String = "retryError"
|
|
52
|
+
|
|
53
|
+
// ResponseWithHttp mapping keys (outgoing)
|
|
54
|
+
static let keyHttpCode: String = "httpCode"
|
|
55
|
+
static let keyResponse: String = "response"
|
|
56
|
+
static let keyError: String = "error"
|
|
57
|
+
static let keyErrorText: String = "errorText"
|
|
58
|
+
static let keyProfile: String = "profile"
|
|
59
|
+
static let keyId: String = "id"
|
|
60
|
+
static let keyStatus: String = "status"
|
|
61
|
+
static let keyIsTest: String = "isTest"
|
|
62
|
+
static let keySubscription: String = "subscription"
|
|
63
|
+
static let keySubscriptionId: String = "subscriptionId"
|
|
64
|
+
static let keyHashId: String = "hashId"
|
|
65
|
+
static let keyFields: String = "fields"
|
|
66
|
+
static let keyCats: String = "cats"
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// MARK: - Module
|
|
70
|
+
|
|
20
71
|
@objc(SdkModule)
|
|
21
72
|
@objcMembers
|
|
22
73
|
@available(iOSApplicationExtension, unavailable)
|
|
23
74
|
public final class SdkModule: NSObject {
|
|
24
75
|
|
|
25
|
-
|
|
76
|
+
// MARK: - Singleton & State
|
|
77
|
+
|
|
78
|
+
/// Shared singleton instance used by the React Native bridge.
|
|
26
79
|
public static let shared = SdkModule()
|
|
27
80
|
|
|
28
|
-
/// A single lock protecting all mutable state in this module.
|
|
29
81
|
private let lock = NSLock()
|
|
30
|
-
|
|
31
|
-
// MARK: - JWT (stable provider reference)
|
|
32
|
-
|
|
33
|
-
/// Current JWT value used by `jwtProvider`.
|
|
34
|
-
///
|
|
35
|
-
/// Access must be protected by `lock`.
|
|
36
|
-
private var jwt: String?
|
|
37
|
-
|
|
38
|
-
/// Stable JWT provider reference.
|
|
39
|
-
///
|
|
40
|
-
/// The SDK keeps a strong reference to the provider, therefore this object must live
|
|
41
|
-
/// for the entire process lifetime. The provider reads `jwt` under `lock`.
|
|
42
|
-
private lazy var jwtProvider: RNJWTProvider = RNJWTProvider { [weak self] in
|
|
43
|
-
guard let self else { return nil }
|
|
44
|
-
self.lock.lock(); defer { self.lock.unlock() }
|
|
45
|
-
return self.jwt
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/// Indicates whether the JWT provider has been registered in the SDK.
|
|
49
|
-
///
|
|
50
|
-
/// Access must be protected by `lock`.
|
|
51
|
-
private var jwtProviderInstalled = false
|
|
52
|
-
|
|
53
|
-
/// Registers `jwtProvider` in the SDK exactly once.
|
|
54
|
-
///
|
|
55
|
-
/// Uses a double-checked locking pattern:
|
|
56
|
-
/// - fast path avoids locking when already installed
|
|
57
|
-
/// - slow path installs provider and flips the flag under lock
|
|
58
|
-
private func installJWTProviderIfNeeded() {
|
|
59
|
-
// double-checked locking pattern (cheap fast-path)
|
|
60
|
-
lock.lock()
|
|
61
|
-
let already = jwtProviderInstalled
|
|
62
|
-
lock.unlock()
|
|
63
|
-
if already { return }
|
|
64
|
-
|
|
65
|
-
AltcraftSDK.shared.setJWTProvider(provider: jwtProvider)
|
|
66
|
-
|
|
67
|
-
lock.lock()
|
|
68
|
-
jwtProviderInstalled = true
|
|
69
|
-
lock.unlock()
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/// Sets JWT token for requests performed by the Altcraft SDK.
|
|
73
|
-
///
|
|
74
|
-
/// ObjC selector used by `Sdk.mm`: `setJWT:`
|
|
75
|
-
///
|
|
76
|
-
/// Behavior:
|
|
77
|
-
/// - Ensures the stable provider is installed once.
|
|
78
|
-
/// - Updates the stored JWT value (provider will start returning the new token).
|
|
79
|
-
@objc(setJWT:)
|
|
80
|
-
public func setJWT(_ token: String?) {
|
|
81
|
-
installJWTProviderIfNeeded()
|
|
82
|
-
lock.lock()
|
|
83
|
-
jwt = token
|
|
84
|
-
lock.unlock()
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// MARK: - AppGroup
|
|
88
|
-
|
|
89
|
-
/// Sets the App Group identifier for shared storage (Core Data + UserDefaults/App Group).
|
|
90
|
-
///
|
|
91
|
-
/// ObjC selector used by `Sdk.mm`: `setAppGroupWithName:`
|
|
92
|
-
///
|
|
93
|
-
/// Call this before operations that rely on a shared container.
|
|
94
|
-
@objc(setAppGroupWithName:)
|
|
95
|
-
public func setAppGroup(name: String?) {
|
|
96
|
-
AltcraftSDK.shared.setAppGroup(groupName: name)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// MARK: - Tokens / Config
|
|
100
|
-
|
|
101
|
-
/// Stored push tokens used by token providers. Access guarded by `lock`.
|
|
102
|
-
private var fcm: String?
|
|
103
|
-
private var hms: String?
|
|
104
|
-
private var apns: String?
|
|
105
|
-
|
|
106
|
-
/// Last built configuration used to initialize the SDK.
|
|
107
|
-
/// Stored for debugging/inspection purposes.
|
|
108
82
|
private var configuration: AltcraftConfiguration?
|
|
109
83
|
|
|
110
|
-
|
|
111
|
-
///
|
|
112
|
-
/// - `get` returns the current token under lock.
|
|
113
|
-
/// - `del` clears the token under lock.
|
|
114
|
-
private lazy var fcmProvider: RNFCMProvider = RNFCMProvider(
|
|
115
|
-
get: { [weak self] completion in
|
|
116
|
-
guard let self else { completion(nil); return }
|
|
117
|
-
self.lock.lock(); defer { self.lock.unlock() }
|
|
118
|
-
completion(self.fcm)
|
|
119
|
-
},
|
|
120
|
-
del: { [weak self] completion in
|
|
121
|
-
guard let self else { completion(false); return }
|
|
122
|
-
self.lock.lock(); self.fcm = nil; self.lock.unlock()
|
|
123
|
-
completion(true)
|
|
124
|
-
}
|
|
125
|
-
)
|
|
126
|
-
|
|
127
|
-
/// Stable HMS provider reference created once and reused.
|
|
128
|
-
///
|
|
129
|
-
/// - `get` returns the current token under lock.
|
|
130
|
-
/// - `del` clears the token under lock.
|
|
131
|
-
private lazy var hmsProvider: RNHMSProvider = RNHMSProvider(
|
|
132
|
-
get: { [weak self] completion in
|
|
133
|
-
guard let self else { completion(nil); return }
|
|
134
|
-
self.lock.lock(); defer { self.lock.unlock() }
|
|
135
|
-
completion(self.hms)
|
|
136
|
-
},
|
|
137
|
-
del: { [weak self] completion in
|
|
138
|
-
guard let self else { completion(false); return }
|
|
139
|
-
self.lock.lock(); self.hms = nil; self.lock.unlock()
|
|
140
|
-
completion(true)
|
|
141
|
-
}
|
|
142
|
-
)
|
|
143
|
-
|
|
144
|
-
/// Stable APNs provider reference created once and reused.
|
|
145
|
-
///
|
|
146
|
-
/// APNs token is read under lock and returned through completion.
|
|
147
|
-
private lazy var apnsProvider: RNAPNSProvider = RNAPNSProvider { [weak self] completion in
|
|
148
|
-
guard let self else { completion(nil); return }
|
|
149
|
-
self.lock.lock(); defer { self.lock.unlock() }
|
|
150
|
-
completion(self.apns)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/// Indicates whether token providers have been registered in the SDK.
|
|
154
|
-
private var tokenProvidersInstalled = false
|
|
155
|
-
|
|
156
|
-
// ---- Events (single subscriber like Android) ----
|
|
157
|
-
|
|
158
|
-
/// Indicates whether the module is currently subscribed to SDK events.
|
|
159
|
-
///
|
|
160
|
-
/// This is a guard to keep the subscription single (idempotent subscribe/unsubscribe).
|
|
161
|
-
private var eventsSubscribed: Bool = false
|
|
162
|
-
|
|
163
|
-
// MARK: - Install providers once (App module only)
|
|
164
|
-
|
|
165
|
-
/// Ensures all token providers are installed in the SDK.
|
|
166
|
-
///
|
|
167
|
-
/// This is called from initialization and from token setters to guarantee the SDK
|
|
168
|
-
/// always has stable references.
|
|
169
|
-
public func ensureProvidersInstalled() {
|
|
170
|
-
installTokenProvidersIfNeeded()
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/// Registers token providers (FCM/HMS/APNs) in the SDK exactly once.
|
|
174
|
-
///
|
|
175
|
-
/// Uses a double-checked locking pattern to avoid repeated work.
|
|
176
|
-
private func installTokenProvidersIfNeeded() {
|
|
177
|
-
lock.lock()
|
|
178
|
-
let already = tokenProvidersInstalled
|
|
179
|
-
lock.unlock()
|
|
180
|
-
if already { return }
|
|
181
|
-
|
|
182
|
-
let push = AltcraftSDK.shared.pushTokenFunction
|
|
183
|
-
push.setFCMTokenProvider(fcmProvider)
|
|
184
|
-
push.setHMSTokenProvider(hmsProvider)
|
|
185
|
-
push.setAPNSTokenProvider(apnsProvider)
|
|
186
|
-
|
|
187
|
-
lock.lock()
|
|
188
|
-
tokenProvidersInstalled = true
|
|
189
|
-
lock.unlock()
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// MARK: - ✅ RN init (ONLY initialize)
|
|
84
|
+
// MARK: - SDK Initialization (RN Promise)
|
|
193
85
|
|
|
194
|
-
/// Initializes Altcraft SDK using
|
|
86
|
+
/// Initializes the Altcraft SDK using a React Native configuration object.
|
|
195
87
|
///
|
|
196
|
-
///
|
|
197
|
-
/// -
|
|
198
|
-
/// -
|
|
199
|
-
///
|
|
200
|
-
/// - `ALTCRAFT_INIT_ERROR` when SDK initialization reports failure.
|
|
88
|
+
/// The configuration dictionary supports:
|
|
89
|
+
/// - Required: `apiUrl` (String)
|
|
90
|
+
/// - Optional: `rToken` (String), `enableLogging` (Bool), `providerPriorityList` ([String]),
|
|
91
|
+
/// `appInfo` (Map with `appID`, `appIID`, `appVer`)
|
|
201
92
|
///
|
|
202
|
-
///
|
|
203
|
-
///
|
|
93
|
+
/// - Parameters:
|
|
94
|
+
/// - config: React Native configuration map (`NSDictionary`).
|
|
95
|
+
/// - resolve: Promise resolver called with `nil` on success.
|
|
96
|
+
/// - reject: Promise rejecter called with an error code and message on failure.
|
|
204
97
|
@objc(initializeWithConfig:resolver:rejecter:)
|
|
205
98
|
public func initialize(
|
|
206
99
|
_ config: NSDictionary,
|
|
207
100
|
resolver resolve: @escaping RCTPromiseResolveBlock,
|
|
208
101
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
209
102
|
) {
|
|
210
|
-
|
|
103
|
+
let dict: [String: Any?] = Converter.toAnyDict(config) ?? [:]
|
|
211
104
|
|
|
212
|
-
let
|
|
213
|
-
|
|
214
|
-
guard let apiUrl = dict["apiUrl"] as? String, !apiUrl.isEmpty else {
|
|
215
|
-
reject("ALTCRAFT_INIT_INVALID_CONFIG", "apiUrl is required", nil)
|
|
105
|
+
guard let apiUrl = dict[RnConstants.keyApiUrl] as? String, !apiUrl.isEmpty else {
|
|
106
|
+
reject(RnConstants.errInitInvalidConfig, RnConstants.msgApiUrlRequired, nil)
|
|
216
107
|
return
|
|
217
108
|
}
|
|
218
|
-
|
|
219
|
-
let
|
|
220
|
-
let enableLogging = dict[
|
|
221
|
-
let
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
if let app = dict["appInfo"] as? [String: Any] {
|
|
225
|
-
let appID = (app["appID"] as? String) ?? ""
|
|
226
|
-
let appIID = (app["appIID"] as? String) ?? ""
|
|
227
|
-
let appVer = (app["appVer"] as? String) ?? ""
|
|
228
|
-
if !appID.isEmpty || !appIID.isEmpty || !appVer.isEmpty {
|
|
229
|
-
appInfo = AppInfo(appID: appID, appIID: appIID, appVer: appVer)
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
109
|
+
let providerPriorityList = dict[RnConstants.keyProviderPriorityList] as? [String]
|
|
110
|
+
let appInfo = Converter.appInfo(from: dict[RnConstants.keyAppInfo] ?? nil)
|
|
111
|
+
let enableLogging = dict[RnConstants.keyEnableLogging] as? Bool
|
|
112
|
+
let rToken = dict[RnConstants.keyRToken] as? String
|
|
113
|
+
|
|
114
|
+
|
|
233
115
|
guard let built = AltcraftConfiguration.Builder()
|
|
234
116
|
.setApiUrl(apiUrl)
|
|
235
117
|
.setRToken(rToken)
|
|
@@ -238,378 +120,232 @@ public final class SdkModule: NSObject {
|
|
|
238
120
|
.setEnableLogging(enableLogging)
|
|
239
121
|
.build()
|
|
240
122
|
else {
|
|
241
|
-
reject(
|
|
123
|
+
reject(
|
|
124
|
+
RnConstants.errInitInvalidConfig,
|
|
125
|
+
RnConstants.msgInvalidConfiguration,
|
|
126
|
+
nil
|
|
127
|
+
)
|
|
242
128
|
return
|
|
243
129
|
}
|
|
244
130
|
|
|
245
131
|
lock.lock()
|
|
246
|
-
|
|
132
|
+
configuration = built
|
|
247
133
|
lock.unlock()
|
|
248
134
|
|
|
249
135
|
AltcraftSDK.shared.initialization(configuration: built) { ok in
|
|
250
136
|
if ok { resolve(nil) }
|
|
251
|
-
else {
|
|
137
|
+
else {
|
|
138
|
+
reject(
|
|
139
|
+
RnConstants.errInitError,
|
|
140
|
+
RnConstants.msgInitializationFailed, nil
|
|
141
|
+
)
|
|
142
|
+
}
|
|
252
143
|
}
|
|
253
144
|
}
|
|
254
145
|
|
|
255
|
-
// MARK: -
|
|
146
|
+
// MARK: - NativeEventEmitter compatibility
|
|
256
147
|
|
|
257
|
-
/// Required by `NativeEventEmitter`
|
|
148
|
+
/// Required by `NativeEventEmitter` on the JS side. No-op on iOS.
|
|
258
149
|
///
|
|
259
|
-
///
|
|
150
|
+
/// - Parameter eventName: Event name from JS.
|
|
260
151
|
public func addListener(_ eventName: String) { _ = eventName }
|
|
261
152
|
|
|
262
|
-
/// Required by `NativeEventEmitter`
|
|
153
|
+
/// Required by `NativeEventEmitter` on the JS side. No-op on iOS.
|
|
263
154
|
///
|
|
264
|
-
///
|
|
155
|
+
/// - Parameter count: Number of listeners removed on JS side.
|
|
265
156
|
public func removeListeners(_ count: NSNumber) { _ = count }
|
|
266
157
|
|
|
267
|
-
// MARK: -
|
|
268
|
-
|
|
269
|
-
/// Subscribes to native SDK events and forwards them to JS.
|
|
270
|
-
///
|
|
271
|
-
/// Single-subscription behavior:
|
|
272
|
-
/// - Multiple calls are safe (idempotent).
|
|
273
|
-
/// - Events are emitted via NotificationCenter using `.altcraftSdkEventNotification`.
|
|
274
|
-
///
|
|
275
|
-
/// Notes:
|
|
276
|
-
/// - The actual event source is `SDKEvents.shared`, consistent with the native SDK event bus.
|
|
277
|
-
public func subscribeToEvents() {
|
|
278
|
-
lock.lock()
|
|
279
|
-
let already = eventsSubscribed
|
|
280
|
-
if !already { eventsSubscribed = true }
|
|
281
|
-
lock.unlock()
|
|
282
|
-
if already { return }
|
|
283
|
-
|
|
284
|
-
// Subscribe to native SDK events emitted through SDKEvents.shared.emit(event:)
|
|
285
|
-
SDKEvents.shared.subscribe { [weak self] ev in
|
|
286
|
-
guard let self else { return }
|
|
158
|
+
// MARK: - RCTEventEmitter
|
|
287
159
|
|
|
288
|
-
|
|
160
|
+
@objc(SDKEventEmitter)
|
|
161
|
+
final class SDKEventEmitter: RCTEventEmitter {
|
|
289
162
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
self.lock.unlock()
|
|
294
|
-
if !subscribed { return }
|
|
163
|
+
private static weak var _shared: SDKEventEmitter?
|
|
164
|
+
private static var pending: [(String, Any?)] = []
|
|
165
|
+
private static let pendingLock = NSLock()
|
|
295
166
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
)
|
|
167
|
+
/// Creates the event emitter instance and flushes any buffered events.
|
|
168
|
+
override init() {
|
|
169
|
+
super.init()
|
|
170
|
+
SDKEventEmitter._shared = self
|
|
171
|
+
SDKEventEmitter.flushPendingIfPossible()
|
|
302
172
|
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/// Unsubscribes from the SDK event stream.
|
|
306
|
-
///
|
|
307
|
-
/// After this call, events are no longer forwarded to JS.
|
|
308
|
-
public func unsubscribeFromEvent() {
|
|
309
|
-
lock.lock()
|
|
310
|
-
eventsSubscribed = false
|
|
311
|
-
lock.unlock()
|
|
312
173
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
///
|
|
318
|
-
/// Output schema (matches Android bridge):
|
|
319
|
-
/// - function: String
|
|
320
|
-
/// - code: Int | null
|
|
321
|
-
/// - message: String
|
|
322
|
-
/// - type: "event" | "error" | "retryError"
|
|
323
|
-
/// - value: Object | null
|
|
324
|
-
private func mapSdkEventToJs(_ ev: Event) -> [String: Any] {
|
|
325
|
-
let message = ev.message ?? String(describing: ev)
|
|
326
|
-
let code: Any = ev.eventCode.map { $0 } ?? NSNull()
|
|
174
|
+
/// Returns the list of supported event names for React Native.
|
|
175
|
+
override func supportedEvents() -> [String]! {
|
|
176
|
+
[RnConstants.jsEventName]
|
|
177
|
+
}
|
|
327
178
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
} else if ev is ErrorEvent {
|
|
332
|
-
type = "error"
|
|
333
|
-
} else {
|
|
334
|
-
type = "event"
|
|
179
|
+
/// Indicates that this module must be initialized on the main thread.
|
|
180
|
+
@objc override static func requiresMainQueueSetup() -> Bool {
|
|
181
|
+
true
|
|
335
182
|
}
|
|
336
183
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
184
|
+
/// Emits an Altcraft event with an integer code to JS.
|
|
185
|
+
///
|
|
186
|
+
/// - Parameter code: Event code.
|
|
187
|
+
@objc static func emitAltcraft(code: Int) {
|
|
188
|
+
emit(name: RnConstants.jsEventName, body: [RnConstants.keyCode: code])
|
|
342
189
|
}
|
|
343
190
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
191
|
+
/// Emits an event to JS. If the emitter is not ready, the event is buffered.
|
|
192
|
+
///
|
|
193
|
+
/// - Parameters:
|
|
194
|
+
/// - name: Event name.
|
|
195
|
+
/// - body: Event payload.
|
|
196
|
+
@objc static func emit(name: String, body: Any?) {
|
|
197
|
+
let deliver = {
|
|
198
|
+
if let emitter = _shared {
|
|
199
|
+
emitter.sendEvent(withName: name, body: body)
|
|
200
|
+
} else {
|
|
201
|
+
buffer(name: name, body: body)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
352
204
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
/// All values are sanitized to supported JS types.
|
|
356
|
-
private func sanitizeToJsObject(_ dict: [String: Any]) -> NSDictionary {
|
|
357
|
-
let out = NSMutableDictionary(capacity: dict.count)
|
|
358
|
-
for (k, v) in dict {
|
|
359
|
-
out[k] = sanitizeAnyToJs(v)
|
|
205
|
+
if Thread.isMainThread { deliver() }
|
|
206
|
+
else { DispatchQueue.main.async { deliver() } }
|
|
360
207
|
}
|
|
361
|
-
return out
|
|
362
|
-
}
|
|
363
208
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
///
|
|
370
|
-
/// Unsupported values are stringified via `String(describing:)`.
|
|
371
|
-
private func sanitizeAnyToJs(_ v: Any) -> Any {
|
|
372
|
-
if v is NSNull { return NSNull() }
|
|
373
|
-
|
|
374
|
-
if let s = v as? String { return s }
|
|
375
|
-
if let n = v as? NSNumber { return n }
|
|
376
|
-
if let b = v as? Bool { return NSNumber(value: b) }
|
|
377
|
-
if let i = v as? Int { return NSNumber(value: i) }
|
|
378
|
-
if let d = v as? Double { return NSNumber(value: d) }
|
|
379
|
-
if let f = v as? Float { return NSNumber(value: Double(f)) }
|
|
380
|
-
|
|
381
|
-
if let arr = v as? [Any] {
|
|
382
|
-
return arr.map { sanitizeAnyToJs($0) }
|
|
209
|
+
/// Buffers an event until the emitter becomes available.
|
|
210
|
+
private static func buffer(name: String, body: Any?) {
|
|
211
|
+
pendingLock.lock()
|
|
212
|
+
pending.append((name, body))
|
|
213
|
+
pendingLock.unlock()
|
|
383
214
|
}
|
|
384
215
|
|
|
385
|
-
if
|
|
386
|
-
|
|
387
|
-
|
|
216
|
+
/// Flushes buffered events if the emitter is available, always on the main thread.
|
|
217
|
+
private static func flushPendingIfPossible() {
|
|
218
|
+
guard Thread.isMainThread else {
|
|
219
|
+
DispatchQueue.main.async { flushPendingIfPossible() }
|
|
220
|
+
return
|
|
221
|
+
}
|
|
222
|
+
guard let emitter = _shared else { return }
|
|
388
223
|
|
|
389
|
-
|
|
224
|
+
pendingLock.lock()
|
|
225
|
+
let toSend = pending
|
|
226
|
+
pending.removeAll()
|
|
227
|
+
pendingLock.unlock()
|
|
228
|
+
|
|
229
|
+
for (name, body) in toSend {
|
|
230
|
+
emitter.sendEvent(withName: name, body: body)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
390
233
|
}
|
|
391
234
|
|
|
392
|
-
// MARK: -
|
|
235
|
+
// MARK: - SDK Events subscription
|
|
393
236
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
///
|
|
237
|
+
private var eventsSubscribed: Bool = false
|
|
238
|
+
|
|
239
|
+
/// Subscribes to native SDK events and forwards them to JS via the event emitter.
|
|
397
240
|
///
|
|
398
|
-
///
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
241
|
+
/// Multiple calls are safe; the subscription is activated only once.
|
|
242
|
+
public func subscribeToEvents() {
|
|
243
|
+
lock.lock()
|
|
244
|
+
let already = eventsSubscribed
|
|
245
|
+
if !already { eventsSubscribed = true }
|
|
246
|
+
lock.unlock()
|
|
247
|
+
if already { return }
|
|
248
|
+
|
|
249
|
+
SDKEvents.shared.subscribe { [weak self] event in
|
|
250
|
+
guard let self else { return }
|
|
251
|
+
SDKEventEmitter.emit(
|
|
252
|
+
name: RnConstants.jsEventName,
|
|
253
|
+
body: toPayload(event)
|
|
254
|
+
)
|
|
411
255
|
}
|
|
412
256
|
}
|
|
413
257
|
|
|
414
|
-
///
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
/// - Otherwise: provider remains registered and will supply the token.
|
|
421
|
-
@objc(setHMS:)
|
|
422
|
-
public func setHMS(_ token: String?) {
|
|
423
|
-
ensureProvidersInstalled()
|
|
424
|
-
lock.lock(); hms = token; lock.unlock()
|
|
425
|
-
|
|
426
|
-
let push = AltcraftSDK.shared.pushTokenFunction
|
|
427
|
-
if token == nil || token?.isEmpty == true {
|
|
428
|
-
push.setHMSTokenProvider(nil)
|
|
429
|
-
} else {
|
|
430
|
-
push.setHMSTokenProvider(hmsProvider)
|
|
431
|
-
}
|
|
258
|
+
/// Unsubscribes from native SDK events.
|
|
259
|
+
public func unsubscribeFromEvent() {
|
|
260
|
+
lock.lock()
|
|
261
|
+
eventsSubscribed = false
|
|
262
|
+
lock.unlock()
|
|
263
|
+
SDKEvents.shared.unsubscribe()
|
|
432
264
|
}
|
|
433
265
|
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
///
|
|
266
|
+
// MARK: - Event payload mapping
|
|
267
|
+
|
|
268
|
+
/// Maps a native SDK event to a JS-friendly payload dictionary.
|
|
437
269
|
///
|
|
438
|
-
///
|
|
439
|
-
/// -
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
270
|
+
/// - Parameter event: Native event.
|
|
271
|
+
/// - Returns: Dictionary containing `function`, `code`, `message`, `type`, and `value`.
|
|
272
|
+
private func toPayload(_ event: Event) -> [String: Any] {
|
|
273
|
+
let type: String
|
|
274
|
+
if event is RetryEvent { type = RnConstants.typeRetryError }
|
|
275
|
+
else if event is ErrorEvent { type = RnConstants.typeError }
|
|
276
|
+
else { type = RnConstants.typeEvent }
|
|
277
|
+
|
|
278
|
+
return [
|
|
279
|
+
RnConstants.keyFunction: event.function,
|
|
280
|
+
RnConstants.keyCode: event.eventCode as Any? ?? NSNull(),
|
|
281
|
+
RnConstants.keyMessage: event.message ?? "",
|
|
282
|
+
RnConstants.keyType: type,
|
|
283
|
+
RnConstants.keyValue: Converter.toAny(event.value) ?? NSNull()
|
|
284
|
+
]
|
|
452
285
|
}
|
|
453
286
|
|
|
454
|
-
// MARK: -
|
|
287
|
+
// MARK: - SDK Clear (RN Promise)
|
|
455
288
|
|
|
456
|
-
/// Clears SDK state
|
|
289
|
+
/// Clears SDK local state and stored data.
|
|
457
290
|
///
|
|
458
|
-
///
|
|
459
|
-
///
|
|
291
|
+
/// - Parameters:
|
|
292
|
+
/// - resolve: Promise resolver called with `nil` when cleanup finishes.
|
|
293
|
+
/// - reject: Promise rejecter (unused; kept for RN signature compatibility).
|
|
460
294
|
@objc(clearWithResolver:rejecter:)
|
|
461
295
|
public func clear(
|
|
462
296
|
resolver resolve: @escaping RCTPromiseResolveBlock,
|
|
463
297
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
464
298
|
) {
|
|
465
|
-
|
|
466
|
-
AltcraftSDK.shared.clear {
|
|
467
|
-
resolve(nil)
|
|
468
|
-
}
|
|
299
|
+
_ = reject
|
|
300
|
+
AltcraftSDK.shared.clear { resolve(nil) }
|
|
469
301
|
}
|
|
470
302
|
|
|
471
|
-
// MARK: - Token API wrappers
|
|
303
|
+
// MARK: - Token API wrappers (ONLY getPushToken / setPushToken)
|
|
472
304
|
|
|
473
|
-
/// Returns the
|
|
305
|
+
/// Returns the current push token data from the native SDK.
|
|
474
306
|
///
|
|
475
|
-
/// - Parameter completion: Called with `{ provider, token }` or `nil` if
|
|
307
|
+
/// - Parameter completion: Called with `{ provider, token }` or `nil` if unavailable.
|
|
476
308
|
@objc(getPushTokenWithCompletion:)
|
|
477
309
|
public func getPushToken(_ completion: @escaping ([String: Any]?) -> Void) {
|
|
478
|
-
ensureProvidersInstalled()
|
|
479
310
|
AltcraftSDK.shared.pushTokenFunction.getPushToken { tokenData in
|
|
480
311
|
guard let tokenData else { completion(nil); return }
|
|
481
312
|
completion([
|
|
482
|
-
|
|
483
|
-
|
|
313
|
+
RnConstants.keyProvider: tokenData.provider,
|
|
314
|
+
RnConstants.keyToken: tokenData.token
|
|
484
315
|
])
|
|
485
316
|
}
|
|
486
317
|
}
|
|
487
318
|
|
|
488
|
-
///
|
|
319
|
+
/// Sets (or clears) the push token for a given provider.
|
|
489
320
|
///
|
|
490
321
|
/// - Parameters:
|
|
491
|
-
/// - provider: Provider
|
|
492
|
-
/// -
|
|
493
|
-
@objc(deleteDeviceTokenWithProvider:completion:)
|
|
494
|
-
public func deleteDeviceToken(provider: String?, completion: @escaping (Bool) -> Void) {
|
|
495
|
-
ensureProvidersInstalled()
|
|
496
|
-
let p = (provider ?? "").lowercased()
|
|
497
|
-
_ = p // keep for potential normalization usage
|
|
498
|
-
|
|
499
|
-
if provider == Constants.ProviderName.firebase {
|
|
500
|
-
AltcraftSDK.shared.pushTokenFunction.deleteDeviceToken(provider: Constants.ProviderName.firebase) {
|
|
501
|
-
completion(true)
|
|
502
|
-
}
|
|
503
|
-
return
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
if provider == Constants.ProviderName.huawei {
|
|
507
|
-
AltcraftSDK.shared.pushTokenFunction.deleteDeviceToken(provider: Constants.ProviderName.huawei) {
|
|
508
|
-
completion(true)
|
|
509
|
-
}
|
|
510
|
-
return
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
if provider == Constants.ProviderName.apns {
|
|
514
|
-
AltcraftSDK.shared.pushTokenFunction.deleteDeviceToken(provider: Constants.ProviderName.apns) {
|
|
515
|
-
completion(true)
|
|
516
|
-
}
|
|
517
|
-
return
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
completion(false)
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
/// Forces push token update flow in the SDK.
|
|
524
|
-
///
|
|
525
|
-
/// - Parameter completion: Called when the update flow finishes.
|
|
526
|
-
@objc(forcedTokenUpdateWithCompletion:)
|
|
527
|
-
public func forcedTokenUpdate(_ completion: @escaping () -> Void) {
|
|
528
|
-
ensureProvidersInstalled()
|
|
529
|
-
AltcraftSDK.shared.pushTokenFunction.forcedTokenUpdate {
|
|
530
|
-
completion()
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
/// Updates push provider priority list.
|
|
535
|
-
///
|
|
536
|
-
/// - Parameters:
|
|
537
|
-
/// - list: Provider ids ordered by preference.
|
|
538
|
-
/// - completion: Returns `true` when the list is applied.
|
|
539
|
-
@objc(changePushProviderPriorityListWithList:completion:)
|
|
540
|
-
public func changePushProviderPriorityList(_ list: [String]?, completion: @escaping (Bool) -> Void) {
|
|
541
|
-
ensureProvidersInstalled()
|
|
542
|
-
AltcraftSDK.shared.pushTokenFunction.changePushProviderPriorityList(list ?? [])
|
|
543
|
-
completion(true)
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
/// Sets or clears push token for a specific provider.
|
|
547
|
-
///
|
|
548
|
-
/// This is a direct passthrough to the SDK API. The SDK decides how to interpret `pushToken`.
|
|
322
|
+
/// - provider: Provider identifier (e.g., `apns`, `fcm`).
|
|
323
|
+
/// - pushToken: Token value. `nil` / `NSNull` clears the token. Non-string values are stringified.
|
|
549
324
|
@objc(setPushTokenWithProvider:pushToken:)
|
|
550
325
|
public func setPushToken(provider: String, pushToken: Any?) {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
/// - empty dictionaries
|
|
562
|
-
private func toAnyDict(_ dict: NSDictionary?) -> [String: Any?]? {
|
|
563
|
-
guard let dict = dict as? [String: Any] else { return nil }
|
|
564
|
-
if dict.isEmpty { return nil }
|
|
565
|
-
var out: [String: Any?] = [:]
|
|
566
|
-
out.reserveCapacity(dict.count)
|
|
567
|
-
for (k, v) in dict {
|
|
568
|
-
out[k] = v
|
|
569
|
-
}
|
|
570
|
-
return out.isEmpty ? nil : out
|
|
326
|
+
let tokenStr: String? = {
|
|
327
|
+
if pushToken == nil || pushToken is NSNull { return nil }
|
|
328
|
+
if let s = pushToken as? String { return s }
|
|
329
|
+
return String(describing: pushToken!)
|
|
330
|
+
}()
|
|
331
|
+
|
|
332
|
+
AltcraftSDK.shared.pushTokenFunction.setPushToken(
|
|
333
|
+
provider: provider,
|
|
334
|
+
pushToken: (tokenStr ?? "")
|
|
335
|
+
)
|
|
571
336
|
}
|
|
572
337
|
|
|
573
|
-
|
|
574
|
-
///
|
|
575
|
-
/// Returns `nil` for:
|
|
576
|
-
/// - nil input
|
|
577
|
-
/// - empty input
|
|
578
|
-
/// - when all items are invalid/unparseable
|
|
579
|
-
private func toCats(_ cats: NSArray?) -> [CategoryData]? {
|
|
580
|
-
guard let arr = cats as? [Any], !arr.isEmpty else { return nil }
|
|
581
|
-
|
|
582
|
-
var out: [CategoryData] = []
|
|
583
|
-
out.reserveCapacity(arr.count)
|
|
584
|
-
|
|
585
|
-
for item in arr {
|
|
586
|
-
guard let m = item as? [String: Any] else { continue }
|
|
587
|
-
|
|
588
|
-
let name = m["name"] as? String
|
|
589
|
-
let title = m["title"] as? String
|
|
590
|
-
let steady = m["steady"] as? Bool
|
|
591
|
-
let active = m["active"] as? Bool
|
|
592
|
-
|
|
593
|
-
out.append(
|
|
594
|
-
CategoryData(
|
|
595
|
-
name: name,
|
|
596
|
-
title: title,
|
|
597
|
-
steady: steady,
|
|
598
|
-
active: active
|
|
599
|
-
)
|
|
600
|
-
)
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
return out.isEmpty ? nil : out
|
|
604
|
-
}
|
|
338
|
+
// MARK: - Push Subscription (void)
|
|
605
339
|
|
|
606
|
-
///
|
|
340
|
+
/// Sends a push subscribe request (fire-and-forget).
|
|
607
341
|
///
|
|
608
|
-
///
|
|
609
|
-
///
|
|
610
|
-
///
|
|
611
|
-
///
|
|
612
|
-
///
|
|
342
|
+
/// - Parameters:
|
|
343
|
+
/// - sync: Execution mode. Defaults to `true` when `nil`.
|
|
344
|
+
/// - profileFields: Optional profile fields map.
|
|
345
|
+
/// - customFields: Optional custom fields map.
|
|
346
|
+
/// - cats: Optional categories array.
|
|
347
|
+
/// - replace: Optional replace flag.
|
|
348
|
+
/// - skipTriggers: Optional flag to skip trigger execution on the server.
|
|
613
349
|
@objc(pushSubscribe:profileFields:customFields:cats:replace:skipTriggers:)
|
|
614
350
|
public func pushSubscribe(
|
|
615
351
|
_ sync: NSNumber?,
|
|
@@ -619,25 +355,29 @@ public final class SdkModule: NSObject {
|
|
|
619
355
|
replace: NSNumber?,
|
|
620
356
|
skipTriggers: NSNumber?
|
|
621
357
|
) {
|
|
622
|
-
ensureProvidersInstalled()
|
|
623
|
-
|
|
624
358
|
let s = sync?.boolValue ?? true
|
|
625
359
|
let r: Bool? = (replace != nil) ? replace!.boolValue : nil
|
|
626
360
|
let st: Bool? = (skipTriggers != nil) ? skipTriggers!.boolValue : nil
|
|
627
361
|
|
|
628
362
|
AltcraftSDK.shared.pushSubscriptionFunctions.pushSubscribe(
|
|
629
363
|
sync: s,
|
|
630
|
-
profileFields: toAnyDict(profileFields),
|
|
631
|
-
customFields: toAnyDict(customFields),
|
|
632
|
-
cats: toCats(cats),
|
|
364
|
+
profileFields: Converter.toAnyDict(profileFields),
|
|
365
|
+
customFields: Converter.toAnyDict(customFields),
|
|
366
|
+
cats: Converter.toCats(cats),
|
|
633
367
|
replace: r,
|
|
634
368
|
skipTriggers: st
|
|
635
369
|
)
|
|
636
370
|
}
|
|
637
371
|
|
|
638
|
-
///
|
|
372
|
+
/// Sends a push suspend request (fire-and-forget).
|
|
639
373
|
///
|
|
640
|
-
///
|
|
374
|
+
/// - Parameters:
|
|
375
|
+
/// - sync: Execution mode. Defaults to `true` when `nil`.
|
|
376
|
+
/// - profileFields: Optional profile fields map.
|
|
377
|
+
/// - customFields: Optional custom fields map.
|
|
378
|
+
/// - cats: Optional categories array.
|
|
379
|
+
/// - replace: Optional replace flag.
|
|
380
|
+
/// - skipTriggers: Optional flag to skip trigger execution on the server.
|
|
641
381
|
@objc(pushSuspend:profileFields:customFields:cats:replace:skipTriggers:)
|
|
642
382
|
public func pushSuspend(
|
|
643
383
|
_ sync: NSNumber?,
|
|
@@ -647,25 +387,29 @@ public final class SdkModule: NSObject {
|
|
|
647
387
|
replace: NSNumber?,
|
|
648
388
|
skipTriggers: NSNumber?
|
|
649
389
|
) {
|
|
650
|
-
ensureProvidersInstalled()
|
|
651
|
-
|
|
652
390
|
let s = sync?.boolValue ?? true
|
|
653
391
|
let r: Bool? = (replace != nil) ? replace!.boolValue : nil
|
|
654
392
|
let st: Bool? = (skipTriggers != nil) ? skipTriggers!.boolValue : nil
|
|
655
393
|
|
|
656
394
|
AltcraftSDK.shared.pushSubscriptionFunctions.pushSuspend(
|
|
657
395
|
sync: s,
|
|
658
|
-
profileFields: toAnyDict(profileFields),
|
|
659
|
-
customFields: toAnyDict(customFields),
|
|
660
|
-
cats: toCats(cats),
|
|
396
|
+
profileFields: Converter.toAnyDict(profileFields),
|
|
397
|
+
customFields: Converter.toAnyDict(customFields),
|
|
398
|
+
cats: Converter.toCats(cats),
|
|
661
399
|
replace: r,
|
|
662
400
|
skipTriggers: st
|
|
663
401
|
)
|
|
664
402
|
}
|
|
665
403
|
|
|
666
|
-
///
|
|
404
|
+
/// Sends a push unsubscribe request (fire-and-forget).
|
|
667
405
|
///
|
|
668
|
-
///
|
|
406
|
+
/// - Parameters:
|
|
407
|
+
/// - sync: Execution mode. Defaults to `true` when `nil`.
|
|
408
|
+
/// - profileFields: Optional profile fields map.
|
|
409
|
+
/// - customFields: Optional custom fields map.
|
|
410
|
+
/// - cats: Optional categories array.
|
|
411
|
+
/// - replace: Optional replace flag.
|
|
412
|
+
/// - skipTriggers: Optional flag to skip trigger execution on the server.
|
|
669
413
|
@objc(pushUnSubscribe:profileFields:customFields:cats:replace:skipTriggers:)
|
|
670
414
|
public func pushUnSubscribe(
|
|
671
415
|
_ sync: NSNumber?,
|
|
@@ -675,32 +419,35 @@ public final class SdkModule: NSObject {
|
|
|
675
419
|
replace: NSNumber?,
|
|
676
420
|
skipTriggers: NSNumber?
|
|
677
421
|
) {
|
|
678
|
-
ensureProvidersInstalled()
|
|
679
|
-
|
|
680
422
|
let s = sync?.boolValue ?? true
|
|
681
423
|
let r: Bool? = (replace != nil) ? replace!.boolValue : nil
|
|
682
424
|
let st: Bool? = (skipTriggers != nil) ? skipTriggers!.boolValue : nil
|
|
683
425
|
|
|
684
426
|
AltcraftSDK.shared.pushSubscriptionFunctions.pushUnSubscribe(
|
|
685
427
|
sync: s,
|
|
686
|
-
profileFields: toAnyDict(profileFields),
|
|
687
|
-
customFields: toAnyDict(customFields),
|
|
688
|
-
cats: toCats(cats),
|
|
428
|
+
profileFields: Converter.toAnyDict(profileFields),
|
|
429
|
+
customFields: Converter.toAnyDict(customFields),
|
|
430
|
+
cats: Converter.toCats(cats),
|
|
689
431
|
replace: r,
|
|
690
432
|
skipTriggers: st
|
|
691
433
|
)
|
|
692
434
|
}
|
|
693
435
|
|
|
694
|
-
// MARK: -
|
|
436
|
+
// MARK: - Mobile Events bridge
|
|
695
437
|
|
|
696
|
-
/// Sends a
|
|
438
|
+
/// Sends a mobile event to the server (fire-and-forget).
|
|
697
439
|
///
|
|
698
|
-
///
|
|
699
|
-
///
|
|
700
|
-
///
|
|
701
|
-
///
|
|
702
|
-
///
|
|
703
|
-
|
|
440
|
+
/// - Parameters:
|
|
441
|
+
/// - sid: Pixel identifier.
|
|
442
|
+
/// - eventName: Event name.
|
|
443
|
+
/// - sendMessageId: Optional message identifier to link the event.
|
|
444
|
+
/// - payload: Optional event payload map.
|
|
445
|
+
/// - matching: Optional matching parameters map.
|
|
446
|
+
/// - matchingType: Optional matching type/mode.
|
|
447
|
+
/// - profileFields: Optional profile fields map.
|
|
448
|
+
/// - subscription: Optional subscription definition (email/sms/push/cc_data).
|
|
449
|
+
/// - utm: Optional UTM tags for attribution.
|
|
450
|
+
@objc(mobileEvent:eventName:sendMessageId:payload:matching:matchingType:profileFields:subscription:utm:)
|
|
704
451
|
public func mobileEvent(
|
|
705
452
|
_ sid: String,
|
|
706
453
|
eventName: String,
|
|
@@ -708,181 +455,217 @@ public final class SdkModule: NSObject {
|
|
|
708
455
|
payload: NSDictionary?,
|
|
709
456
|
matching: NSDictionary?,
|
|
710
457
|
matchingType: String?,
|
|
711
|
-
profileFields: NSDictionary
|
|
458
|
+
profileFields: NSDictionary?,
|
|
459
|
+
subscription: NSDictionary?,
|
|
460
|
+
utm: NSDictionary?
|
|
712
461
|
) {
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
let payloadAny = toAnyDict(payload)
|
|
716
|
-
let matchingAny = toAnyDict(matching)
|
|
717
|
-
let profileAny = toAnyDict(profileFields)
|
|
462
|
+
let utmObj = Converter.toUTM(utm)
|
|
463
|
+
let subscriptionObj = Converter.toSubscription(subscription)
|
|
718
464
|
|
|
719
465
|
AltcraftSDK.shared.mobileEventFunctions.mobileEvent(
|
|
720
466
|
sid: sid,
|
|
721
467
|
altcraftClientID: "",
|
|
722
468
|
eventName: eventName,
|
|
723
469
|
sendMessageId: sendMessageId,
|
|
724
|
-
payload:
|
|
725
|
-
matching:
|
|
470
|
+
payload: Converter.toAnyDict(payload),
|
|
471
|
+
matching: Converter.toAnyDict(matching),
|
|
726
472
|
matchingType: matchingType,
|
|
727
|
-
profileFields:
|
|
728
|
-
subscription:
|
|
729
|
-
utm:
|
|
473
|
+
profileFields: Converter.toAnyDict(profileFields),
|
|
474
|
+
subscription: subscriptionObj,
|
|
475
|
+
utm: utmObj
|
|
730
476
|
)
|
|
731
477
|
}
|
|
732
478
|
|
|
733
|
-
// MARK: -
|
|
479
|
+
// MARK: - Promise API: PushSubscription status/results
|
|
734
480
|
|
|
735
|
-
/// Helper: converts `nil` to `NSNull()` for JS payloads.
|
|
736
481
|
private func nsNull(_ v: Any?) -> Any { v ?? NSNull() }
|
|
737
482
|
|
|
738
|
-
///
|
|
483
|
+
/// Unsuspends push subscriptions based on native matching rules.
|
|
739
484
|
///
|
|
740
|
-
///
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
var out: [String: String] = [:]
|
|
744
|
-
out.reserveCapacity(dict.count)
|
|
745
|
-
for (k, v) in dict {
|
|
746
|
-
guard let v else { continue }
|
|
747
|
-
out[k] = String(describing: v)
|
|
748
|
-
}
|
|
749
|
-
return out.isEmpty ? nil : out
|
|
750
|
-
}
|
|
751
|
-
|
|
752
|
-
/// Maps SDK category model to JS object.
|
|
753
|
-
private func mapCategory(_ c: CategoryData) -> [String: Any] {
|
|
754
|
-
[
|
|
755
|
-
"name": nsNull(c.name),
|
|
756
|
-
"title": nsNull(c.title),
|
|
757
|
-
"steady": nsNull(c.steady),
|
|
758
|
-
"active": nsNull(c.active),
|
|
759
|
-
]
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
/// Maps SDK subscription model to JS object.
|
|
763
|
-
private func mapSubscription(_ s: SubscriptionData) -> [String: Any] {
|
|
764
|
-
let fieldsString = toStringMap(s.fields)
|
|
765
|
-
let catsArr: [[String: Any]]? = s.cats?.map { mapCategory($0) }
|
|
766
|
-
|
|
767
|
-
return [
|
|
768
|
-
"subscriptionId": nsNull(s.subscriptionId),
|
|
769
|
-
"hashId": nsNull(s.hashId),
|
|
770
|
-
"provider": nsNull(s.provider),
|
|
771
|
-
"status": nsNull(s.status),
|
|
772
|
-
"fields": nsNull(fieldsString),
|
|
773
|
-
"cats": nsNull(catsArr),
|
|
774
|
-
]
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
/// Maps SDK profile model to JS object.
|
|
778
|
-
private func mapProfile(_ p: ProfileData) -> [String: Any] {
|
|
779
|
-
let subDict: [String: Any]? = p.subscription.map { mapSubscription($0) }
|
|
780
|
-
return [
|
|
781
|
-
"id": nsNull(p.id),
|
|
782
|
-
"status": nsNull(p.status),
|
|
783
|
-
"isTest": nsNull(p.isTest),
|
|
784
|
-
"subscription": nsNull(subDict),
|
|
785
|
-
]
|
|
786
|
-
}
|
|
787
|
-
|
|
788
|
-
/// Maps SDK response model to JS object.
|
|
789
|
-
private func mapResponse(_ r: Response) -> [String: Any] {
|
|
790
|
-
let profileDict: [String: Any]? = r.profile.map { mapProfile($0) }
|
|
791
|
-
return [
|
|
792
|
-
"error": nsNull(r.error),
|
|
793
|
-
"errorText": nsNull(r.errorText),
|
|
794
|
-
"profile": nsNull(profileDict),
|
|
795
|
-
]
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
/// Maps SDK response-with-http model to JS object.
|
|
799
|
-
private func mapResponseWithHttp(_ r: ResponseWithHttp) -> [String: Any] {
|
|
800
|
-
let respDict: [String: Any]? = r.response.map { mapResponse($0) }
|
|
801
|
-
return [
|
|
802
|
-
"httpCode": nsNull(r.httpCode),
|
|
803
|
-
"response": nsNull(respDict),
|
|
804
|
-
]
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
/// Attempts to un-suspend a push subscription.
|
|
808
|
-
///
|
|
809
|
-
/// Promise contract:
|
|
810
|
-
/// - Resolves with `null` if SDK returns nil.
|
|
811
|
-
/// - Resolves with `{ httpCode, response }` if available.
|
|
485
|
+
/// - Parameters:
|
|
486
|
+
/// - resolve: Promise resolver called with a mapped `{ httpCode, response }` or `null`.
|
|
487
|
+
/// - reject: Promise rejecter (unused; kept for RN signature compatibility).
|
|
812
488
|
@objc(unSuspendPushSubscriptionWithResolver:rejecter:)
|
|
813
489
|
public func unSuspendPushSubscription(
|
|
814
490
|
resolver resolve: @escaping RCTPromiseResolveBlock,
|
|
815
491
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
816
492
|
) {
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
493
|
+
_ = reject
|
|
494
|
+
AltcraftSDK.shared.pushSubscriptionFunctions.unSuspendPushSubscription { [weak self] result in
|
|
495
|
+
guard let self else { resolve(NSNull()); return }
|
|
820
496
|
guard let result else { resolve(NSNull()); return }
|
|
821
|
-
resolve(
|
|
497
|
+
resolve(mapResponseWithHttp(result))
|
|
822
498
|
}
|
|
823
499
|
}
|
|
824
500
|
|
|
825
|
-
/// Returns status of the latest subscription
|
|
501
|
+
/// Returns the status of the latest subscription in profile.
|
|
826
502
|
///
|
|
827
|
-
///
|
|
828
|
-
///
|
|
829
|
-
///
|
|
503
|
+
/// - Parameters:
|
|
504
|
+
/// - resolve: Promise resolver called with a mapped `{ httpCode, response }` or `null`.
|
|
505
|
+
/// - reject: Promise rejecter (unused; kept for RN signature compatibility).
|
|
830
506
|
@objc(getStatusOfLatestSubscriptionWithResolver:rejecter:)
|
|
831
507
|
public func getStatusOfLatestSubscription(
|
|
832
508
|
resolver resolve: @escaping RCTPromiseResolveBlock,
|
|
833
509
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
834
510
|
) {
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
511
|
+
_ = reject
|
|
512
|
+
AltcraftSDK.shared.pushSubscriptionFunctions.getStatusOfLatestSubscription { [weak self] result in
|
|
513
|
+
guard let self else { resolve(NSNull()); return }
|
|
838
514
|
guard let result else { resolve(NSNull()); return }
|
|
839
|
-
resolve(
|
|
515
|
+
resolve(mapResponseWithHttp(result))
|
|
840
516
|
}
|
|
841
517
|
}
|
|
842
518
|
|
|
843
|
-
/// Returns status
|
|
519
|
+
/// Returns the status for current subscription (by current provider/token context).
|
|
844
520
|
///
|
|
845
|
-
///
|
|
846
|
-
///
|
|
847
|
-
///
|
|
521
|
+
/// - Parameters:
|
|
522
|
+
/// - resolve: Promise resolver called with a mapped `{ httpCode, response }` or `null`.
|
|
523
|
+
/// - reject: Promise rejecter (unused; kept for RN signature compatibility).
|
|
848
524
|
@objc(getStatusForCurrentSubscriptionWithResolver:rejecter:)
|
|
849
525
|
public func getStatusForCurrentSubscription(
|
|
850
526
|
resolver resolve: @escaping RCTPromiseResolveBlock,
|
|
851
527
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
852
528
|
) {
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
529
|
+
_ = reject
|
|
530
|
+
AltcraftSDK.shared.pushSubscriptionFunctions.getStatusForCurrentSubscription { [weak self] result in
|
|
531
|
+
guard let self else { resolve(NSNull()); return }
|
|
856
532
|
guard let result else { resolve(NSNull()); return }
|
|
857
|
-
resolve(
|
|
533
|
+
resolve(mapResponseWithHttp(result))
|
|
858
534
|
}
|
|
859
535
|
}
|
|
860
536
|
|
|
861
|
-
/// Returns status of the latest subscription
|
|
537
|
+
/// Returns the status of the latest subscription for the given provider.
|
|
862
538
|
///
|
|
863
|
-
///
|
|
864
|
-
///
|
|
865
|
-
///
|
|
539
|
+
/// - Parameters:
|
|
540
|
+
/// - provider: Optional provider identifier.
|
|
541
|
+
/// - resolve: Promise resolver called with a mapped `{ httpCode, response }` or `null`.
|
|
542
|
+
/// - reject: Promise rejecter (unused; kept for RN signature compatibility).
|
|
866
543
|
@objc(getStatusOfLatestSubscriptionForProviderWithProvider:resolver:rejecter:)
|
|
867
544
|
public func getStatusOfLatestSubscriptionForProvider(
|
|
868
545
|
provider: String?,
|
|
869
546
|
resolver resolve: @escaping RCTPromiseResolveBlock,
|
|
870
547
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
871
548
|
) {
|
|
872
|
-
|
|
549
|
+
_ = reject
|
|
550
|
+
AltcraftSDK.shared.pushSubscriptionFunctions
|
|
551
|
+
.getStatusOfLatestSubscriptionForProvider(
|
|
552
|
+
provider: provider
|
|
553
|
+
) { [weak self] result in
|
|
554
|
+
guard let self else { resolve(NSNull()); return }
|
|
555
|
+
guard let result else { resolve(NSNull()); return }
|
|
556
|
+
resolve(mapResponseWithHttp(result))
|
|
557
|
+
}
|
|
558
|
+
}
|
|
873
559
|
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
560
|
+
/// Maps a native `ResponseWithHttp` into a JS-friendly dictionary.
|
|
561
|
+
///
|
|
562
|
+
/// - Parameter r: Response with HTTP object.
|
|
563
|
+
/// - Returns: Dictionary with HTTP code and response payload.
|
|
564
|
+
private func mapResponseWithHttp(_ r: ResponseWithHttp) -> [String: Any] {
|
|
565
|
+
let respDict: [String: Any]? = r.response.map { mapResponse($0) }
|
|
566
|
+
return [
|
|
567
|
+
RnConstants.keyHttpCode: nsNull(r.httpCode),
|
|
568
|
+
RnConstants.keyResponse: nsNull(respDict)
|
|
569
|
+
]
|
|
878
570
|
}
|
|
879
|
-
}
|
|
880
571
|
|
|
881
|
-
|
|
572
|
+
/// Maps a native `Response` into a JS-friendly dictionary.
|
|
573
|
+
///
|
|
574
|
+
/// - Parameter r: Response object.
|
|
575
|
+
/// - Returns: Dictionary with response fields.
|
|
576
|
+
private func mapResponse(_ r: Response) -> [String: Any] {
|
|
577
|
+
let profileDict: [String: Any]? = r.profile.map { mapProfile($0) }
|
|
578
|
+
return [
|
|
579
|
+
RnConstants.keyError: nsNull(r.error),
|
|
580
|
+
RnConstants.keyErrorText: nsNull(r.errorText),
|
|
581
|
+
RnConstants.keyProfile: nsNull(profileDict)
|
|
582
|
+
]
|
|
583
|
+
}
|
|
882
584
|
|
|
883
|
-
|
|
884
|
-
/// Notification posted when an SDK event is forwarded to JS.
|
|
585
|
+
/// Maps a native `ProfileData` into a JS-friendly dictionary.
|
|
885
586
|
///
|
|
886
|
-
///
|
|
887
|
-
|
|
888
|
-
|
|
587
|
+
/// - Parameter p: Profile data.
|
|
588
|
+
/// - Returns: Dictionary with profile fields.
|
|
589
|
+
private func mapProfile(_ p: ProfileData) -> [String: Any] {
|
|
590
|
+
let subDict: [String: Any]? = p.subscription.map { mapSubscription($0) }
|
|
591
|
+
return [
|
|
592
|
+
RnConstants.keyId: nsNull(p.id),
|
|
593
|
+
RnConstants.keyStatus: nsNull(p.status),
|
|
594
|
+
RnConstants.keyIsTest: nsNull(p.isTest),
|
|
595
|
+
RnConstants.keySubscription: nsNull(subDict)
|
|
596
|
+
]
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/// Maps a native `SubscriptionData` into a JS-friendly dictionary.
|
|
600
|
+
///
|
|
601
|
+
/// - Parameter s: Subscription data.
|
|
602
|
+
/// - Returns: Dictionary with subscription fields.
|
|
603
|
+
private func mapSubscription(_ s: SubscriptionData) -> [String: Any] {
|
|
604
|
+
let fieldsString = Converter.toStringMap(s.fields)
|
|
605
|
+
let catsArr: [[String: Any]]? = s.cats?.map { mapCategory($0) }
|
|
606
|
+
return [
|
|
607
|
+
RnConstants.keySubscriptionId: nsNull(s.subscriptionId),
|
|
608
|
+
RnConstants.keyHashId: nsNull(s.hashId),
|
|
609
|
+
RnConstants.keyProvider: nsNull(s.provider),
|
|
610
|
+
RnConstants.keyStatus: nsNull(s.status),
|
|
611
|
+
RnConstants.keyFields: nsNull(fieldsString),
|
|
612
|
+
RnConstants.keyCats: nsNull(catsArr)
|
|
613
|
+
]
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/// Maps a native `CategoryData` into a JS-friendly dictionary.
|
|
617
|
+
///
|
|
618
|
+
/// - Parameter c: Category data.
|
|
619
|
+
/// - Returns: Dictionary with category fields.
|
|
620
|
+
private func mapCategory(_ c: CategoryData) -> [String: Any] {
|
|
621
|
+
[
|
|
622
|
+
"name": nsNull(c.name),
|
|
623
|
+
"title": nsNull(c.title),
|
|
624
|
+
"steady": nsNull(c.steady),
|
|
625
|
+
"active": nsNull(c.active)
|
|
626
|
+
]
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// MARK: - UserDefaults
|
|
630
|
+
|
|
631
|
+
/// Stores a key-value pair in persistent storage.
|
|
632
|
+
///
|
|
633
|
+
/// Platform behavior:
|
|
634
|
+
/// - iOS: Uses `UserDefaults` (optionally scoped by `suiteName` / App Group).
|
|
635
|
+
///
|
|
636
|
+
/// Supported value types:
|
|
637
|
+
/// - Property list compatible values (Bool, Number, String, Data, Date, Arrays/Dictionaries of those)
|
|
638
|
+
/// - Complex values may be converted to a property list representation by `Converter`
|
|
639
|
+
/// - `nil`/`NSNull` removes the stored value
|
|
640
|
+
///
|
|
641
|
+
/// - Parameters:
|
|
642
|
+
/// - suiteName: Optional suite name (App Group) for scoped `UserDefaults`.
|
|
643
|
+
/// - key: Storage key (trimmed; empty keys are ignored).
|
|
644
|
+
/// - value: Value to store or `nil`/`NSNull` to remove.
|
|
645
|
+
@objc(setUserDefaultsValueWithSuiteName:key:value:)
|
|
646
|
+
public func setUserDefaultsValue(
|
|
647
|
+
suiteName: String?,
|
|
648
|
+
key: String,
|
|
649
|
+
value: Any?
|
|
650
|
+
) {
|
|
651
|
+
let k = key.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
652
|
+
if k.isEmpty { return }
|
|
653
|
+
|
|
654
|
+
let sn = suiteName?.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
655
|
+
let ud: UserDefaults = {
|
|
656
|
+
if let sn, !sn.isEmpty, let g = UserDefaults(suiteName: sn) { return g }
|
|
657
|
+
return .standard
|
|
658
|
+
}()
|
|
659
|
+
|
|
660
|
+
if value == nil || value is NSNull {
|
|
661
|
+
ud.removeObject(forKey: k)
|
|
662
|
+
return
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
if let plist = Converter.toPropertyListValue(value) {
|
|
666
|
+
ud.set(plist, forKey: k)
|
|
667
|
+
} else {
|
|
668
|
+
ud.removeObject(forKey: k)
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|