@altcraft/react-native-sdk 0.0.2 → 0.0.4

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 (36) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +119 -27
  3. package/Sdk.podspec +2 -2
  4. package/android/build.gradle +3 -4
  5. package/android/src/main/java/com/altcraft/sdk/SdkEventEmitter.kt +50 -0
  6. package/android/src/main/java/com/altcraft/sdk/SdkModule.kt +663 -480
  7. package/android/src/main/java/com/altcraft/sdk/SdkPackage.kt +16 -8
  8. package/android/src/main/java/com/altcraft/sdk/utilities/Converter.kt +164 -0
  9. package/android/src/main/java/com/altcraft/sdk/utilities/Extension.kt +72 -0
  10. package/ios/Converter.swift +485 -48
  11. package/ios/Sdk.mm +195 -270
  12. package/ios/SdkModule.swift +456 -507
  13. package/lib/module/NativeSdk.js +35 -0
  14. package/lib/module/NativeSdk.js.map +1 -1
  15. package/lib/module/Utilities.js +178 -0
  16. package/lib/module/Utilities.js.map +1 -0
  17. package/lib/module/index.js +335 -245
  18. package/lib/module/index.js.map +1 -1
  19. package/lib/typescript/src/NativeSdk.d.ts +125 -33
  20. package/lib/typescript/src/NativeSdk.d.ts.map +1 -1
  21. package/lib/typescript/src/Utilities.d.ts +47 -0
  22. package/lib/typescript/src/Utilities.d.ts.map +1 -0
  23. package/lib/typescript/src/index.d.ts +218 -94
  24. package/lib/typescript/src/index.d.ts.map +1 -1
  25. package/package.json +1 -1
  26. package/src/NativeSdk.ts +169 -46
  27. package/src/Utilities.ts +180 -0
  28. package/src/index.tsx +410 -312
  29. package/android/src/main/java/com/altcraft/sdk/interfaces/RnFCMProvider.kt +0 -26
  30. package/android/src/main/java/com/altcraft/sdk/interfaces/RnHMSProvider.kt +0 -32
  31. package/android/src/main/java/com/altcraft/sdk/interfaces/RnJWTProvider.kt +0 -21
  32. package/android/src/main/java/com/altcraft/sdk/interfaces/RnRustoreProvider.kt +0 -53
  33. package/ios/protocols/RNAPNSProvider.swift +0 -15
  34. package/ios/protocols/RNFCMProvider.swift +0 -24
  35. package/ios/protocols/RNHMSProvider.swift +0 -24
  36. package/ios/protocols/RNJWTProvider.swift +0 -15
@@ -9,10 +9,10 @@ import java.util.HashMap
9
9
 
10
10
  class SdkPackage : BaseReactPackage() {
11
11
  override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
12
- return if (name == SdkModule.NAME) {
13
- SdkModule(reactContext)
14
- } else {
15
- null
12
+ return when (name) {
13
+ SdkModule.NAME -> SdkModule(reactContext)
14
+ SdkEventEmitter.NAME -> SdkEventEmitter(reactContext)
15
+ else -> null
16
16
  }
17
17
  }
18
18
 
@@ -22,10 +22,18 @@ class SdkPackage : BaseReactPackage() {
22
22
  moduleInfos[SdkModule.NAME] = ReactModuleInfo(
23
23
  SdkModule.NAME,
24
24
  SdkModule.NAME,
25
- false, // canOverrideExistingModule
26
- false, // needsEagerInit
27
- false, // isCxxModule
28
- true // isTurboModule
25
+ false,
26
+ false,
27
+ false,
28
+ true
29
+ )
30
+ moduleInfos[SdkEventEmitter.NAME] = ReactModuleInfo(
31
+ SdkEventEmitter.NAME,
32
+ SdkEventEmitter.NAME,
33
+ false,
34
+ false,
35
+ false,
36
+ false
29
37
  )
30
38
  moduleInfos
31
39
  }
@@ -4,6 +4,13 @@ import com.altcraft.sdk.data.DataClasses
4
4
  import com.facebook.react.bridge.ReadableArray
5
5
  import com.facebook.react.bridge.ReadableMap
6
6
  import com.facebook.react.bridge.ReadableType
7
+ import kotlinx.serialization.json.Json
8
+ import kotlinx.serialization.json.JsonObject
9
+ import kotlinx.serialization.json.jsonObject
10
+ import org.json.JSONArray
11
+ import org.json.JSONException
12
+ import org.json.JSONObject
13
+ import android.widget.Toast
7
14
 
8
15
  object Converter {
9
16
 
@@ -159,4 +166,161 @@ object Converter {
159
166
  asLong
160
167
  }
161
168
  }
169
+
170
+ private val jsonKx = Json { ignoreUnknownKeys = true }
171
+
172
+ /**
173
+ * RN subscription map comes from JS as Record<string,string> (string-only).
174
+ * So: resource_id / priority may arrive as strings.
175
+ */
176
+ fun toSubscriptionOrNull(subscriptionMap: ReadableMap?): DataClasses.Subscription? {
177
+ if (subscriptionMap == null) return null
178
+
179
+ return try {
180
+ val type = subscriptionMap.getString("type")?.trim().orEmpty()
181
+ if (type.isEmpty()) return null
182
+
183
+ val resourceId = subscriptionMap.getString("resource_id")
184
+ ?.trim()
185
+ ?.toIntOrNull()
186
+ ?: return null
187
+
188
+ val status = subscriptionMap.getString("status")
189
+
190
+ val priority: Int? =
191
+ subscriptionMap.getString("priority")
192
+ ?.trim()
193
+ ?.toIntOrNull()
194
+
195
+ val customFields: Map<String, Any?>? =
196
+ subscriptionMap.getString("custom_fields")
197
+ ?.takeIf { it.isNotBlank() }
198
+ ?.let { parseJsonToMap(it) }
199
+
200
+ val cats: List<String>? =
201
+ subscriptionMap.getString("cats")
202
+ ?.takeIf { it.isNotBlank() }
203
+ ?.let { parseJsonToStringList(it) }
204
+
205
+ when (type) {
206
+ "email" -> {
207
+ val email = subscriptionMap.getString("email") ?: return null
208
+ DataClasses.EmailSubscription(
209
+ resourceId = resourceId,
210
+ email = email,
211
+ status = status,
212
+ priority = priority,
213
+ customFields = customFields,
214
+ cats = cats
215
+ )
216
+ }
217
+
218
+ "sms" -> {
219
+ val phone = subscriptionMap.getString("phone") ?: return null
220
+ DataClasses.SmsSubscription(
221
+ resourceId = resourceId,
222
+ phone = phone,
223
+ status = status,
224
+ priority = priority,
225
+ customFields = customFields,
226
+ cats = cats
227
+ )
228
+ }
229
+
230
+ "push" -> {
231
+ val provider = subscriptionMap.getString("provider") ?: return null
232
+ val subscriptionId = subscriptionMap.getString("subscription_id") ?: return null
233
+ DataClasses.PushSubscription(
234
+ resourceId = resourceId,
235
+ provider = provider,
236
+ subscriptionId = subscriptionId,
237
+ status = status,
238
+ priority = priority,
239
+ customFields = customFields,
240
+ cats = cats
241
+ )
242
+ }
243
+
244
+ "cc_data" -> {
245
+ val channel = subscriptionMap.getString("channel") ?: return null
246
+ val ccDataJson = subscriptionMap.getString("cc_data") ?: "{}"
247
+ val ccData = parseJsonObjectToKotlinx(ccDataJson)
248
+ DataClasses.CcDataSubscription(
249
+ resourceId = resourceId,
250
+ channel = channel,
251
+ ccData = ccData,
252
+ status = status,
253
+ priority = priority,
254
+ customFields = customFields,
255
+ cats = cats
256
+ )
257
+ }
258
+
259
+ else -> null
260
+ }
261
+ } catch (_: Exception) {
262
+ null
263
+ }
264
+ }
265
+
266
+ // ---------------- JSON helpers ----------------
267
+
268
+ private fun parseJsonToMap(jsonString: String): Map<String, Any?> {
269
+ val result = mutableMapOf<String, Any?>()
270
+ try {
271
+ val jsonObject = JSONObject(jsonString)
272
+ val keys = jsonObject.keys()
273
+ while (keys.hasNext()) {
274
+ val key = keys.next()
275
+ val value = jsonObject.get(key)
276
+ result[key] = when (value) {
277
+ is JSONObject -> parseJsonToMap(value.toString())
278
+ is JSONArray -> parseJsonArrayToList(value)
279
+ JSONObject.NULL -> null
280
+ else -> value
281
+ }
282
+ }
283
+ } catch (_: JSONException) {
284
+ // return empty map on parse error
285
+ }
286
+ return result
287
+ }
288
+
289
+ private fun parseJsonToStringList(jsonString: String): List<String> {
290
+ val result = mutableListOf<String>()
291
+ try {
292
+ val jsonArray = JSONArray(jsonString)
293
+ for (i in 0 until jsonArray.length()) {
294
+ val item = jsonArray.optString(i, null)
295
+ if (item != null) result.add(item)
296
+ }
297
+ } catch (_: JSONException) {
298
+ // return empty list on parse error
299
+ }
300
+ return result
301
+ }
302
+
303
+ private fun parseJsonArrayToList(jsonArray: JSONArray): List<Any?> {
304
+ val result = mutableListOf<Any?>()
305
+ for (i in 0 until jsonArray.length()) {
306
+ val value = jsonArray.get(i)
307
+ result.add(
308
+ when (value) {
309
+ is JSONObject -> parseJsonToMap(value.toString())
310
+ is JSONArray -> parseJsonArrayToList(value)
311
+ JSONObject.NULL -> null
312
+ else -> value
313
+ }
314
+ )
315
+ }
316
+ return result
317
+ }
318
+
319
+ private fun parseJsonObjectToKotlinx(jsonString: String): JsonObject {
320
+ return try {
321
+ jsonKx.parseToJsonElement(jsonString).jsonObject
322
+ } catch (_: Exception) {
323
+ JsonObject(emptyMap())
324
+ }
325
+ }
162
326
  }
@@ -0,0 +1,72 @@
1
+ package com.altcraft.sdk.rn
2
+
3
+ import com.facebook.react.bridge.WritableMap
4
+ import com.facebook.react.bridge.ReadableArray
5
+ import com.facebook.react.bridge.ReadableMap
6
+
7
+ /** Puts Int or null into WritableMap. */
8
+ internal fun WritableMap.putNullableInt(key: String, value: Int?) {
9
+ if (value != null) putInt(key, value) else putNull(key)
10
+ }
11
+
12
+ /** Puts Double or null into WritableMap. */
13
+ internal fun WritableMap.putNullableDouble(key: String, value: Double?) {
14
+ if (value != null) putDouble(key, value) else putNull(key)
15
+ }
16
+
17
+ /** Puts Boolean or null into WritableMap. */
18
+ internal fun WritableMap.putNullableBoolean(key: String, value: Boolean?) {
19
+ if (value != null) putBoolean(key, value) else putNull(key)
20
+ }
21
+
22
+ /** Puts String or null into WritableMap. */
23
+ internal fun WritableMap.putNullableString(key: String, value: String?) {
24
+ if (value != null) putString(key, value) else putNull(key)
25
+ }
26
+
27
+ /** Puts nested map or null into WritableMap. */
28
+ internal fun WritableMap.putNullableMap(key: String, value: WritableMap?) {
29
+ if (value != null) putMap(key, value) else putNull(key)
30
+ }
31
+
32
+ /** Returns true if key exists and is not null. */
33
+ internal fun ReadableMap.hasNonNullKey(key: String): Boolean {
34
+ return hasKey(key) && !isNull(key)
35
+ }
36
+
37
+ /** Returns String value or null if missing/null. */
38
+ internal fun ReadableMap.getStringOrNull(key: String): String? {
39
+ return if (hasNonNullKey(key)) getString(key) else null
40
+ }
41
+
42
+ /** Returns Boolean value or null if missing/null. */
43
+ internal fun ReadableMap.getBooleanOrNull(key: String): Boolean? {
44
+ return if (hasNonNullKey(key)) getBoolean(key) else null
45
+ }
46
+
47
+ /** Returns Int value or null if missing/null. */
48
+ internal fun ReadableMap.getIntOrNull(key: String): Int? {
49
+ return if (hasNonNullKey(key)) getInt(key) else null
50
+ }
51
+
52
+ /** Returns nested map or null if missing/null. */
53
+ internal fun ReadableMap.getMapOrNull(key: String): ReadableMap? {
54
+ return if (hasNonNullKey(key)) getMap(key) else null
55
+ }
56
+
57
+ /** Returns array or null if missing/null. */
58
+ internal fun ReadableMap.getArrayOrNull(key: String): ReadableArray? {
59
+ return if (hasNonNullKey(key)) getArray(key) else null
60
+ }
61
+
62
+ /** Converts ReadableArray of strings to Kotlin List<String> (skips nulls/non-strings). */
63
+ internal fun ReadableArray.toStringList(): List<String> {
64
+ val result = ArrayList<String>(size())
65
+ for (i in 0 until size()) {
66
+ if (!isNull(i)) {
67
+ val v = getString(i)
68
+ if (v != null) result.add(v)
69
+ }
70
+ }
71
+ return result
72
+ }