@altcraft/react-native-sdk 0.0.2 → 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 +551 -478
- package/android/src/main/java/com/altcraft/sdk/utilities/Converter.kt +164 -0
- package/android/src/main/java/com/altcraft/sdk/utilities/Extension.kt +72 -0
- package/ios/Converter.swift +483 -46
- package/ios/Sdk.mm +83 -271
- package/ios/SdkModule.swift +335 -493
- 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 +224 -246
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/NativeSdk.d.ts +107 -31
- 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 +162 -94
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/NativeSdk.ts +135 -44
- package/src/Utilities.ts +116 -0
- package/src/index.tsx +281 -311
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnFCMProvider.kt +0 -26
- 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,18 +1,73 @@
|
|
|
1
|
-
//
|
|
1
|
+
//
|
|
2
|
+
// SdkModule.swift
|
|
3
|
+
// AltcraftRnBridge
|
|
4
|
+
//
|
|
5
|
+
// Created by Andrey Pogodin.
|
|
6
|
+
//
|
|
7
|
+
// Copyright © 2026 Altcraft. All rights reserved.
|
|
8
|
+
|
|
9
|
+
|
|
2
10
|
import Foundation
|
|
3
11
|
import Altcraft
|
|
4
12
|
import React
|
|
5
13
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
+
|
|
16
71
|
@objc(SdkModule)
|
|
17
72
|
@objcMembers
|
|
18
73
|
@available(iOSApplicationExtension, unavailable)
|
|
@@ -20,187 +75,43 @@ public final class SdkModule: NSObject {
|
|
|
20
75
|
|
|
21
76
|
// MARK: - Singleton & State
|
|
22
77
|
|
|
23
|
-
/// Shared singleton instance used by the
|
|
78
|
+
/// Shared singleton instance used by the React Native bridge.
|
|
24
79
|
public static let shared = SdkModule()
|
|
25
80
|
|
|
26
|
-
/// Protects all mutable state in this module.
|
|
27
81
|
private let lock = NSLock()
|
|
28
|
-
|
|
29
|
-
/// Last configuration used to initialize the SDK.
|
|
30
82
|
private var configuration: AltcraftConfiguration?
|
|
31
83
|
|
|
32
|
-
// MARK: - JWT Provider
|
|
33
|
-
|
|
34
|
-
/// Current JWT value used by `jwtProvider`. Access under `lock`.
|
|
35
|
-
private var jwt: String?
|
|
36
|
-
|
|
37
|
-
/// Indicates whether the JWT provider has been installed in the SDK.
|
|
38
|
-
private var jwtProviderInstalled = false
|
|
39
|
-
|
|
40
|
-
/// Stable JWT provider reference stored by the SDK for the process lifetime.
|
|
41
|
-
private lazy var jwtProvider: RNJWTProvider = RNJWTProvider { [weak self] in
|
|
42
|
-
guard let self else { return nil }
|
|
43
|
-
self.lock.lock(); defer { self.lock.unlock() }
|
|
44
|
-
return self.jwt
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/// Installs JWT provider into the SDK once.
|
|
48
|
-
private func installJWTProviderIfNeeded() {
|
|
49
|
-
lock.lock()
|
|
50
|
-
let alreadyInstalled = jwtProviderInstalled
|
|
51
|
-
lock.unlock()
|
|
52
|
-
if alreadyInstalled { return }
|
|
53
|
-
|
|
54
|
-
AltcraftSDK.shared.setJWTProvider(provider: jwtProvider)
|
|
55
|
-
|
|
56
|
-
lock.lock()
|
|
57
|
-
jwtProviderInstalled = true
|
|
58
|
-
lock.unlock()
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/// Sets JWT token used by SDK requests.
|
|
62
|
-
///
|
|
63
|
-
/// ObjC selector used by `Sdk.mm`: `setJWT:`
|
|
64
|
-
@objc(setJWT:)
|
|
65
|
-
public func setJWT(_ token: String?) {
|
|
66
|
-
installJWTProviderIfNeeded()
|
|
67
|
-
lock.lock()
|
|
68
|
-
jwt = token
|
|
69
|
-
lock.unlock()
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// MARK: - App Group
|
|
73
|
-
|
|
74
|
-
/// Sets the App Group identifier for shared storage.
|
|
75
|
-
///
|
|
76
|
-
/// ObjC selector used by `Sdk.mm`: `setAppGroupWithName:`
|
|
77
|
-
@objc(setAppGroupWithName:)
|
|
78
|
-
public func setAppGroup(name: String?) {
|
|
79
|
-
AltcraftSDK.shared.setAppGroup(groupName: name)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// MARK: - Push Tokens
|
|
83
|
-
|
|
84
|
-
/// Stored push tokens used by token providers. Access under `lock`.
|
|
85
|
-
private var fcm: String?
|
|
86
|
-
private var hms: String?
|
|
87
|
-
private var apns: String?
|
|
88
|
-
|
|
89
|
-
// MARK: - Token Providers
|
|
90
|
-
|
|
91
|
-
/// Indicates whether token providers have been installed in the SDK.
|
|
92
|
-
private var tokenProvidersInstalled = false
|
|
93
|
-
|
|
94
|
-
/// FCM provider backed by stored `fcm` token.
|
|
95
|
-
private lazy var fcmProvider: RNFCMProvider = RNFCMProvider(
|
|
96
|
-
get: { [weak self] completion in
|
|
97
|
-
guard let self else { completion(nil); return }
|
|
98
|
-
self.lock.lock(); defer { self.lock.unlock() }
|
|
99
|
-
completion(self.fcm)
|
|
100
|
-
},
|
|
101
|
-
del: { [weak self] completion in
|
|
102
|
-
guard let self else { completion(false); return }
|
|
103
|
-
self.lock.lock()
|
|
104
|
-
self.fcm = nil
|
|
105
|
-
self.lock.unlock()
|
|
106
|
-
completion(true)
|
|
107
|
-
}
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
/// HMS provider backed by stored `hms` token.
|
|
111
|
-
private lazy var hmsProvider: RNHMSProvider = RNHMSProvider(
|
|
112
|
-
get: { [weak self] completion in
|
|
113
|
-
guard let self else { completion(nil); return }
|
|
114
|
-
self.lock.lock(); defer { self.lock.unlock() }
|
|
115
|
-
completion(self.hms)
|
|
116
|
-
},
|
|
117
|
-
del: { [weak self] completion in
|
|
118
|
-
guard let self else { completion(false); return }
|
|
119
|
-
self.lock.lock()
|
|
120
|
-
self.hms = nil
|
|
121
|
-
self.lock.unlock()
|
|
122
|
-
completion(true)
|
|
123
|
-
}
|
|
124
|
-
)
|
|
125
|
-
|
|
126
|
-
/// APNs provider backed by stored `apns` token.
|
|
127
|
-
private lazy var apnsProvider: RNAPNSProvider = RNAPNSProvider { [weak self] completion in
|
|
128
|
-
guard let self else { completion(nil); return }
|
|
129
|
-
self.lock.lock(); defer { self.lock.unlock() }
|
|
130
|
-
completion(self.apns)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/// Ensures token providers are installed in the SDK once.
|
|
134
|
-
public func ensureProvidersInstalled() {
|
|
135
|
-
installTokenProvidersIfNeeded()
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/// Installs token providers using a double-checked locking pattern.
|
|
139
|
-
private func installTokenProvidersIfNeeded() {
|
|
140
|
-
lock.lock()
|
|
141
|
-
let alreadyInstalled = tokenProvidersInstalled
|
|
142
|
-
lock.unlock()
|
|
143
|
-
if alreadyInstalled { return }
|
|
144
|
-
|
|
145
|
-
let push = AltcraftSDK.shared.pushTokenFunction
|
|
146
|
-
push.setFCMTokenProvider(fcmProvider)
|
|
147
|
-
push.setHMSTokenProvider(hmsProvider)
|
|
148
|
-
push.setAPNSTokenProvider(apnsProvider)
|
|
149
|
-
|
|
150
|
-
lock.lock()
|
|
151
|
-
tokenProvidersInstalled = true
|
|
152
|
-
lock.unlock()
|
|
153
|
-
}
|
|
154
|
-
|
|
155
84
|
// MARK: - SDK Initialization (RN Promise)
|
|
156
85
|
|
|
157
|
-
/// Initializes Altcraft SDK using
|
|
86
|
+
/// Initializes the Altcraft SDK using a React Native configuration object.
|
|
158
87
|
///
|
|
159
|
-
///
|
|
160
|
-
/// -
|
|
161
|
-
/// -
|
|
162
|
-
///
|
|
163
|
-
/// - `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`)
|
|
164
92
|
///
|
|
165
|
-
///
|
|
166
|
-
///
|
|
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.
|
|
167
97
|
@objc(initializeWithConfig:resolver:rejecter:)
|
|
168
98
|
public func initialize(
|
|
169
99
|
_ config: NSDictionary,
|
|
170
100
|
resolver resolve: @escaping RCTPromiseResolveBlock,
|
|
171
101
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
172
102
|
) {
|
|
173
|
-
ensureProvidersInstalled()
|
|
174
|
-
|
|
175
|
-
// Normalizes numbers/bools, parses JSON strings, and recursively converts nested structures.
|
|
176
103
|
let dict: [String: Any?] = Converter.toAnyDict(config) ?? [:]
|
|
177
104
|
|
|
178
|
-
guard let apiUrl = dict[
|
|
179
|
-
reject(
|
|
105
|
+
guard let apiUrl = dict[RnConstants.keyApiUrl] as? String, !apiUrl.isEmpty else {
|
|
106
|
+
reject(RnConstants.errInitInvalidConfig, RnConstants.msgApiUrlRequired, nil)
|
|
180
107
|
return
|
|
181
108
|
}
|
|
182
|
-
|
|
183
|
-
let
|
|
184
|
-
let enableLogging = dict[
|
|
185
|
-
let
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if let app = dict["appInfo"] as? [String: Any?] {
|
|
189
|
-
let appID = (app["appID"] as? String) ?? ""
|
|
190
|
-
let appIID = (app["appIID"] as? String) ?? ""
|
|
191
|
-
let appVer = (app["appVer"] as? String) ?? ""
|
|
192
|
-
if !appID.isEmpty || !appIID.isEmpty || !appVer.isEmpty {
|
|
193
|
-
appInfo = AppInfo(appID: appID, appIID: appIID, appVer: appVer)
|
|
194
|
-
}
|
|
195
|
-
} else if let app = dict["appInfo"] as? [String: Any] {
|
|
196
|
-
let appID = (app["appID"] as? String) ?? ""
|
|
197
|
-
let appIID = (app["appIID"] as? String) ?? ""
|
|
198
|
-
let appVer = (app["appVer"] as? String) ?? ""
|
|
199
|
-
if !appID.isEmpty || !appIID.isEmpty || !appVer.isEmpty {
|
|
200
|
-
appInfo = AppInfo(appID: appID, appIID: appIID, appVer: appVer)
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
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
|
+
|
|
204
115
|
guard let built = AltcraftConfiguration.Builder()
|
|
205
116
|
.setApiUrl(apiUrl)
|
|
206
117
|
.setRToken(rToken)
|
|
@@ -209,63 +120,79 @@ public final class SdkModule: NSObject {
|
|
|
209
120
|
.setEnableLogging(enableLogging)
|
|
210
121
|
.build()
|
|
211
122
|
else {
|
|
212
|
-
reject(
|
|
123
|
+
reject(
|
|
124
|
+
RnConstants.errInitInvalidConfig,
|
|
125
|
+
RnConstants.msgInvalidConfiguration,
|
|
126
|
+
nil
|
|
127
|
+
)
|
|
213
128
|
return
|
|
214
129
|
}
|
|
215
130
|
|
|
216
131
|
lock.lock()
|
|
217
|
-
|
|
132
|
+
configuration = built
|
|
218
133
|
lock.unlock()
|
|
219
134
|
|
|
220
135
|
AltcraftSDK.shared.initialization(configuration: built) { ok in
|
|
221
136
|
if ok { resolve(nil) }
|
|
222
|
-
else {
|
|
137
|
+
else {
|
|
138
|
+
reject(
|
|
139
|
+
RnConstants.errInitError,
|
|
140
|
+
RnConstants.msgInitializationFailed, nil
|
|
141
|
+
)
|
|
142
|
+
}
|
|
223
143
|
}
|
|
224
144
|
}
|
|
225
145
|
|
|
226
146
|
// MARK: - NativeEventEmitter compatibility
|
|
227
147
|
|
|
228
|
-
/// Required by `NativeEventEmitter`
|
|
148
|
+
/// Required by `NativeEventEmitter` on the JS side. No-op on iOS.
|
|
149
|
+
///
|
|
150
|
+
/// - Parameter eventName: Event name from JS.
|
|
229
151
|
public func addListener(_ eventName: String) { _ = eventName }
|
|
230
152
|
|
|
231
|
-
/// Required by `NativeEventEmitter`
|
|
153
|
+
/// Required by `NativeEventEmitter` on the JS side. No-op on iOS.
|
|
154
|
+
///
|
|
155
|
+
/// - Parameter count: Number of listeners removed on JS side.
|
|
232
156
|
public func removeListeners(_ count: NSNumber) { _ = count }
|
|
233
157
|
|
|
234
158
|
// MARK: - RCTEventEmitter
|
|
235
159
|
|
|
236
|
-
/// Event emitter used by RN to receive Altcraft SDK events.
|
|
237
|
-
///
|
|
238
|
-
/// Emits: `"AltcraftSdkEvent"`.
|
|
239
160
|
@objc(SDKEventEmitter)
|
|
240
161
|
final class SDKEventEmitter: RCTEventEmitter {
|
|
241
162
|
|
|
242
|
-
/// Active emitter instance created by React Native.
|
|
243
163
|
private static weak var _shared: SDKEventEmitter?
|
|
244
|
-
|
|
245
|
-
/// Events buffered before React Native subscribes.
|
|
246
164
|
private static var pending: [(String, Any?)] = []
|
|
247
165
|
private static let pendingLock = NSLock()
|
|
248
166
|
|
|
167
|
+
/// Creates the event emitter instance and flushes any buffered events.
|
|
249
168
|
override init() {
|
|
250
169
|
super.init()
|
|
251
170
|
SDKEventEmitter._shared = self
|
|
252
171
|
SDKEventEmitter.flushPendingIfPossible()
|
|
253
172
|
}
|
|
254
173
|
|
|
174
|
+
/// Returns the list of supported event names for React Native.
|
|
255
175
|
override func supportedEvents() -> [String]! {
|
|
256
|
-
[
|
|
176
|
+
[RnConstants.jsEventName]
|
|
257
177
|
}
|
|
258
178
|
|
|
179
|
+
/// Indicates that this module must be initialized on the main thread.
|
|
259
180
|
@objc override static func requiresMainQueueSetup() -> Bool {
|
|
260
181
|
true
|
|
261
182
|
}
|
|
262
183
|
|
|
263
|
-
/// Emits
|
|
184
|
+
/// Emits an Altcraft event with an integer code to JS.
|
|
185
|
+
///
|
|
186
|
+
/// - Parameter code: Event code.
|
|
264
187
|
@objc static func emitAltcraft(code: Int) {
|
|
265
|
-
emit(name:
|
|
188
|
+
emit(name: RnConstants.jsEventName, body: [RnConstants.keyCode: code])
|
|
266
189
|
}
|
|
267
190
|
|
|
268
|
-
///
|
|
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.
|
|
269
196
|
@objc static func emit(name: String, body: Any?) {
|
|
270
197
|
let deliver = {
|
|
271
198
|
if let emitter = _shared {
|
|
@@ -275,19 +202,18 @@ public final class SdkModule: NSObject {
|
|
|
275
202
|
}
|
|
276
203
|
}
|
|
277
204
|
|
|
278
|
-
if Thread.isMainThread {
|
|
279
|
-
|
|
280
|
-
} else {
|
|
281
|
-
DispatchQueue.main.async { deliver() }
|
|
282
|
-
}
|
|
205
|
+
if Thread.isMainThread { deliver() }
|
|
206
|
+
else { DispatchQueue.main.async { deliver() } }
|
|
283
207
|
}
|
|
284
208
|
|
|
209
|
+
/// Buffers an event until the emitter becomes available.
|
|
285
210
|
private static func buffer(name: String, body: Any?) {
|
|
286
211
|
pendingLock.lock()
|
|
287
212
|
pending.append((name, body))
|
|
288
213
|
pendingLock.unlock()
|
|
289
214
|
}
|
|
290
215
|
|
|
216
|
+
/// Flushes buffered events if the emitter is available, always on the main thread.
|
|
291
217
|
private static func flushPendingIfPossible() {
|
|
292
218
|
guard Thread.isMainThread else {
|
|
293
219
|
DispatchQueue.main.async { flushPendingIfPossible() }
|
|
@@ -308,10 +234,11 @@ public final class SdkModule: NSObject {
|
|
|
308
234
|
|
|
309
235
|
// MARK: - SDK Events subscription
|
|
310
236
|
|
|
311
|
-
/// Indicates whether this module is currently subscribed to SDK events.
|
|
312
237
|
private var eventsSubscribed: Bool = false
|
|
313
238
|
|
|
314
|
-
/// Subscribes to SDK events and forwards them to JS.
|
|
239
|
+
/// Subscribes to native SDK events and forwards them to JS via the event emitter.
|
|
240
|
+
///
|
|
241
|
+
/// Multiple calls are safe; the subscription is activated only once.
|
|
315
242
|
public func subscribeToEvents() {
|
|
316
243
|
lock.lock()
|
|
317
244
|
let already = eventsSubscribed
|
|
@@ -321,11 +248,14 @@ public final class SdkModule: NSObject {
|
|
|
321
248
|
|
|
322
249
|
SDKEvents.shared.subscribe { [weak self] event in
|
|
323
250
|
guard let self else { return }
|
|
324
|
-
SDKEventEmitter.emit(
|
|
251
|
+
SDKEventEmitter.emit(
|
|
252
|
+
name: RnConstants.jsEventName,
|
|
253
|
+
body: toPayload(event)
|
|
254
|
+
)
|
|
325
255
|
}
|
|
326
256
|
}
|
|
327
257
|
|
|
328
|
-
/// Unsubscribes from SDK events.
|
|
258
|
+
/// Unsubscribes from native SDK events.
|
|
329
259
|
public func unsubscribeFromEvent() {
|
|
330
260
|
lock.lock()
|
|
331
261
|
eventsSubscribed = false
|
|
@@ -335,203 +265,87 @@ public final class SdkModule: NSObject {
|
|
|
335
265
|
|
|
336
266
|
// MARK: - Event payload mapping
|
|
337
267
|
|
|
338
|
-
///
|
|
268
|
+
/// Maps a native SDK event to a JS-friendly payload dictionary.
|
|
269
|
+
///
|
|
270
|
+
/// - Parameter event: Native event.
|
|
271
|
+
/// - Returns: Dictionary containing `function`, `code`, `message`, `type`, and `value`.
|
|
339
272
|
private func toPayload(_ event: Event) -> [String: Any] {
|
|
340
273
|
let type: String
|
|
341
|
-
if event is RetryEvent {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
type = "error"
|
|
345
|
-
} else {
|
|
346
|
-
type = "event"
|
|
347
|
-
}
|
|
274
|
+
if event is RetryEvent { type = RnConstants.typeRetryError }
|
|
275
|
+
else if event is ErrorEvent { type = RnConstants.typeError }
|
|
276
|
+
else { type = RnConstants.typeEvent }
|
|
348
277
|
|
|
349
278
|
return [
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
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()
|
|
355
284
|
]
|
|
356
285
|
}
|
|
357
286
|
|
|
358
|
-
// MARK: - Token setters
|
|
359
|
-
|
|
360
|
-
/// Sets (or clears) the stored FCM token and updates SDK provider registration.
|
|
361
|
-
///
|
|
362
|
-
/// ObjC selector used by `Sdk.mm`: `setFCM:`
|
|
363
|
-
@objc(setFCM:)
|
|
364
|
-
public func setFCM(_ token: String?) {
|
|
365
|
-
ensureProvidersInstalled()
|
|
366
|
-
lock.lock(); fcm = token; lock.unlock()
|
|
367
|
-
|
|
368
|
-
let push = AltcraftSDK.shared.pushTokenFunction
|
|
369
|
-
if token == nil || token?.isEmpty == true {
|
|
370
|
-
push.setFCMTokenProvider(nil)
|
|
371
|
-
} else {
|
|
372
|
-
push.setFCMTokenProvider(fcmProvider)
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/// Sets (or clears) the stored HMS token and updates SDK provider registration.
|
|
377
|
-
///
|
|
378
|
-
/// ObjC selector used by `Sdk.mm`: `setHMS:`
|
|
379
|
-
@objc(setHMS:)
|
|
380
|
-
public func setHMS(_ token: String?) {
|
|
381
|
-
ensureProvidersInstalled()
|
|
382
|
-
lock.lock(); hms = token; lock.unlock()
|
|
383
|
-
|
|
384
|
-
let push = AltcraftSDK.shared.pushTokenFunction
|
|
385
|
-
if token == nil || token?.isEmpty == true {
|
|
386
|
-
push.setHMSTokenProvider(nil)
|
|
387
|
-
} else {
|
|
388
|
-
push.setHMSTokenProvider(hmsProvider)
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/// Sets (or clears) the stored APNs token and updates SDK provider registration.
|
|
393
|
-
///
|
|
394
|
-
/// ObjC selector used by `Sdk.mm`: `setAPNS:`
|
|
395
|
-
@objc(setAPNS:)
|
|
396
|
-
public func setAPNS(_ token: String?) {
|
|
397
|
-
ensureProvidersInstalled()
|
|
398
|
-
lock.lock(); apns = token; lock.unlock()
|
|
399
|
-
|
|
400
|
-
let push = AltcraftSDK.shared.pushTokenFunction
|
|
401
|
-
if token == nil || token?.isEmpty == true {
|
|
402
|
-
push.setAPNSTokenProvider(nil)
|
|
403
|
-
} else {
|
|
404
|
-
push.setAPNSTokenProvider(apnsProvider)
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
|
|
408
287
|
// MARK: - SDK Clear (RN Promise)
|
|
409
288
|
|
|
410
|
-
/// Clears SDK state
|
|
289
|
+
/// Clears SDK local state and stored data.
|
|
290
|
+
///
|
|
291
|
+
/// - Parameters:
|
|
292
|
+
/// - resolve: Promise resolver called with `nil` when cleanup finishes.
|
|
293
|
+
/// - reject: Promise rejecter (unused; kept for RN signature compatibility).
|
|
411
294
|
@objc(clearWithResolver:rejecter:)
|
|
412
295
|
public func clear(
|
|
413
296
|
resolver resolve: @escaping RCTPromiseResolveBlock,
|
|
414
297
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
415
298
|
) {
|
|
416
299
|
_ = reject
|
|
417
|
-
ensureProvidersInstalled()
|
|
418
300
|
AltcraftSDK.shared.clear { resolve(nil) }
|
|
419
301
|
}
|
|
420
302
|
|
|
421
|
-
// MARK: - Token API wrappers
|
|
303
|
+
// MARK: - Token API wrappers (ONLY getPushToken / setPushToken)
|
|
422
304
|
|
|
423
|
-
/// Returns the current push token from the SDK.
|
|
305
|
+
/// Returns the current push token data from the native SDK.
|
|
424
306
|
///
|
|
425
|
-
/// - Parameter completion: Called with `{ provider, token }` or `nil` if
|
|
307
|
+
/// - Parameter completion: Called with `{ provider, token }` or `nil` if unavailable.
|
|
426
308
|
@objc(getPushTokenWithCompletion:)
|
|
427
309
|
public func getPushToken(_ completion: @escaping ([String: Any]?) -> Void) {
|
|
428
|
-
ensureProvidersInstalled()
|
|
429
310
|
AltcraftSDK.shared.pushTokenFunction.getPushToken { tokenData in
|
|
430
311
|
guard let tokenData else { completion(nil); return }
|
|
431
312
|
completion([
|
|
432
|
-
|
|
433
|
-
|
|
313
|
+
RnConstants.keyProvider: tokenData.provider,
|
|
314
|
+
RnConstants.keyToken: tokenData.token
|
|
434
315
|
])
|
|
435
316
|
}
|
|
436
317
|
}
|
|
437
318
|
|
|
438
|
-
///
|
|
319
|
+
/// Sets (or clears) the push token for a given provider.
|
|
439
320
|
///
|
|
440
321
|
/// - Parameters:
|
|
441
|
-
/// - provider: Provider
|
|
442
|
-
/// -
|
|
443
|
-
@objc(deleteDeviceTokenWithProvider:completion:)
|
|
444
|
-
public func deleteDeviceToken(provider: String?, completion: @escaping (Bool) -> Void) {
|
|
445
|
-
ensureProvidersInstalled()
|
|
446
|
-
|
|
447
|
-
let p = (provider ?? "").lowercased()
|
|
448
|
-
|
|
449
|
-
if p == Constants.ProviderName.firebase.lowercased() {
|
|
450
|
-
AltcraftSDK.shared.pushTokenFunction.deleteDeviceToken(provider: Constants.ProviderName.firebase) {
|
|
451
|
-
completion(true)
|
|
452
|
-
}
|
|
453
|
-
return
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
if p == Constants.ProviderName.huawei.lowercased() {
|
|
457
|
-
AltcraftSDK.shared.pushTokenFunction.deleteDeviceToken(provider: Constants.ProviderName.huawei) {
|
|
458
|
-
completion(true)
|
|
459
|
-
}
|
|
460
|
-
return
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
if p == Constants.ProviderName.apns.lowercased() {
|
|
464
|
-
AltcraftSDK.shared.pushTokenFunction.deleteDeviceToken(provider: Constants.ProviderName.apns) {
|
|
465
|
-
completion(true)
|
|
466
|
-
}
|
|
467
|
-
return
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
completion(false)
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
/// Forces push token update flow in the SDK.
|
|
474
|
-
@objc(forcedTokenUpdateWithCompletion:)
|
|
475
|
-
public func forcedTokenUpdate(_ completion: @escaping () -> Void) {
|
|
476
|
-
ensureProvidersInstalled()
|
|
477
|
-
AltcraftSDK.shared.pushTokenFunction.forcedTokenUpdate { completion() }
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
/// Updates push provider priority list.
|
|
481
|
-
@objc(changePushProviderPriorityListWithList:completion:)
|
|
482
|
-
public func changePushProviderPriorityList(_ list: [String]?, completion: @escaping (Bool) -> Void) {
|
|
483
|
-
ensureProvidersInstalled()
|
|
484
|
-
AltcraftSDK.shared.pushTokenFunction.changePushProviderPriorityList(list ?? [])
|
|
485
|
-
completion(true)
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
/// Sets push token for a specific provider.
|
|
322
|
+
/// - provider: Provider identifier (e.g., `apns`, `fcm`).
|
|
323
|
+
/// - pushToken: Token value. `nil` / `NSNull` clears the token. Non-string values are stringified.
|
|
489
324
|
@objc(setPushTokenWithProvider:pushToken:)
|
|
490
325
|
public func setPushToken(provider: String, pushToken: Any?) {
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
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
|
+
}()
|
|
496
331
|
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
332
|
+
AltcraftSDK.shared.pushTokenFunction.setPushToken(
|
|
333
|
+
provider: provider,
|
|
334
|
+
pushToken: (tokenStr ?? "")
|
|
335
|
+
)
|
|
500
336
|
}
|
|
501
337
|
|
|
502
|
-
|
|
503
|
-
private func toCats(_ cats: NSArray?) -> [CategoryData]? {
|
|
504
|
-
guard let arr = cats, arr.count > 0 else { return nil }
|
|
505
|
-
|
|
506
|
-
var out: [CategoryData] = []
|
|
507
|
-
out.reserveCapacity(arr.count)
|
|
508
|
-
|
|
509
|
-
for item in arr {
|
|
510
|
-
if let s = item as? String, !s.isEmpty {
|
|
511
|
-
out.append(CategoryData(name: s, title: nil, steady: nil, active: true))
|
|
512
|
-
continue
|
|
513
|
-
}
|
|
338
|
+
// MARK: - Push Subscription (void)
|
|
514
339
|
|
|
515
|
-
|
|
516
|
-
let mm = Converter.toAnyDict(m) ?? [:]
|
|
517
|
-
let name = mm["name"] as? String
|
|
518
|
-
let title = mm["title"] as? String
|
|
519
|
-
let steady = mm["steady"] as? Bool
|
|
520
|
-
let active = mm["active"] as? Bool
|
|
521
|
-
|
|
522
|
-
if name != nil {
|
|
523
|
-
out.append(CategoryData(name: name, title: title, steady: steady, active: active))
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
return out.isEmpty ? nil : out
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
/// Subscribes device/profile for push notifications.
|
|
340
|
+
/// Sends a push subscribe request (fire-and-forget).
|
|
532
341
|
///
|
|
533
|
-
///
|
|
534
|
-
/// `
|
|
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.
|
|
535
349
|
@objc(pushSubscribe:profileFields:customFields:cats:replace:skipTriggers:)
|
|
536
350
|
public func pushSubscribe(
|
|
537
351
|
_ sync: NSNumber?,
|
|
@@ -541,26 +355,29 @@ public final class SdkModule: NSObject {
|
|
|
541
355
|
replace: NSNumber?,
|
|
542
356
|
skipTriggers: NSNumber?
|
|
543
357
|
) {
|
|
544
|
-
ensureProvidersInstalled()
|
|
545
|
-
|
|
546
358
|
let s = sync?.boolValue ?? true
|
|
547
359
|
let r: Bool? = (replace != nil) ? replace!.boolValue : nil
|
|
548
360
|
let st: Bool? = (skipTriggers != nil) ? skipTriggers!.boolValue : nil
|
|
549
361
|
|
|
550
362
|
AltcraftSDK.shared.pushSubscriptionFunctions.pushSubscribe(
|
|
551
363
|
sync: s,
|
|
552
|
-
profileFields: toAnyDict(profileFields),
|
|
553
|
-
customFields: toAnyDict(customFields),
|
|
554
|
-
cats: toCats(cats),
|
|
364
|
+
profileFields: Converter.toAnyDict(profileFields),
|
|
365
|
+
customFields: Converter.toAnyDict(customFields),
|
|
366
|
+
cats: Converter.toCats(cats),
|
|
555
367
|
replace: r,
|
|
556
368
|
skipTriggers: st
|
|
557
369
|
)
|
|
558
370
|
}
|
|
559
371
|
|
|
560
|
-
///
|
|
372
|
+
/// Sends a push suspend request (fire-and-forget).
|
|
561
373
|
///
|
|
562
|
-
///
|
|
563
|
-
/// `
|
|
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.
|
|
564
381
|
@objc(pushSuspend:profileFields:customFields:cats:replace:skipTriggers:)
|
|
565
382
|
public func pushSuspend(
|
|
566
383
|
_ sync: NSNumber?,
|
|
@@ -570,26 +387,29 @@ public final class SdkModule: NSObject {
|
|
|
570
387
|
replace: NSNumber?,
|
|
571
388
|
skipTriggers: NSNumber?
|
|
572
389
|
) {
|
|
573
|
-
ensureProvidersInstalled()
|
|
574
|
-
|
|
575
390
|
let s = sync?.boolValue ?? true
|
|
576
391
|
let r: Bool? = (replace != nil) ? replace!.boolValue : nil
|
|
577
392
|
let st: Bool? = (skipTriggers != nil) ? skipTriggers!.boolValue : nil
|
|
578
393
|
|
|
579
394
|
AltcraftSDK.shared.pushSubscriptionFunctions.pushSuspend(
|
|
580
395
|
sync: s,
|
|
581
|
-
profileFields: toAnyDict(profileFields),
|
|
582
|
-
customFields: toAnyDict(customFields),
|
|
583
|
-
cats: toCats(cats),
|
|
396
|
+
profileFields: Converter.toAnyDict(profileFields),
|
|
397
|
+
customFields: Converter.toAnyDict(customFields),
|
|
398
|
+
cats: Converter.toCats(cats),
|
|
584
399
|
replace: r,
|
|
585
400
|
skipTriggers: st
|
|
586
401
|
)
|
|
587
402
|
}
|
|
588
403
|
|
|
589
|
-
///
|
|
404
|
+
/// Sends a push unsubscribe request (fire-and-forget).
|
|
590
405
|
///
|
|
591
|
-
///
|
|
592
|
-
/// `
|
|
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.
|
|
593
413
|
@objc(pushUnSubscribe:profileFields:customFields:cats:replace:skipTriggers:)
|
|
594
414
|
public func pushUnSubscribe(
|
|
595
415
|
_ sync: NSNumber?,
|
|
@@ -599,29 +419,35 @@ public final class SdkModule: NSObject {
|
|
|
599
419
|
replace: NSNumber?,
|
|
600
420
|
skipTriggers: NSNumber?
|
|
601
421
|
) {
|
|
602
|
-
ensureProvidersInstalled()
|
|
603
|
-
|
|
604
422
|
let s = sync?.boolValue ?? true
|
|
605
423
|
let r: Bool? = (replace != nil) ? replace!.boolValue : nil
|
|
606
424
|
let st: Bool? = (skipTriggers != nil) ? skipTriggers!.boolValue : nil
|
|
607
425
|
|
|
608
426
|
AltcraftSDK.shared.pushSubscriptionFunctions.pushUnSubscribe(
|
|
609
427
|
sync: s,
|
|
610
|
-
profileFields: toAnyDict(profileFields),
|
|
611
|
-
customFields: toAnyDict(customFields),
|
|
612
|
-
cats: toCats(cats),
|
|
428
|
+
profileFields: Converter.toAnyDict(profileFields),
|
|
429
|
+
customFields: Converter.toAnyDict(customFields),
|
|
430
|
+
cats: Converter.toCats(cats),
|
|
613
431
|
replace: r,
|
|
614
432
|
skipTriggers: st
|
|
615
433
|
)
|
|
616
434
|
}
|
|
617
435
|
|
|
618
|
-
// MARK: - Mobile Events
|
|
436
|
+
// MARK: - Mobile Events bridge
|
|
619
437
|
|
|
620
|
-
/// Sends a mobile event to
|
|
438
|
+
/// Sends a mobile event to the server (fire-and-forget).
|
|
621
439
|
///
|
|
622
|
-
///
|
|
623
|
-
///
|
|
624
|
-
|
|
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:)
|
|
625
451
|
public func mobileEvent(
|
|
626
452
|
_ sid: String,
|
|
627
453
|
eventName: String,
|
|
@@ -630,153 +456,90 @@ public final class SdkModule: NSObject {
|
|
|
630
456
|
matching: NSDictionary?,
|
|
631
457
|
matchingType: String?,
|
|
632
458
|
profileFields: NSDictionary?,
|
|
459
|
+
subscription: NSDictionary?,
|
|
633
460
|
utm: NSDictionary?
|
|
634
461
|
) {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
let payloadNorm = toAnyDict(payload)
|
|
638
|
-
let matchingNorm = toAnyDict(matching)
|
|
639
|
-
let profileNorm = toAnyDict(profileFields)
|
|
640
|
-
let utmNorm = Converter.toAnyDict(utm)
|
|
641
|
-
|
|
642
|
-
let utmObj: UTM? = utmNorm.flatMap {
|
|
643
|
-
UTM(
|
|
644
|
-
campaign: $0["campaign"] as? String,
|
|
645
|
-
content: $0["content"] as? String,
|
|
646
|
-
keyword: $0["keyword"] as? String,
|
|
647
|
-
medium: $0["medium"] as? String,
|
|
648
|
-
source: $0["source"] as? String,
|
|
649
|
-
temp: $0["temp"] as? String
|
|
650
|
-
)
|
|
651
|
-
}
|
|
462
|
+
let utmObj = Converter.toUTM(utm)
|
|
463
|
+
let subscriptionObj = Converter.toSubscription(subscription)
|
|
652
464
|
|
|
653
465
|
AltcraftSDK.shared.mobileEventFunctions.mobileEvent(
|
|
654
466
|
sid: sid,
|
|
655
467
|
altcraftClientID: "",
|
|
656
468
|
eventName: eventName,
|
|
657
469
|
sendMessageId: sendMessageId,
|
|
658
|
-
payload:
|
|
659
|
-
matching:
|
|
470
|
+
payload: Converter.toAnyDict(payload),
|
|
471
|
+
matching: Converter.toAnyDict(matching),
|
|
660
472
|
matchingType: matchingType,
|
|
661
|
-
profileFields:
|
|
662
|
-
subscription:
|
|
473
|
+
profileFields: Converter.toAnyDict(profileFields),
|
|
474
|
+
subscription: subscriptionObj,
|
|
663
475
|
utm: utmObj
|
|
664
476
|
)
|
|
665
477
|
}
|
|
666
478
|
|
|
667
479
|
// MARK: - Promise API: PushSubscription status/results
|
|
668
480
|
|
|
669
|
-
/// Converts `nil` to `NSNull()` for JS payloads.
|
|
670
481
|
private func nsNull(_ v: Any?) -> Any { v ?? NSNull() }
|
|
671
482
|
|
|
672
|
-
///
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
for (k, v) in dict {
|
|
678
|
-
guard let v else { continue }
|
|
679
|
-
out[k] = String(describing: v)
|
|
680
|
-
}
|
|
681
|
-
return out.isEmpty ? nil : out
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
private func mapCategory(_ c: CategoryData) -> [String: Any] {
|
|
685
|
-
[
|
|
686
|
-
"name": nsNull(c.name),
|
|
687
|
-
"title": nsNull(c.title),
|
|
688
|
-
"steady": nsNull(c.steady),
|
|
689
|
-
"active": nsNull(c.active),
|
|
690
|
-
]
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
private func mapSubscription(_ s: SubscriptionData) -> [String: Any] {
|
|
694
|
-
let fieldsString = toStringMap(s.fields)
|
|
695
|
-
let catsArr: [[String: Any]]? = s.cats?.map { mapCategory($0) }
|
|
696
|
-
|
|
697
|
-
return [
|
|
698
|
-
"subscriptionId": nsNull(s.subscriptionId),
|
|
699
|
-
"hashId": nsNull(s.hashId),
|
|
700
|
-
"provider": nsNull(s.provider),
|
|
701
|
-
"status": nsNull(s.status),
|
|
702
|
-
"fields": nsNull(fieldsString),
|
|
703
|
-
"cats": nsNull(catsArr),
|
|
704
|
-
]
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
private func mapProfile(_ p: ProfileData) -> [String: Any] {
|
|
708
|
-
let subDict: [String: Any]? = p.subscription.map { mapSubscription($0) }
|
|
709
|
-
return [
|
|
710
|
-
"id": nsNull(p.id),
|
|
711
|
-
"status": nsNull(p.status),
|
|
712
|
-
"isTest": nsNull(p.isTest),
|
|
713
|
-
"subscription": nsNull(subDict),
|
|
714
|
-
]
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
private func mapResponse(_ r: Response) -> [String: Any] {
|
|
718
|
-
let profileDict: [String: Any]? = r.profile.map { mapProfile($0) }
|
|
719
|
-
return [
|
|
720
|
-
"error": nsNull(r.error),
|
|
721
|
-
"errorText": nsNull(r.errorText),
|
|
722
|
-
"profile": nsNull(profileDict),
|
|
723
|
-
]
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
private func mapResponseWithHttp(_ r: ResponseWithHttp) -> [String: Any] {
|
|
727
|
-
let respDict: [String: Any]? = r.response.map { mapResponse($0) }
|
|
728
|
-
return [
|
|
729
|
-
"httpCode": nsNull(r.httpCode),
|
|
730
|
-
"response": nsNull(respDict),
|
|
731
|
-
]
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
/// Attempts to un-suspend a push subscription.
|
|
483
|
+
/// Unsuspends push subscriptions based on native matching rules.
|
|
484
|
+
///
|
|
485
|
+
/// - Parameters:
|
|
486
|
+
/// - resolve: Promise resolver called with a mapped `{ httpCode, response }` or `null`.
|
|
487
|
+
/// - reject: Promise rejecter (unused; kept for RN signature compatibility).
|
|
735
488
|
@objc(unSuspendPushSubscriptionWithResolver:rejecter:)
|
|
736
489
|
public func unSuspendPushSubscription(
|
|
737
490
|
resolver resolve: @escaping RCTPromiseResolveBlock,
|
|
738
491
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
739
492
|
) {
|
|
740
493
|
_ = reject
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
AltcraftSDK.shared.pushSubscriptionFunctions.unSuspendPushSubscription { result in
|
|
494
|
+
AltcraftSDK.shared.pushSubscriptionFunctions.unSuspendPushSubscription { [weak self] result in
|
|
495
|
+
guard let self else { resolve(NSNull()); return }
|
|
744
496
|
guard let result else { resolve(NSNull()); return }
|
|
745
|
-
resolve(
|
|
497
|
+
resolve(mapResponseWithHttp(result))
|
|
746
498
|
}
|
|
747
499
|
}
|
|
748
500
|
|
|
749
|
-
/// Returns status of the latest subscription
|
|
501
|
+
/// Returns the status of the latest subscription in profile.
|
|
502
|
+
///
|
|
503
|
+
/// - Parameters:
|
|
504
|
+
/// - resolve: Promise resolver called with a mapped `{ httpCode, response }` or `null`.
|
|
505
|
+
/// - reject: Promise rejecter (unused; kept for RN signature compatibility).
|
|
750
506
|
@objc(getStatusOfLatestSubscriptionWithResolver:rejecter:)
|
|
751
507
|
public func getStatusOfLatestSubscription(
|
|
752
508
|
resolver resolve: @escaping RCTPromiseResolveBlock,
|
|
753
509
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
754
510
|
) {
|
|
755
511
|
_ = reject
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
AltcraftSDK.shared.pushSubscriptionFunctions.getStatusOfLatestSubscription { result in
|
|
512
|
+
AltcraftSDK.shared.pushSubscriptionFunctions.getStatusOfLatestSubscription { [weak self] result in
|
|
513
|
+
guard let self else { resolve(NSNull()); return }
|
|
759
514
|
guard let result else { resolve(NSNull()); return }
|
|
760
|
-
resolve(
|
|
515
|
+
resolve(mapResponseWithHttp(result))
|
|
761
516
|
}
|
|
762
517
|
}
|
|
763
518
|
|
|
764
|
-
/// Returns status
|
|
519
|
+
/// Returns the status for current subscription (by current provider/token context).
|
|
520
|
+
///
|
|
521
|
+
/// - Parameters:
|
|
522
|
+
/// - resolve: Promise resolver called with a mapped `{ httpCode, response }` or `null`.
|
|
523
|
+
/// - reject: Promise rejecter (unused; kept for RN signature compatibility).
|
|
765
524
|
@objc(getStatusForCurrentSubscriptionWithResolver:rejecter:)
|
|
766
525
|
public func getStatusForCurrentSubscription(
|
|
767
526
|
resolver resolve: @escaping RCTPromiseResolveBlock,
|
|
768
527
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
769
528
|
) {
|
|
770
529
|
_ = reject
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
AltcraftSDK.shared.pushSubscriptionFunctions.getStatusForCurrentSubscription { result in
|
|
530
|
+
AltcraftSDK.shared.pushSubscriptionFunctions.getStatusForCurrentSubscription { [weak self] result in
|
|
531
|
+
guard let self else { resolve(NSNull()); return }
|
|
774
532
|
guard let result else { resolve(NSNull()); return }
|
|
775
|
-
resolve(
|
|
533
|
+
resolve(mapResponseWithHttp(result))
|
|
776
534
|
}
|
|
777
535
|
}
|
|
778
536
|
|
|
779
|
-
/// Returns status of the latest subscription
|
|
537
|
+
/// Returns the status of the latest subscription for the given provider.
|
|
538
|
+
///
|
|
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).
|
|
780
543
|
@objc(getStatusOfLatestSubscriptionForProviderWithProvider:resolver:rejecter:)
|
|
781
544
|
public func getStatusOfLatestSubscriptionForProvider(
|
|
782
545
|
provider: String?,
|
|
@@ -784,22 +547,101 @@ public final class SdkModule: NSObject {
|
|
|
784
547
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
785
548
|
) {
|
|
786
549
|
_ = reject
|
|
787
|
-
|
|
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
|
+
}
|
|
788
559
|
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
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
|
+
]
|
|
570
|
+
}
|
|
571
|
+
|
|
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
|
+
}
|
|
584
|
+
|
|
585
|
+
/// Maps a native `ProfileData` into a JS-friendly dictionary.
|
|
586
|
+
///
|
|
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
|
+
]
|
|
793
627
|
}
|
|
794
628
|
|
|
795
629
|
// MARK: - UserDefaults
|
|
796
630
|
|
|
797
|
-
/// Stores a value
|
|
631
|
+
/// Stores a key-value pair in persistent storage.
|
|
632
|
+
///
|
|
633
|
+
/// Platform behavior:
|
|
634
|
+
/// - iOS: Uses `UserDefaults` (optionally scoped by `suiteName` / App Group).
|
|
798
635
|
///
|
|
799
|
-
///
|
|
800
|
-
/// -
|
|
801
|
-
/// -
|
|
802
|
-
/// -
|
|
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.
|
|
803
645
|
@objc(setUserDefaultsValueWithSuiteName:key:value:)
|
|
804
646
|
public func setUserDefaultsValue(
|
|
805
647
|
suiteName: String?,
|
|
@@ -826,4 +668,4 @@ public final class SdkModule: NSObject {
|
|
|
826
668
|
ud.removeObject(forKey: k)
|
|
827
669
|
}
|
|
828
670
|
}
|
|
829
|
-
}
|
|
671
|
+
}
|