@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.
Files changed (33) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +5 -2
  3. package/android/build.gradle +2 -3
  4. package/android/src/main/java/com/altcraft/sdk/SdkModule.kt +551 -478
  5. package/android/src/main/java/com/altcraft/sdk/utilities/Converter.kt +164 -0
  6. package/android/src/main/java/com/altcraft/sdk/utilities/Extension.kt +72 -0
  7. package/ios/Converter.swift +483 -46
  8. package/ios/Sdk.mm +83 -271
  9. package/ios/SdkModule.swift +335 -493
  10. package/lib/module/NativeSdk.js +35 -0
  11. package/lib/module/NativeSdk.js.map +1 -1
  12. package/lib/module/Utilities.js +94 -0
  13. package/lib/module/Utilities.js.map +1 -0
  14. package/lib/module/index.js +224 -246
  15. package/lib/module/index.js.map +1 -1
  16. package/lib/typescript/src/NativeSdk.d.ts +107 -31
  17. package/lib/typescript/src/NativeSdk.d.ts.map +1 -1
  18. package/lib/typescript/src/Utilities.d.ts +15 -0
  19. package/lib/typescript/src/Utilities.d.ts.map +1 -0
  20. package/lib/typescript/src/index.d.ts +162 -94
  21. package/lib/typescript/src/index.d.ts.map +1 -1
  22. package/package.json +2 -2
  23. package/src/NativeSdk.ts +135 -44
  24. package/src/Utilities.ts +116 -0
  25. package/src/index.tsx +281 -311
  26. package/android/src/main/java/com/altcraft/sdk/interfaces/RnFCMProvider.kt +0 -26
  27. package/android/src/main/java/com/altcraft/sdk/interfaces/RnHMSProvider.kt +0 -32
  28. package/android/src/main/java/com/altcraft/sdk/interfaces/RnJWTProvider.kt +0 -21
  29. package/android/src/main/java/com/altcraft/sdk/interfaces/RnRustoreProvider.kt +0 -53
  30. package/ios/protocols/RNAPNSProvider.swift +0 -15
  31. package/ios/protocols/RNFCMProvider.swift +0 -24
  32. package/ios/protocols/RNHMSProvider.swift +0 -24
  33. package/ios/protocols/RNJWTProvider.swift +0 -15
@@ -1,5 +1,9 @@
1
1
  package com.altcraft.sdk
2
2
 
3
+ // Created by Andrey Pogodin.
4
+ //
5
+ // Copyright © 2025 Altcraft. All rights reserved.
6
+
3
7
  import androidx.activity.ComponentActivity
4
8
  import com.altcraft.sdk.config.AltcraftConfiguration
5
9
  import com.altcraft.sdk.data.DataClasses
@@ -7,10 +11,16 @@ import com.altcraft.sdk.mob_events.PublicMobileEventFunction
7
11
  import com.altcraft.sdk.push.events.PublicPushEventFunctions
8
12
  import com.altcraft.sdk.push.subscribe.PublicPushSubscriptionFunctions
9
13
  import com.altcraft.sdk.push.token.PublicPushTokenFunctions
10
- import com.altcraft.sdk.rn.providers.RnFCMProvider
11
- import com.altcraft.sdk.rn.providers.RnHMSProvider
12
- import com.altcraft.sdk.rn.providers.RnJWTProvider
13
- import com.altcraft.sdk.rn.providers.RnRuStoreProvider
14
+ import com.altcraft.sdk.rn.getArrayOrNull
15
+ import com.altcraft.sdk.rn.getBooleanOrNull
16
+ import com.altcraft.sdk.rn.getIntOrNull
17
+ import com.altcraft.sdk.rn.getMapOrNull
18
+ import com.altcraft.sdk.rn.getStringOrNull
19
+ import com.altcraft.sdk.rn.hasNonNullKey
20
+ import com.altcraft.sdk.rn.putNullableBoolean
21
+ import com.altcraft.sdk.rn.putNullableInt
22
+ import com.altcraft.sdk.rn.putNullableString
23
+ import com.altcraft.sdk.rn.toStringList
14
24
  import com.altcraft.sdk.sdk_events.Events
15
25
  import com.altcraft.sdk.utilities.Converter
16
26
  import com.facebook.react.bridge.Arguments
@@ -27,93 +37,121 @@ import kotlinx.coroutines.Dispatchers
27
37
  import kotlinx.coroutines.SupervisorJob
28
38
  import kotlinx.coroutines.launch
29
39
 
30
- @ReactModule(name = SdkModule.NAME)
31
- class SdkModule(
32
- reactContext: ReactApplicationContext
33
- ) : NativeSdkSpec(reactContext) {
34
-
35
- private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
36
-
37
- override fun getName(): String = NAME
38
-
39
- // ---------------------- init / auth / providers ----------------------
40
-
41
- override fun initialize(config: ReadableMap, promise: Promise) {
42
- AltcraftRnInitializer.initialize(
43
- reactApplicationContext,
44
- config,
45
- promise
46
- )
47
- }
48
-
49
- override fun setJwt(token: String?) {
50
- if (token.isNullOrBlank()) {
51
- RnJWTProvider.clear()
52
- AltcraftSDK.setJWTProvider(null)
53
- } else {
54
- RnJWTProvider.setToken(token)
55
- AltcraftSDK.setJWTProvider(RnJWTProvider)
56
- }
57
- }
58
-
59
- override fun setAndroidFcmToken(token: String?) {
60
- if (token.isNullOrBlank()) {
61
- RnFCMProvider.clear()
62
- PublicPushTokenFunctions.setFCMTokenProvider(null)
63
- } else {
64
- RnFCMProvider.setToken(token)
65
- PublicPushTokenFunctions.setFCMTokenProvider(RnFCMProvider)
66
- }
67
- }
68
-
69
- override fun setIosFcmToken(token: String?) {
70
- // no-op on Android
71
- }
72
-
73
- override fun setAndroidHmsToken(token: String?) {
74
- if (token.isNullOrBlank()) {
75
- RnHMSProvider.clear()
76
- PublicPushTokenFunctions.setHMSTokenProvider(null)
77
- } else {
78
- RnHMSProvider.setToken(token)
79
- PublicPushTokenFunctions.setHMSTokenProvider(RnHMSProvider)
80
- }
81
- }
82
-
83
- override fun setIosHmsToken(token: String?) {
84
- // no-op on Android
85
- }
86
-
87
- override fun setApnsToken(token: String?) {
88
- // no-op on Android
89
- }
40
+ // ---------------------- Constants ----------------------
41
+
42
+ private object RnConstants {
43
+ // Module
44
+ const val MODULE_NAME: String = "Sdk"
45
+
46
+ // JS event emitter
47
+ const val JS_EVENT_NAME: String = "AltcraftSdkEvent"
48
+
49
+ // Promise reject codes
50
+ const val ERR_INIT_INVALID_CONFIG: String = "ALTCRAFT_INIT_INVALID_CONFIG"
51
+ const val ERR_INIT_ERROR: String = "ALTCRAFT_INIT_ERROR"
52
+ const val ERR_GET_PUSH_TOKEN: String = "getPushToken"
53
+ const val ERR_SET_PUSH_TOKEN: String = "setPushToken"
54
+ const val ERR_CLEAR: String = "clear"
55
+ const val ERR_UNSUSPEND: String = "unSuspendPushSubscription"
56
+ const val ERR_GET_LATEST_STATUS: String = "getStatusOfLatestSubscription"
57
+ const val ERR_GET_CURRENT_STATUS: String = "getStatusForCurrentSubscription"
58
+ const val ERR_GET_LATEST_STATUS_PROVIDER: String = "getStatusOfLatestSubscriptionForProvider"
59
+
60
+ // Promise reject messages
61
+ const val MSG_PROVIDER_IS_BLANK: String = "provider is blank"
62
+ const val MSG_INIT_UNKNOWN: String = "Altcraft initialization failed with unknown error"
63
+ const val MSG_INIT_ERROR: String = "Altcraft initialization error"
64
+
65
+ // Keys: TokenData map
66
+ const val KEY_PROVIDER: String = "provider"
67
+ const val KEY_TOKEN: String = "token"
68
+
69
+ // Keys: SDK event map
70
+ const val KEY_FUNCTION: String = "function"
71
+ const val KEY_CODE: String = "code"
72
+ const val KEY_MESSAGE: String = "message"
73
+ const val KEY_TYPE: String = "type"
74
+ const val KEY_VALUE: String = "value"
75
+
76
+ // Event types for JS
77
+ const val TYPE_EVENT: String = "event"
78
+ const val TYPE_ERROR: String = "error"
79
+ const val TYPE_RETRY_ERROR: String = "retryError"
80
+
81
+ // Keys: ResponseWithHttpCode -> JS map
82
+ const val KEY_HTTP_CODE: String = "httpCode"
83
+ const val KEY_RESPONSE: String = "response"
84
+ const val KEY_ERROR: String = "error"
85
+ const val KEY_ERROR_TEXT: String = "errorText"
86
+ const val KEY_PROFILE: String = "profile"
87
+ const val KEY_ID: String = "id"
88
+ const val KEY_STATUS: String = "status"
89
+ const val KEY_IS_TEST: String = "isTest"
90
+ const val KEY_SUBSCRIPTION: String = "subscription"
91
+ const val KEY_SUBSCRIPTION_ID: String = "subscriptionId"
92
+ const val KEY_HASH_ID: String = "hashId"
93
+
94
+ // Fields not mapped yet (kept for parity with TS)
95
+ const val KEY_FIELDS: String = "fields"
96
+ const val KEY_CATS: String = "cats"
97
+ }
90
98
 
91
- override fun setUserDefaultsValue(suiteName: String?, key: String, value: String?) {
92
- // no-op on Android
93
- }
99
+ @ReactModule(name = RnConstants.MODULE_NAME)
100
+ class SdkModule(reactContext: ReactApplicationContext) : NativeSdkSpec(reactContext) {
94
101
 
95
- override fun setRustoreToken(token: String?) {
96
- if (token.isNullOrBlank()) {
97
- RnRuStoreProvider.clear()
98
- PublicPushTokenFunctions.setRuStoreTokenProvider(null)
99
- } else {
100
- RnRuStoreProvider.setToken(token)
101
- PublicPushTokenFunctions.setRuStoreTokenProvider(RnRuStoreProvider)
102
- }
103
- }
102
+ private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
104
103
 
105
- // ---------------------- required for NativeEventEmitter ----------------------
104
+ override fun getName(): String = RnConstants.MODULE_NAME
106
105
 
106
+ /**
107
+ * Required by NativeEventEmitter (no-op on Android).
108
+ *
109
+ * @param eventName Event name from JS side.
110
+ */
107
111
  override fun addListener(eventName: String) {
108
112
  // no-op
109
113
  }
110
114
 
115
+ /**
116
+ * Required by NativeEventEmitter (no-op on Android).
117
+ *
118
+ * @param count Number of JS listeners removed.
119
+ */
111
120
  override fun removeListeners(count: Double) {
112
121
  // no-op
113
122
  }
114
123
 
115
- // ---------------------- token-API for RN ----------------------
124
+ // ---------------------- init ----------------------
125
+
126
+ /**
127
+ * Initializes the native Altcraft SDK using RN config map.
128
+ *
129
+ * @param config React Native configuration object.
130
+ * ```
131
+ * Required: `apiUrl`.
132
+ * Optional: `rToken`, `appInfo`, `providerPriorityList`, `enableLogging`,
133
+ * Android-only: `android_icon`, `android_usingService`, `android_serviceMessage`,
134
+ * `android_pushReceiverModules`, `android_pushChannelName`, `android_pushChannelDescription`.
135
+ * @param promise
136
+ * ```
137
+ * Promise resolved on success; rejected on invalid config or initialization failure.
138
+ */
139
+ override fun initialize(config: ReadableMap, promise: Promise) {
140
+ AltcraftRnInitializer.initialize(
141
+ reactContext = reactApplicationContext,
142
+ config = config,
143
+ promise = promise
144
+ )
145
+ }
116
146
 
147
+ // ---------------------- token-API for RN (ONLY getPushToken / setPushToken)
148
+ // ----------------------
149
+
150
+ /**
151
+ * Returns current push token data from native side.
152
+ *
153
+ * @param promise Resolves to `{ provider, token }` or `null` if token is not available.
154
+ */
117
155
  override fun getPushToken(promise: Promise) {
118
156
  coroutineScope.launch {
119
157
  try {
@@ -122,165 +160,143 @@ class SdkModule(
122
160
  promise.resolve(null)
123
161
  } else {
124
162
  val map = Arguments.createMap()
125
- map.putString("provider", tokenData.provider)
126
- map.putString("token", tokenData.token)
163
+ map.putString(RnConstants.KEY_PROVIDER, tokenData.provider)
164
+ map.putString(RnConstants.KEY_TOKEN, tokenData.token)
127
165
  promise.resolve(map)
128
166
  }
129
167
  } catch (e: Exception) {
130
- promise.reject("getPushToken", e)
131
- }
132
- }
133
- }
134
-
135
- override fun deleteDeviceToken(provider: String?, promise: Promise) {
136
- val p: String = provider ?: run {
137
- promise.reject("deleteDeviceToken", "provider is null")
138
- return
139
- }
140
-
141
- coroutineScope.launch {
142
- try {
143
- PublicPushTokenFunctions.deleteDeviceToken(reactApplicationContext, p)
144
- promise.resolve(null)
145
- } catch (e: Exception) {
146
- promise.reject("deleteDeviceToken", e)
147
- }
148
- }
149
- }
150
-
151
- override fun forcedTokenUpdate(promise: Promise) {
152
- coroutineScope.launch {
153
- try {
154
- PublicPushTokenFunctions.forcedTokenUpdate(reactApplicationContext) {
155
- promise.resolve(null)
156
- }
157
- } catch (e: Exception) {
158
- promise.reject("forcedTokenUpdate", e)
159
- }
160
- }
161
- }
162
-
163
- override fun changePushProviderPriorityList(priorityList: ReadableArray?, promise: Promise) {
164
- val list = Converter.toStringList(priorityList)
165
- coroutineScope.launch {
166
- try {
167
- PublicPushTokenFunctions.changePushProviderPriorityList(
168
- context = reactApplicationContext,
169
- priorityList = list
170
- )
171
- promise.resolve(null)
172
- } catch (e: Exception) {
173
- promise.reject("changePushProviderPriorityList", e)
168
+ promise.reject(RnConstants.ERR_GET_PUSH_TOKEN, e)
174
169
  }
175
170
  }
176
171
  }
177
172
 
173
+ /**
174
+ * Sets or clears push token for the given provider.
175
+ *
176
+ * @param provider Provider identifier (must be non-blank).
177
+ * @param token Token string or `null` to clear (native expects String; `null` is converted to
178
+ * empty string).
179
+ * @param promise Resolves when the token is saved on native side.
180
+ */
178
181
  override fun setPushToken(provider: String, token: String?, promise: Promise) {
179
182
  if (provider.isBlank()) {
180
- promise.reject("setPushToken", "provider is blank")
183
+ promise.reject(RnConstants.ERR_SET_PUSH_TOKEN, RnConstants.MSG_PROVIDER_IS_BLANK)
181
184
  return
182
185
  }
183
186
 
184
187
  coroutineScope.launch {
185
188
  try {
186
- if (token == null) {
187
- PublicPushTokenFunctions.deleteDeviceToken(
188
- context = reactApplicationContext,
189
- provider = provider
190
- )
191
- promise.resolve(null)
192
- return@launch
193
- }
189
+ val tokenToPass: String = token ?: ""
194
190
 
195
191
  PublicPushTokenFunctions.setPushToken(
196
- context = reactApplicationContext,
197
- provider = provider,
198
- token = token
192
+ context = reactApplicationContext,
193
+ provider = provider,
194
+ token = tokenToPass
199
195
  )
200
196
  promise.resolve(null)
201
197
  } catch (e: Exception) {
202
- promise.reject("setPushToken", e)
198
+ promise.reject(RnConstants.ERR_SET_PUSH_TOKEN, e)
203
199
  }
204
200
  }
205
201
  }
206
202
 
207
- // ---------------------- AltcraftSDK: clear / initial ops / permission ----------------------
203
+ // ---------------------- AltcraftSDK: clear / permission ----------------------
208
204
 
205
+ /**
206
+ * Clears SDK local state and stored data (native).
207
+ *
208
+ * @param promise Resolves after cleanup is finished.
209
+ */
209
210
  override fun clear(promise: Promise) {
210
211
  try {
211
- AltcraftSDK.clear(reactApplicationContext) {
212
- promise.resolve(null)
213
- }
212
+ AltcraftSDK.clear(reactApplicationContext) { promise.resolve(null) }
214
213
  } catch (e: Exception) {
215
- promise.reject("clear", e)
216
- }
217
- }
218
-
219
- override fun unlockInitOperationsInThisSession() {
220
- try {
221
- AltcraftSDK.unlockInitOperationsInThisSession()
222
- } catch (_: Exception) {
214
+ promise.reject(RnConstants.ERR_CLEAR, e)
223
215
  }
224
216
  }
225
217
 
218
+ /**
219
+ * Requests notification permission (Android-only runtime behavior). Safe no-op if current
220
+ * Activity is not a ComponentActivity.
221
+ */
226
222
  override fun requestNotificationPermission() {
227
223
  val activity = currentActivity
228
- if (activity !is ComponentActivity) {
229
- return
230
- }
224
+ if (activity !is ComponentActivity) return
225
+
231
226
  UiThreadUtil.runOnUiThread {
232
227
  try {
233
- AltcraftSDK.requestNotificationPermission(
234
- reactApplicationContext,
235
- activity
236
- )
228
+ AltcraftSDK.requestNotificationPermission(reactApplicationContext, activity)
237
229
  } catch (_: Exception) {
230
+ // ignore
238
231
  }
239
232
  }
240
233
  }
241
234
 
242
235
  // ---------------------- MobileEvent bridge ----------------------
243
236
 
237
+ /**
238
+ * Sends a mobile event to the server (fire-and-forget).
239
+ *
240
+ * @param sid Pixel identifier.
241
+ * @param eventName Event name.
242
+ * @param sendMessageId Optional message identifier to link the event.
243
+ * @param payload Optional event payload (string-only map).
244
+ * @param matching Optional matching parameters (string-only map).
245
+ * @param matchingType Optional matching mode/type.
246
+ * @param profileFields Optional profile fields (string-only map).
247
+ * @param subscription Optional subscription info (email/sms/push/cc_data), converted on native
248
+ * side.
249
+ * @param utm Optional UTM tags for attribution.
250
+ */
244
251
  override fun mobileEvent(
245
- sid: String,
246
- eventName: String,
247
- sendMessageId: String?,
248
- payload: ReadableMap?,
249
- matching: ReadableMap?,
250
- matchingType: String?,
251
- profileFields: ReadableMap?,
252
- utm: ReadableMap?
252
+ sid: String,
253
+ eventName: String,
254
+ sendMessageId: String?,
255
+ payload: ReadableMap?,
256
+ matching: ReadableMap?,
257
+ matchingType: String?,
258
+ profileFields: ReadableMap?,
259
+ subscription: ReadableMap?,
260
+ utm: ReadableMap?
253
261
  ) {
254
262
  try {
255
- val utmData = utm?.let {
256
- DataClasses.UTM(
257
- campaign = it.getString("campaign"),
258
- content = it.getString("content"),
259
- keyword = it.getString("keyword"),
260
- medium = it.getString("medium"),
261
- source = it.getString("source"),
262
- temp = it.getString("temp")
263
- )
264
- }
263
+ val utmData =
264
+ utm?.let {
265
+ DataClasses.UTM(
266
+ campaign = it.getString("campaign"),
267
+ content = it.getString("content"),
268
+ keyword = it.getString("keyword"),
269
+ medium = it.getString("medium"),
270
+ source = it.getString("source"),
271
+ temp = it.getString("temp")
272
+ )
273
+ }
265
274
 
266
275
  PublicMobileEventFunction.mobileEvent(
267
- context = reactApplicationContext,
268
- sid = sid,
269
- eventName = eventName,
270
- sendMessageId = sendMessageId,
271
- payload = Converter.toKotlinMap(payload),
272
- matching = Converter.toKotlinMap(matching),
273
- matchingType = matchingType,
274
- profileFields = Converter.toKotlinMap(profileFields),
275
- subscription = null,
276
- utm = utmData
276
+ context = reactApplicationContext,
277
+ sid = sid,
278
+ eventName = eventName,
279
+ sendMessageId = sendMessageId,
280
+ payload = Converter.toKotlinMap(payload),
281
+ matching = Converter.toKotlinMap(matching),
282
+ matchingType = matchingType,
283
+ profileFields = Converter.toKotlinMap(profileFields),
284
+ subscription = Converter.toSubscriptionOrNull(subscription),
285
+ utm = utmData
277
286
  )
278
- } catch (_: Exception) {
287
+ } catch (e: Exception) {
288
+ // Log error but don't crash bridge
289
+ e.printStackTrace()
279
290
  }
280
291
  }
281
292
 
282
293
  // ---------------------- push payload bridge ----------------------
283
294
 
295
+ /**
296
+ * Forwards a push payload to native SDK (Android-only behavior).
297
+ *
298
+ * @param message Push payload map (string-only).
299
+ */
284
300
  override fun takePush(message: ReadableMap?) {
285
301
  if (message == null) return
286
302
  val map = Converter.toStringMapOrNull(message) ?: return
@@ -289,53 +305,72 @@ class SdkModule(
289
305
  }
290
306
  }
291
307
 
308
+ // ---------------------- manual push events ----------------------
309
+
310
+ /**
311
+ * Reports a push delivery event (Android-only behavior).
312
+ *
313
+ * @param message Optional push payload (string-only map).
314
+ * @param messageUID Optional Altcraft message UID.
315
+ */
292
316
  override fun deliveryEvent(message: ReadableMap?, messageUID: String?) {
293
317
  try {
294
318
  val map = Converter.toStringMapOrNull(message)
295
319
  PublicPushEventFunctions.deliveryEvent(
296
- context = reactApplicationContext,
297
- message = map,
298
- messageUID = messageUID
320
+ context = reactApplicationContext,
321
+ message = map,
322
+ messageUID = messageUID
299
323
  )
300
324
  } catch (_: Exception) {
325
+ // ignore
301
326
  }
302
327
  }
303
328
 
329
+ /**
330
+ * Reports a push open event (Android-only behavior).
331
+ *
332
+ * @param message Optional push payload (string-only map).
333
+ * @param messageUID Optional Altcraft message UID.
334
+ */
304
335
  override fun openEvent(message: ReadableMap?, messageUID: String?) {
305
336
  try {
306
337
  val map = Converter.toStringMapOrNull(message)
307
338
  PublicPushEventFunctions.openEvent(
308
- context = reactApplicationContext,
309
- message = map,
310
- messageUID = messageUID
339
+ context = reactApplicationContext,
340
+ message = map,
341
+ messageUID = messageUID
311
342
  )
312
343
  } catch (_: Exception) {
344
+ // ignore
313
345
  }
314
346
  }
315
347
 
316
348
  // --------------------- Events bridge for RN ---------------------
317
349
 
350
+ /**
351
+ * Subscribes native SDK events stream and forwards events to JS via DeviceEventEmitter. Calling
352
+ * from JS multiple times replaces handler on JS side (native always emits to the same event
353
+ * name).
354
+ */
318
355
  override fun subscribeToEvents() {
319
356
  Events.subscribe { event -> sendEventToJs(event) }
320
357
  }
321
358
 
359
+ /** Unsubscribes native SDK events stream. */
322
360
  override fun unsubscribeFromEvent() {
323
361
  Events.unsubscribe()
324
362
  }
325
363
 
326
364
  private fun sendEventToJs(event: DataClasses.Event) {
327
365
  val eventMap = Arguments.createMap()
328
- eventMap.putString("function", event.function)
366
+ eventMap.putString(RnConstants.KEY_FUNCTION, event.function)
329
367
 
330
368
  val code = event.eventCode
331
- if (code != null) {
332
- eventMap.putInt("code", code)
333
- } else {
334
- eventMap.putNull("code")
335
- }
369
+ if (code != null) eventMap.putInt(RnConstants.KEY_CODE, code)
370
+ else eventMap.putNull(RnConstants.KEY_CODE)
336
371
 
337
372
  val message = event.eventMessage ?: event.toString()
338
- eventMap.putString("message", message)
373
+ eventMap.putString(RnConstants.KEY_MESSAGE, message)
339
374
 
340
375
  val value = event.eventValue
341
376
  if (value != null) {
@@ -352,275 +387,354 @@ class SdkModule(
352
387
  else -> valueMap.putString(key, anyValue.toString())
353
388
  }
354
389
  }
355
- eventMap.putMap("value", valueMap)
390
+ eventMap.putMap(RnConstants.KEY_VALUE, valueMap)
356
391
  } else {
357
- eventMap.putNull("value")
358
- }
359
-
360
- val type = when (event) {
361
- is DataClasses.Error -> "error"
362
- is DataClasses.RetryError -> "retryError"
363
- else -> "event"
392
+ eventMap.putNull(RnConstants.KEY_VALUE)
364
393
  }
365
- eventMap.putString("type", type)
394
+ val type =
395
+ when (event) {
396
+ is DataClasses.Error -> RnConstants.TYPE_ERROR
397
+ is DataClasses.RetryError -> RnConstants.TYPE_RETRY_ERROR
398
+ else -> RnConstants.TYPE_EVENT
399
+ }
400
+ eventMap.putString(RnConstants.KEY_TYPE, type)
366
401
 
367
402
  reactApplicationContext
368
- .getJSModule(RCTDeviceEventEmitter::class.java)
369
- .emit("AltcraftSdkEvent", eventMap)
403
+ .getJSModule(RCTDeviceEventEmitter::class.java)
404
+ .emit(RnConstants.JS_EVENT_NAME, eventMap)
370
405
  }
371
406
 
372
407
  // --------------------- subscription bridge ---------------------
373
408
 
409
+ /**
410
+ * Sends a push subscribe request.
411
+ *
412
+ * @param sync Execution mode:
413
+ * ```
414
+ * `true` — synchronous server request returning the operation result.
415
+ * `false` — asynchronous server request returning enqueue result.
416
+ * `null` — defaults to `true` on Android bridge.
417
+ * @param profileFields
418
+ * ```
419
+ * Optional profile fields (string-only map).
420
+ * @param customFields Optional custom fields (string-only map).
421
+ * @param cats Optional categories list.
422
+ * @param replace Optional flag to replace an existing subscription.
423
+ * @param skipTriggers Optional flag to skip trigger execution on the server.
424
+ */
374
425
  override fun pushSubscribe(
375
- sync: Boolean?,
376
- profileFields: ReadableMap?,
377
- customFields: ReadableMap?,
378
- cats: ReadableArray?,
379
- replace: Boolean?,
380
- skipTriggers: Boolean?
426
+ sync: Boolean?,
427
+ profileFields: ReadableMap?,
428
+ customFields: ReadableMap?,
429
+ cats: ReadableArray?,
430
+ replace: Boolean?,
431
+ skipTriggers: Boolean?
381
432
  ) {
382
433
  val isSync = sync ?: true
383
434
  PublicPushSubscriptionFunctions.pushSubscribe(
384
- context = reactApplicationContext,
385
- sync = isSync,
386
- profileFields = Converter.toKotlinMap(profileFields),
387
- customFields = Converter.toKotlinMap(customFields),
388
- cats = Converter.toCategoryListOrNull(cats),
389
- replace = replace,
390
- skipTriggers = skipTriggers
435
+ context = reactApplicationContext,
436
+ sync = isSync,
437
+ profileFields = Converter.toKotlinMap(profileFields),
438
+ customFields = Converter.toKotlinMap(customFields),
439
+ cats = Converter.toCategoryListOrNull(cats),
440
+ replace = replace,
441
+ skipTriggers = skipTriggers
391
442
  )
392
443
  }
393
444
 
445
+ /**
446
+ * Sends a push suspend request.
447
+ *
448
+ * @param sync Execution mode (see [pushSubscribe]).
449
+ * @param profileFields Optional profile fields (string-only map).
450
+ * @param customFields Optional custom fields (string-only map).
451
+ * @param cats Optional categories list.
452
+ * @param replace Optional flag to replace an existing subscription.
453
+ * @param skipTriggers Optional flag to skip trigger execution on the server.
454
+ */
394
455
  override fun pushSuspend(
395
- sync: Boolean?,
396
- profileFields: ReadableMap?,
397
- customFields: ReadableMap?,
398
- cats: ReadableArray?,
399
- replace: Boolean?,
400
- skipTriggers: Boolean?
456
+ sync: Boolean?,
457
+ profileFields: ReadableMap?,
458
+ customFields: ReadableMap?,
459
+ cats: ReadableArray?,
460
+ replace: Boolean?,
461
+ skipTriggers: Boolean?
401
462
  ) {
402
463
  val isSync = sync ?: true
403
464
  PublicPushSubscriptionFunctions.pushSuspend(
404
- context = reactApplicationContext,
405
- sync = isSync,
406
- profileFields = Converter.toKotlinMap(profileFields),
407
- customFields = Converter.toKotlinMap(customFields),
408
- cats = Converter.toCategoryListOrNull(cats),
409
- replace = replace,
410
- skipTriggers = skipTriggers
465
+ context = reactApplicationContext,
466
+ sync = isSync,
467
+ profileFields = Converter.toKotlinMap(profileFields),
468
+ customFields = Converter.toKotlinMap(customFields),
469
+ cats = Converter.toCategoryListOrNull(cats),
470
+ replace = replace,
471
+ skipTriggers = skipTriggers
411
472
  )
412
473
  }
413
474
 
475
+ /**
476
+ * Sends a push unsubscribe request.
477
+ *
478
+ * @param sync Execution mode (see [pushSubscribe]).
479
+ * @param profileFields Optional profile fields (string-only map).
480
+ * @param customFields Optional custom fields (string-only map).
481
+ * @param cats Optional categories list.
482
+ * @param replace Optional flag to replace an existing subscription.
483
+ * @param skipTriggers Optional flag to skip trigger execution on the server.
484
+ */
414
485
  override fun pushUnSubscribe(
415
- sync: Boolean?,
416
- profileFields: ReadableMap?,
417
- customFields: ReadableMap?,
418
- cats: ReadableArray?,
419
- replace: Boolean?,
420
- skipTriggers: Boolean?
486
+ sync: Boolean?,
487
+ profileFields: ReadableMap?,
488
+ customFields: ReadableMap?,
489
+ cats: ReadableArray?,
490
+ replace: Boolean?,
491
+ skipTriggers: Boolean?
421
492
  ) {
422
493
  val isSync = sync ?: true
423
494
  PublicPushSubscriptionFunctions.pushUnSubscribe(
424
- context = reactApplicationContext,
425
- sync = isSync,
426
- profileFields = Converter.toKotlinMap(profileFields),
427
- customFields = Converter.toKotlinMap(customFields),
428
- cats = Converter.toCategoryListOrNull(cats),
429
- replace = replace,
430
- skipTriggers = skipTriggers
495
+ context = reactApplicationContext,
496
+ sync = isSync,
497
+ profileFields = Converter.toKotlinMap(profileFields),
498
+ customFields = Converter.toKotlinMap(customFields),
499
+ cats = Converter.toCategoryListOrNull(cats),
500
+ replace = replace,
501
+ skipTriggers = skipTriggers
431
502
  )
432
503
  }
433
504
 
434
505
  // --------------------- async status bridges ---------------------
435
506
 
507
+ /**
508
+ * Unsuspends push subscriptions based on native matching rules.
509
+ *
510
+ * @param promise Resolves to response with HTTP code or `null` if unavailable.
511
+ */
436
512
  override fun unSuspendPushSubscription(promise: Promise) {
437
513
  coroutineScope.launch {
438
514
  try {
439
- val res = PublicPushSubscriptionFunctions.unSuspendPushSubscription(
440
- reactApplicationContext
441
- )
515
+ val res =
516
+ PublicPushSubscriptionFunctions.unSuspendPushSubscription(
517
+ reactApplicationContext
518
+ )
442
519
  promise.resolve(responseWithHttpCodeToWritableMap(res))
443
520
  } catch (e: Exception) {
444
- promise.reject("unSuspendPushSubscription", e)
521
+ promise.reject(RnConstants.ERR_UNSUSPEND, e)
445
522
  }
446
523
  }
447
524
  }
448
525
 
526
+ /**
527
+ * Returns the status of the latest subscription in profile.
528
+ *
529
+ * @param promise Resolves to response with HTTP code or `null` if unavailable.
530
+ */
449
531
  override fun getStatusOfLatestSubscription(promise: Promise) {
450
532
  coroutineScope.launch {
451
533
  try {
452
- val res = PublicPushSubscriptionFunctions.getStatusOfLatestSubscription(
453
- reactApplicationContext
454
- )
534
+ val res =
535
+ PublicPushSubscriptionFunctions.getStatusOfLatestSubscription(
536
+ reactApplicationContext
537
+ )
455
538
  promise.resolve(responseWithHttpCodeToWritableMap(res))
456
539
  } catch (e: Exception) {
457
- promise.reject("getStatusOfLatestSubscription", e)
540
+ promise.reject(RnConstants.ERR_GET_LATEST_STATUS, e)
458
541
  }
459
542
  }
460
543
  }
461
544
 
462
- override fun getStatusOfLatestSubscriptionForProvider(
463
- provider: String?,
464
- promise: Promise
465
- ) {
545
+ /**
546
+ * Returns the status of the latest subscription for the given provider.
547
+ *
548
+ * @param provider Optional provider identifier (may be null).
549
+ * @param promise Resolves to response with HTTP code or `null` if unavailable.
550
+ */
551
+ override fun getStatusOfLatestSubscriptionForProvider(provider: String?, promise: Promise) {
466
552
  coroutineScope.launch {
467
553
  try {
468
- val res = PublicPushSubscriptionFunctions.getStatusOfLatestSubscriptionForProvider(
469
- context = reactApplicationContext,
470
- provider = provider
471
- )
554
+ val res =
555
+ PublicPushSubscriptionFunctions.getStatusOfLatestSubscriptionForProvider(
556
+ context = reactApplicationContext,
557
+ provider = provider
558
+ )
472
559
  promise.resolve(responseWithHttpCodeToWritableMap(res))
473
560
  } catch (e: Exception) {
474
- promise.reject("getStatusOfLatestSubscriptionForProvider", e)
561
+ promise.reject(RnConstants.ERR_GET_LATEST_STATUS_PROVIDER, e)
475
562
  }
476
563
  }
477
564
  }
478
565
 
566
+ /**
567
+ * Returns the status for current subscription (by current provider/token context).
568
+ *
569
+ * @param promise Resolves to response with HTTP code or `null` if unavailable.
570
+ */
479
571
  override fun getStatusForCurrentSubscription(promise: Promise) {
480
572
  coroutineScope.launch {
481
573
  try {
482
- val res = PublicPushSubscriptionFunctions.getStatusForCurrentSubscription(
483
- reactApplicationContext
484
- )
574
+ val res =
575
+ PublicPushSubscriptionFunctions.getStatusForCurrentSubscription(
576
+ reactApplicationContext
577
+ )
485
578
  promise.resolve(responseWithHttpCodeToWritableMap(res))
486
579
  } catch (e: Exception) {
487
- promise.reject("getStatusForCurrentSubscription", e)
580
+ promise.reject(RnConstants.ERR_GET_CURRENT_STATUS, e)
488
581
  }
489
582
  }
490
583
  }
491
584
 
585
+ /**
586
+ * Maps {@link DataClasses.ResponseWithHttpCode} to a RN {@link WritableMap}. Returns `null` if
587
+ * input is `null`. Unmapped fields (`fields`, `cats`) are set to `null`.
588
+ */
492
589
  private fun responseWithHttpCodeToWritableMap(
493
- data: DataClasses.ResponseWithHttpCode?
590
+ data: DataClasses.ResponseWithHttpCode?
494
591
  ): WritableMap? {
495
592
  if (data == null) return null
496
593
 
497
- val root = Arguments.createMap()
498
-
499
- val httpCode = data.httpCode
500
- if (httpCode != null) {
501
- root.putInt("httpCode", httpCode)
502
- } else {
503
- root.putNull("httpCode")
504
- }
505
-
506
594
  val resp = data.response
507
- if (resp == null) {
508
- root.putNull("response")
509
- return root
510
- }
511
595
 
512
- val respMap = Arguments.createMap()
596
+ return Arguments.createMap().apply root@{
597
+ putNullableInt(RnConstants.KEY_HTTP_CODE, data.httpCode)
513
598
 
514
- val error = resp.error
515
- if (error != null) {
516
- respMap.putInt("error", error)
517
- } else {
518
- respMap.putNull("error")
519
- }
599
+ if (resp == null) {
600
+ putNull(RnConstants.KEY_RESPONSE)
601
+ return@root
602
+ }
520
603
 
521
- val errorText = resp.errorText
522
- if (errorText != null) {
523
- respMap.putString("errorText", errorText)
524
- } else {
525
- respMap.putNull("errorText")
526
- }
604
+ val respMap =
605
+ Arguments.createMap().apply resp@{
606
+ putNullableInt(RnConstants.KEY_ERROR, resp.error)
607
+ putNullableString(RnConstants.KEY_ERROR_TEXT, resp.errorText)
527
608
 
528
- val profile = resp.profile
529
- if (profile == null) {
530
- respMap.putNull("profile")
531
- } else {
532
- val profileMap = Arguments.createMap()
533
-
534
- val profileId = profile.id
535
- if (profileId != null) {
536
- profileMap.putString("id", profileId)
537
- } else {
538
- profileMap.putNull("id")
539
- }
609
+ val profile = resp.profile
610
+ if (profile == null) {
611
+ putNull(RnConstants.KEY_PROFILE)
612
+ return@resp
613
+ }
540
614
 
541
- val status = profile.status
542
- if (status != null) {
543
- profileMap.putString("status", status)
544
- } else {
545
- profileMap.putNull("status")
546
- }
615
+ val profileMap =
616
+ Arguments.createMap().apply profile@{
617
+ putNullableString(RnConstants.KEY_ID, profile.id)
618
+ putNullableString(RnConstants.KEY_STATUS, profile.status)
619
+ putNullableBoolean(RnConstants.KEY_IS_TEST, profile.isTest)
620
+
621
+ val sub = profile.subscription
622
+ if (sub == null) {
623
+ putNull(RnConstants.KEY_SUBSCRIPTION)
624
+ return@profile
625
+ }
626
+
627
+ val subMap =
628
+ Arguments.createMap().apply sub@{
629
+ putNullableString(
630
+ RnConstants.KEY_SUBSCRIPTION_ID,
631
+ sub.subscriptionId
632
+ )
633
+ putNullableString(
634
+ RnConstants.KEY_HASH_ID,
635
+ sub.hashId
636
+ )
637
+ putNullableString(
638
+ RnConstants.KEY_PROVIDER,
639
+ sub.provider
640
+ )
641
+ putNullableString(
642
+ RnConstants.KEY_STATUS,
643
+ sub.status
644
+ )
645
+
646
+ putNull(RnConstants.KEY_FIELDS)
647
+ putNull(RnConstants.KEY_CATS)
648
+ }
649
+
650
+ putMap(RnConstants.KEY_SUBSCRIPTION, subMap)
651
+ }
652
+
653
+ putMap(RnConstants.KEY_PROFILE, profileMap)
654
+ }
547
655
 
548
- val isTest = profile.isTest
549
- if (isTest != null) {
550
- profileMap.putBoolean("isTest", isTest)
551
- } else {
552
- profileMap.putNull("isTest")
553
- }
656
+ putMap(RnConstants.KEY_RESPONSE, respMap)
657
+ }
658
+ }
659
+
660
+ /**
661
+ * Stores a key-value pair in native persistent storage.
662
+ *
663
+ * Platform behavior:
664
+ * - iOS: uses UserDefaults (optionally scoped by `suiteName` / App Group).
665
+ * - Android: uses SharedPreferences; `suiteName` is used as preferences name.
666
+ *
667
+ * Supported value types:
668
+ * - primitives (Boolean, Number, String)
669
+ * - complex structures serialized as JSON string
670
+ * - `null` removes the stored value
671
+ *
672
+ * @param suiteName Storage namespace / suite name (platform-dependent).
673
+ * @param key Storage key.
674
+ * @param value Value to store or `null` to remove.
675
+ */
676
+ override fun setUserDefaultsValue(suiteName: String?, key: String, value: String?) {
677
+ val k = key.trim()
678
+ if (k.isEmpty()) return
554
679
 
555
- val sub = profile.subscription
556
- if (sub == null) {
557
- profileMap.putNull("subscription")
558
- } else {
559
- val subMap = Arguments.createMap()
680
+ val prefsName =
681
+ suiteName?.trim().takeUnless { it.isNullOrEmpty() }
682
+ ?: reactApplicationContext.packageName
560
683
 
561
- val subscriptionId = sub.subscriptionId
562
- if (subscriptionId != null) {
563
- subMap.putString("subscriptionId", subscriptionId)
564
- } else {
565
- subMap.putNull("subscriptionId")
566
- }
684
+ val prefs =
685
+ reactApplicationContext.getSharedPreferences(
686
+ prefsName,
687
+ android.content.Context.MODE_PRIVATE
688
+ )
567
689
 
568
- val hashId = sub.hashId
569
- if (hashId != null) {
570
- subMap.putString("hashId", hashId)
571
- } else {
572
- subMap.putNull("hashId")
573
- }
690
+ if (value == null) {
691
+ prefs.edit().remove(k).apply()
692
+ return
693
+ }
574
694
 
575
- val provider = sub.provider
576
- if (provider != null) {
577
- subMap.putString("provider", provider)
578
- } else {
579
- subMap.putNull("provider")
580
- }
695
+ val v = value.trim()
581
696
 
582
- val subStatus = sub.status
583
- if (subStatus != null) {
584
- subMap.putString("status", subStatus)
585
- } else {
586
- subMap.putNull("status")
587
- }
697
+ // Store JSON as String (SharedPreferences has no native JSON type)
698
+ if ((v.startsWith("{") && v.endsWith("}")) || (v.startsWith("[") && v.endsWith("]"))) {
699
+ prefs.edit().putString(k, v).apply()
700
+ return
701
+ }
588
702
 
589
- subMap.putNull("fields")
590
- subMap.putNull("cats")
703
+ // Boolean
704
+ if (v.equals("true", ignoreCase = true) || v.equals("false", ignoreCase = true)) {
705
+ prefs.edit().putBoolean(k, v.equals("true", ignoreCase = true)).apply()
706
+ return
707
+ }
591
708
 
592
- profileMap.putMap("subscription", subMap)
593
- }
709
+ // Integer (Long)
710
+ v.toLongOrNull()?.let { asLong ->
711
+ prefs.edit().putLong(k, asLong).apply()
712
+ return
713
+ }
594
714
 
595
- respMap.putMap("profile", profileMap)
715
+ // Double -> keep precision as String
716
+ v.toDoubleOrNull()?.let {
717
+ prefs.edit().putString(k, v).apply()
718
+ return
596
719
  }
597
720
 
598
- root.putMap("response", respMap)
599
- return root
721
+ // Fallback: String
722
+ prefs.edit().putString(k, value).apply()
600
723
  }
601
724
 
602
725
  companion object {
603
- const val NAME: String = "Sdk"
726
+ const val NAME: String = RnConstants.MODULE_NAME
604
727
  }
605
728
  }
606
729
 
607
- /**
608
- * Internal RN initializer entry point.
609
- */
730
+ /** Internal RN initializer entry point. */
610
731
  internal object AltcraftRnInitializer {
611
732
 
612
- fun initialize(
613
- reactContext: ReactApplicationContext,
614
- config: ReadableMap,
615
- promise: Promise
616
- ) {
733
+ fun initialize(reactContext: ReactApplicationContext, config: ReadableMap, promise: Promise) {
617
734
  try {
618
735
  val configuration = AltcraftRnConfigMapper.buildConfiguration(config)
619
736
 
620
- AltcraftSDK.initialization(
621
- reactContext,
622
- configuration
623
- ) { result ->
737
+ AltcraftSDK.initialization(reactContext, configuration) { result ->
624
738
  reactContext.runOnUiQueueThread {
625
739
  if (result.isSuccess) {
626
740
  promise.resolve(null)
@@ -628,139 +742,98 @@ internal object AltcraftRnInitializer {
628
742
  val throwable = result.exceptionOrNull()
629
743
  if (throwable != null) {
630
744
  promise.reject(
631
- "ALTCRAFT_INIT_ERROR",
632
- throwable.message ?: "Altcraft initialization error",
633
- throwable
745
+ RnConstants.ERR_INIT_ERROR,
746
+ throwable.message ?: RnConstants.MSG_INIT_ERROR,
747
+ throwable
634
748
  )
635
749
  } else {
636
- promise.reject(
637
- "ALTCRAFT_INIT_ERROR",
638
- "Altcraft initialization failed with unknown error"
639
- )
750
+ promise.reject(RnConstants.ERR_INIT_ERROR, RnConstants.MSG_INIT_UNKNOWN)
640
751
  }
641
752
  }
642
753
  }
643
754
  }
644
755
  } catch (e: Exception) {
645
756
  promise.reject(
646
- "ALTCRAFT_INIT_INVALID_CONFIG",
647
- e.message ?: "Invalid Altcraft configuration",
648
- e
757
+ RnConstants.ERR_INIT_INVALID_CONFIG,
758
+ e.message ?: "Invalid Altcraft configuration",
759
+ e
649
760
  )
650
761
  }
651
762
  }
652
763
  }
653
764
 
654
- /**
655
- * Maps React Native configuration object to native [AltcraftConfiguration].
656
- */
765
+ /** Maps React Native configuration object to native [AltcraftConfiguration]. */
657
766
  internal object AltcraftRnConfigMapper {
658
767
 
768
+ private const val KEY_API_URL = "apiUrl"
769
+ private const val KEY_R_TOKEN = "rToken"
770
+ private const val KEY_APP_INFO = "appInfo"
771
+ private const val KEY_PROVIDER_PRIORITY_LIST = "providerPriorityList"
772
+ private const val KEY_ENABLE_LOGGING = "enableLogging"
773
+
774
+ private const val KEY_ANDROID_ICON = "android_icon"
775
+ private const val KEY_ANDROID_USING_SERVICE = "android_usingService"
776
+ private const val KEY_ANDROID_SERVICE_MESSAGE = "android_serviceMessage"
777
+ private const val KEY_ANDROID_PUSH_RECEIVER_MODULES = "android_pushReceiverModules"
778
+ private const val KEY_ANDROID_PUSH_CHANNEL_NAME = "android_pushChannelName"
779
+ private const val KEY_ANDROID_PUSH_CHANNEL_DESCRIPTION = "android_pushChannelDescription"
780
+
781
+ private const val KEY_APP_ID = "appID"
782
+ private const val KEY_APP_IID = "appIID"
783
+ private const val KEY_APP_VER = "appVer"
784
+
785
+ private const val MSG_API_URL_REQUIRED = "apiUrl is required for AltcraftConfiguration"
786
+ private const val MSG_API_URL_MUST_BE_STRING = "apiUrl must be a non-null string"
787
+
659
788
  fun buildConfiguration(config: ReadableMap): AltcraftConfiguration {
660
- if (!config.hasKey("apiUrl") || config.isNull("apiUrl")) {
661
- throw IllegalArgumentException("apiUrl is required for AltcraftConfiguration")
789
+ if (!config.hasNonNullKey(KEY_API_URL)) {
790
+ throw IllegalArgumentException(MSG_API_URL_REQUIRED)
662
791
  }
663
792
 
664
- val apiUrl = config.getString("apiUrl")
665
- ?: throw IllegalArgumentException("apiUrl must be a non-null string")
793
+ val apiUrl =
794
+ config.getString(KEY_API_URL)
795
+ ?: throw IllegalArgumentException(MSG_API_URL_MUST_BE_STRING)
666
796
 
667
- val rToken: String? =
668
- if (config.hasKey("rToken") && !config.isNull("rToken")) {
669
- config.getString("rToken")
670
- } else {
671
- null
672
- }
673
-
674
- val appInfo: DataClasses.AppInfo? =
675
- if (config.hasKey("appInfo") && !config.isNull("appInfo")) {
676
- val appInfoMap = config.getMap("appInfo")
677
- if (appInfoMap == null) {
678
- null
679
- } else {
680
- val appID =
681
- if (appInfoMap.hasKey("appID") && !appInfoMap.isNull("appID")) {
682
- appInfoMap.getString("appID") ?: ""
683
- } else ""
684
- val appIID =
685
- if (appInfoMap.hasKey("appIID") && !appInfoMap.isNull("appIID")) {
686
- appInfoMap.getString("appIID") ?: ""
687
- } else ""
688
- val appVer =
689
- if (appInfoMap.hasKey("appVer") && !appInfoMap.isNull("appVer")) {
690
- appInfoMap.getString("appVer") ?: ""
691
- } else ""
797
+ val rToken = config.getStringOrNull(KEY_R_TOKEN)
692
798
 
799
+ val appInfo =
800
+ config.getMapOrNull(KEY_APP_INFO)?.let { appInfoMap ->
801
+ val appID = appInfoMap.getStringOrNull(KEY_APP_ID) ?: ""
802
+ val appIID = appInfoMap.getStringOrNull(KEY_APP_IID) ?: ""
803
+ val appVer = appInfoMap.getStringOrNull(KEY_APP_VER) ?: ""
693
804
  DataClasses.AppInfo(appID = appID, appIID = appIID, appVer = appVer)
694
805
  }
695
- } else {
696
- null
697
- }
698
806
 
699
- val providerPriorityList: List<String>? =
700
- if (config.hasKey("providerPriorityList") && !config.isNull("providerPriorityList")) {
701
- val array = config.getArray("providerPriorityList")
702
- if (array == null) null else readableStringArrayToList(array)
703
- } else null
704
-
705
- val enableLogging: Boolean? =
706
- if (config.hasKey("enableLogging") && !config.isNull("enableLogging")) {
707
- config.getBoolean("enableLogging")
708
- } else null
709
-
710
- val icon: Int? =
711
- if (config.hasKey("android_icon") && !config.isNull("android_icon")) {
712
- config.getInt("android_icon")
713
- } else null
714
-
715
- val usingService: Boolean =
716
- if (config.hasKey("android_usingService") && !config.isNull("android_usingService")) {
717
- config.getBoolean("android_usingService")
718
- } else false
719
-
720
- val serviceMessage: String? =
721
- if (config.hasKey("android_serviceMessage") && !config.isNull("android_serviceMessage")) {
722
- config.getString("android_serviceMessage")
723
- } else null
724
-
725
- val pushReceiverModules: List<String>? =
726
- if (config.hasKey("android_pushReceiverModules") && !config.isNull("android_pushReceiverModules")) {
727
- val array = config.getArray("android_pushReceiverModules")
728
- if (array == null) null else readableStringArrayToList(array)
729
- } else null
730
-
731
- val pushChannelName: String? =
732
- if (config.hasKey("android_pushChannelName") && !config.isNull("android_pushChannelName")) {
733
- config.getString("android_pushChannelName")
734
- } else null
735
-
736
- val pushChannelDescription: String? =
737
- if (config.hasKey("android_pushChannelDescription") && !config.isNull("android_pushChannelDescription")) {
738
- config.getString("android_pushChannelDescription")
739
- } else null
807
+ val providerPriorityList = config.getArrayOrNull(KEY_PROVIDER_PRIORITY_LIST)?.toStringList()
740
808
 
741
- return AltcraftConfiguration.Builder(
742
- apiUrl = apiUrl,
743
- icon = icon,
744
- rToken = rToken,
745
- usingService = usingService,
746
- serviceMessage = serviceMessage,
747
- appInfo = appInfo,
748
- providerPriorityList = providerPriorityList,
749
- pushReceiverModules = pushReceiverModules,
750
- pushChannelName = pushChannelName,
751
- pushChannelDescription = pushChannelDescription,
752
- enableLogging = enableLogging
753
- ).build()
754
- }
809
+ val enableLogging = config.getBooleanOrNull(KEY_ENABLE_LOGGING)
755
810
 
756
- private fun readableStringArrayToList(array: ReadableArray): List<String> {
757
- val result = ArrayList<String>()
758
- for (i in 0 until array.size()) {
759
- if (!array.isNull(i)) {
760
- val value = array.getString(i)
761
- if (value != null) result.add(value)
762
- }
763
- }
764
- return result
811
+ val icon = config.getIntOrNull(KEY_ANDROID_ICON)
812
+
813
+ val usingService = config.getBooleanOrNull(KEY_ANDROID_USING_SERVICE) ?: false
814
+
815
+ val serviceMessage = config.getStringOrNull(KEY_ANDROID_SERVICE_MESSAGE)
816
+
817
+ val pushReceiverModules =
818
+ config.getArrayOrNull(KEY_ANDROID_PUSH_RECEIVER_MODULES)?.toStringList()
819
+
820
+ val pushChannelName = config.getStringOrNull(KEY_ANDROID_PUSH_CHANNEL_NAME)
821
+
822
+ val pushChannelDescription = config.getStringOrNull(KEY_ANDROID_PUSH_CHANNEL_DESCRIPTION)
823
+
824
+ return AltcraftConfiguration.Builder(
825
+ apiUrl = apiUrl,
826
+ icon = icon,
827
+ rToken = rToken,
828
+ usingService = usingService,
829
+ serviceMessage = serviceMessage,
830
+ appInfo = appInfo,
831
+ providerPriorityList = providerPriorityList,
832
+ pushReceiverModules = pushReceiverModules,
833
+ pushChannelName = pushChannelName,
834
+ pushChannelDescription = pushChannelDescription,
835
+ enableLogging = enableLogging
836
+ )
837
+ .build()
765
838
  }
766
839
  }