@altcraft/react-native-sdk 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) 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 +493 -896
  5. package/android/src/main/java/com/altcraft/sdk/utilities/Converter.kt +326 -0
  6. package/android/src/main/java/com/altcraft/sdk/utilities/Extension.kt +72 -0
  7. package/ios/Converter.swift +640 -0
  8. package/ios/Sdk.h +4 -3
  9. package/ios/Sdk.mm +124 -370
  10. package/ios/SdkModule.swift +436 -653
  11. package/lib/module/NativeSdk.js +35 -0
  12. package/lib/module/NativeSdk.js.map +1 -1
  13. package/lib/module/Utilities.js +94 -0
  14. package/lib/module/Utilities.js.map +1 -0
  15. package/lib/module/index.js +180 -419
  16. package/lib/module/index.js.map +1 -1
  17. package/lib/typescript/src/NativeSdk.d.ts +117 -32
  18. package/lib/typescript/src/NativeSdk.d.ts.map +1 -1
  19. package/lib/typescript/src/Utilities.d.ts +15 -0
  20. package/lib/typescript/src/Utilities.d.ts.map +1 -0
  21. package/lib/typescript/src/index.d.ts +113 -308
  22. package/lib/typescript/src/index.d.ts.map +1 -1
  23. package/package.json +2 -2
  24. package/src/NativeSdk.ts +156 -46
  25. package/src/Utilities.ts +116 -0
  26. package/src/index.tsx +255 -483
  27. package/android/src/main/java/com/altcraft/sdk/interfaces/RnFCMProvider.kt +0 -27
  28. package/android/src/main/java/com/altcraft/sdk/interfaces/RnHMSProvider.kt +0 -32
  29. package/android/src/main/java/com/altcraft/sdk/interfaces/RnJWTProvider.kt +0 -21
  30. package/android/src/main/java/com/altcraft/sdk/interfaces/RnRustoreProvider.kt +0 -53
  31. package/ios/protocols/RNAPNSProvider.swift +0 -15
  32. package/ios/protocols/RNFCMProvider.swift +0 -24
  33. package/ios/protocols/RNHMSProvider.swift +0 -24
  34. package/ios/protocols/RNJWTProvider.swift +0 -15
@@ -0,0 +1,326 @@
1
+ package com.altcraft.sdk.utilities
2
+
3
+ import com.altcraft.sdk.data.DataClasses
4
+ import com.facebook.react.bridge.ReadableArray
5
+ import com.facebook.react.bridge.ReadableMap
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
14
+
15
+ object Converter {
16
+
17
+ /** ReadableMap? -> Map<String, Any?>? (RN object -> Kotlin map) */
18
+ fun toKotlinMap(map: ReadableMap?): Map<String, Any?>? {
19
+ if (map == null) return null
20
+ val result = LinkedHashMap<String, Any?>()
21
+ val it = map.keySetIterator()
22
+ while (it.hasNextKey()) {
23
+ val key = it.nextKey()
24
+ result[key] = readMapValue(map, key) // RN value -> Kotlin Any?
25
+ }
26
+ return result
27
+ }
28
+
29
+ /** ReadableArray? -> List<Any?>? (RN array -> Kotlin list) */
30
+ fun toKotlinList(array: ReadableArray?): List<Any?>? {
31
+ if (array == null) return null
32
+ val result = ArrayList<Any?>(array.size())
33
+ for (i in 0 until array.size()) {
34
+ result.add(readArrayValue(array, i)) // RN value -> Kotlin Any?
35
+ }
36
+ return result
37
+ }
38
+
39
+ /** ReadableArray? -> List<String> (берёт только строки, остальное игнорит) */
40
+ fun toStringList(array: ReadableArray?): List<String> {
41
+ val anyList = toKotlinList(array) ?: return emptyList()
42
+ val result = ArrayList<String>(anyList.size)
43
+ for (v in anyList) {
44
+ if (v is String) result.add(v)
45
+ }
46
+ return result
47
+ }
48
+
49
+ /** ReadableMap? -> Map<String, String>? (берёт только string values; если пусто -> null) */
50
+ fun toStringMapOrNull(map: ReadableMap?): Map<String, String>? {
51
+ val anyMap = toKotlinMap(map) ?: return null
52
+ val result = LinkedHashMap<String, String>()
53
+ for ((k, v) in anyMap) {
54
+ if (v is String) result[k] = v
55
+ }
56
+ return if (result.isEmpty()) null else result
57
+ }
58
+
59
+ /**
60
+ * ReadableArray? -> List<CategoryData>?
61
+ * RN array items:
62
+ * - String -> CategoryData(name=string, active=true)
63
+ * - Map -> CategoryData(name/title/steady/active) (добавляется только если есть name)
64
+ */
65
+ fun toCategoryListOrNull(array: ReadableArray?): List<DataClasses.CategoryData>? {
66
+ if (array == null) return null
67
+
68
+ val anyList = toKotlinList(array) ?: return emptyList()
69
+ val result = ArrayList<DataClasses.CategoryData>(anyList.size)
70
+
71
+ for (item in anyList) {
72
+ when (item) {
73
+ is String -> {
74
+ result.add(
75
+ DataClasses.CategoryData(
76
+ name = item,
77
+ title = null,
78
+ steady = null,
79
+ active = true
80
+ )
81
+ )
82
+ }
83
+
84
+ is Map<*, *> -> {
85
+ val name = item["name"] as? String
86
+ val title = item["title"] as? String
87
+ val steady = item["steady"] as? Boolean
88
+ val active = item["active"] as? Boolean
89
+
90
+ if (name != null) {
91
+ result.add(
92
+ DataClasses.CategoryData(
93
+ name = name,
94
+ title = title,
95
+ steady = steady,
96
+ active = active
97
+ )
98
+ )
99
+ }
100
+ }
101
+ }
102
+ }
103
+
104
+ return result
105
+ }
106
+
107
+ /**
108
+ * Any? -> Any?
109
+ * - ReadableMap -> Map<String, Any?>?
110
+ * - ReadableArray -> List<Any?>?
111
+ * - Number(Double/Float) -> Int/Long/Double (нормализация)
112
+ * - Boolean/String/Int/Long -> как есть
113
+ */
114
+ fun toKotlinAny(value: Any?): Any? = when (value) {
115
+ null -> null
116
+ is ReadableMap -> toKotlinMap(value)
117
+ is ReadableArray -> toKotlinList(value)
118
+ is Boolean -> value
119
+ is String -> value
120
+ is Double -> normalizeNumber(value)
121
+ is Float -> normalizeNumber(value.toDouble())
122
+ is Int, is Long -> value
123
+ else -> value
124
+ }
125
+
126
+ /** ReadableMap[key] -> Kotlin Any? */
127
+ private fun readMapValue(map: ReadableMap, key: String): Any? {
128
+ return when (map.getType(key)) {
129
+ ReadableType.Null -> null
130
+ ReadableType.Boolean -> map.getBoolean(key)
131
+ ReadableType.String -> map.getString(key)
132
+ ReadableType.Number -> normalizeNumber(map.getDouble(key))
133
+ ReadableType.Map -> toKotlinMap(map.getMap(key))
134
+ ReadableType.Array -> toKotlinList(map.getArray(key))
135
+ }
136
+ }
137
+
138
+ /** ReadableArray[index] -> Kotlin Any? */
139
+ private fun readArrayValue(array: ReadableArray, index: Int): Any? {
140
+ return when (array.getType(index)) {
141
+ ReadableType.Null -> null
142
+ ReadableType.Boolean -> array.getBoolean(index)
143
+ ReadableType.String -> array.getString(index)
144
+ ReadableType.Number -> normalizeNumber(array.getDouble(index))
145
+ ReadableType.Map -> toKotlinMap(array.getMap(index))
146
+ ReadableType.Array -> toKotlinList(array.getArray(index))
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Double(JS number) -> Int/Long/Double
152
+ * - NaN/Infinity -> Double
153
+ * - целое -> Int если влезает, иначе Long
154
+ * - дробное -> Double
155
+ */
156
+ private fun normalizeNumber(d: Double): Any {
157
+ if (!d.isFinite()) return d
158
+
159
+ val asLong = d.toLong()
160
+ val isInteger = d == asLong.toDouble()
161
+ if (!isInteger) return d
162
+
163
+ return if (asLong in Int.MIN_VALUE.toLong()..Int.MAX_VALUE.toLong()) {
164
+ asLong.toInt()
165
+ } else {
166
+ asLong
167
+ }
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
+ }
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
+ }