@altcraft/react-native-sdk 0.0.1 → 0.0.2
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/android/build.gradle +1 -1
- package/android/src/main/java/com/altcraft/sdk/SdkModule.kt +53 -529
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnFCMProvider.kt +0 -1
- package/android/src/main/java/com/altcraft/sdk/utilities/Converter.kt +162 -0
- package/ios/Converter.swift +203 -0
- package/ios/Sdk.h +4 -3
- package/ios/Sdk.mm +74 -132
- package/ios/SdkModule.swift +260 -319
- package/lib/module/NativeSdk.js.map +1 -1
- package/lib/module/index.js +124 -341
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/NativeSdk.d.ts +11 -2
- package/lib/typescript/src/NativeSdk.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +69 -332
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/NativeSdk.ts +25 -6
- package/src/index.tsx +170 -368
|
@@ -0,0 +1,162 @@
|
|
|
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
|
+
|
|
8
|
+
object Converter {
|
|
9
|
+
|
|
10
|
+
/** ReadableMap? -> Map<String, Any?>? (RN object -> Kotlin map) */
|
|
11
|
+
fun toKotlinMap(map: ReadableMap?): Map<String, Any?>? {
|
|
12
|
+
if (map == null) return null
|
|
13
|
+
val result = LinkedHashMap<String, Any?>()
|
|
14
|
+
val it = map.keySetIterator()
|
|
15
|
+
while (it.hasNextKey()) {
|
|
16
|
+
val key = it.nextKey()
|
|
17
|
+
result[key] = readMapValue(map, key) // RN value -> Kotlin Any?
|
|
18
|
+
}
|
|
19
|
+
return result
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** ReadableArray? -> List<Any?>? (RN array -> Kotlin list) */
|
|
23
|
+
fun toKotlinList(array: ReadableArray?): List<Any?>? {
|
|
24
|
+
if (array == null) return null
|
|
25
|
+
val result = ArrayList<Any?>(array.size())
|
|
26
|
+
for (i in 0 until array.size()) {
|
|
27
|
+
result.add(readArrayValue(array, i)) // RN value -> Kotlin Any?
|
|
28
|
+
}
|
|
29
|
+
return result
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** ReadableArray? -> List<String> (берёт только строки, остальное игнорит) */
|
|
33
|
+
fun toStringList(array: ReadableArray?): List<String> {
|
|
34
|
+
val anyList = toKotlinList(array) ?: return emptyList()
|
|
35
|
+
val result = ArrayList<String>(anyList.size)
|
|
36
|
+
for (v in anyList) {
|
|
37
|
+
if (v is String) result.add(v)
|
|
38
|
+
}
|
|
39
|
+
return result
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** ReadableMap? -> Map<String, String>? (берёт только string values; если пусто -> null) */
|
|
43
|
+
fun toStringMapOrNull(map: ReadableMap?): Map<String, String>? {
|
|
44
|
+
val anyMap = toKotlinMap(map) ?: return null
|
|
45
|
+
val result = LinkedHashMap<String, String>()
|
|
46
|
+
for ((k, v) in anyMap) {
|
|
47
|
+
if (v is String) result[k] = v
|
|
48
|
+
}
|
|
49
|
+
return if (result.isEmpty()) null else result
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* ReadableArray? -> List<CategoryData>?
|
|
54
|
+
* RN array items:
|
|
55
|
+
* - String -> CategoryData(name=string, active=true)
|
|
56
|
+
* - Map -> CategoryData(name/title/steady/active) (добавляется только если есть name)
|
|
57
|
+
*/
|
|
58
|
+
fun toCategoryListOrNull(array: ReadableArray?): List<DataClasses.CategoryData>? {
|
|
59
|
+
if (array == null) return null
|
|
60
|
+
|
|
61
|
+
val anyList = toKotlinList(array) ?: return emptyList()
|
|
62
|
+
val result = ArrayList<DataClasses.CategoryData>(anyList.size)
|
|
63
|
+
|
|
64
|
+
for (item in anyList) {
|
|
65
|
+
when (item) {
|
|
66
|
+
is String -> {
|
|
67
|
+
result.add(
|
|
68
|
+
DataClasses.CategoryData(
|
|
69
|
+
name = item,
|
|
70
|
+
title = null,
|
|
71
|
+
steady = null,
|
|
72
|
+
active = true
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
is Map<*, *> -> {
|
|
78
|
+
val name = item["name"] as? String
|
|
79
|
+
val title = item["title"] as? String
|
|
80
|
+
val steady = item["steady"] as? Boolean
|
|
81
|
+
val active = item["active"] as? Boolean
|
|
82
|
+
|
|
83
|
+
if (name != null) {
|
|
84
|
+
result.add(
|
|
85
|
+
DataClasses.CategoryData(
|
|
86
|
+
name = name,
|
|
87
|
+
title = title,
|
|
88
|
+
steady = steady,
|
|
89
|
+
active = active
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return result
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Any? -> Any?
|
|
102
|
+
* - ReadableMap -> Map<String, Any?>?
|
|
103
|
+
* - ReadableArray -> List<Any?>?
|
|
104
|
+
* - Number(Double/Float) -> Int/Long/Double (нормализация)
|
|
105
|
+
* - Boolean/String/Int/Long -> как есть
|
|
106
|
+
*/
|
|
107
|
+
fun toKotlinAny(value: Any?): Any? = when (value) {
|
|
108
|
+
null -> null
|
|
109
|
+
is ReadableMap -> toKotlinMap(value)
|
|
110
|
+
is ReadableArray -> toKotlinList(value)
|
|
111
|
+
is Boolean -> value
|
|
112
|
+
is String -> value
|
|
113
|
+
is Double -> normalizeNumber(value)
|
|
114
|
+
is Float -> normalizeNumber(value.toDouble())
|
|
115
|
+
is Int, is Long -> value
|
|
116
|
+
else -> value
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** ReadableMap[key] -> Kotlin Any? */
|
|
120
|
+
private fun readMapValue(map: ReadableMap, key: String): Any? {
|
|
121
|
+
return when (map.getType(key)) {
|
|
122
|
+
ReadableType.Null -> null
|
|
123
|
+
ReadableType.Boolean -> map.getBoolean(key)
|
|
124
|
+
ReadableType.String -> map.getString(key)
|
|
125
|
+
ReadableType.Number -> normalizeNumber(map.getDouble(key))
|
|
126
|
+
ReadableType.Map -> toKotlinMap(map.getMap(key))
|
|
127
|
+
ReadableType.Array -> toKotlinList(map.getArray(key))
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** ReadableArray[index] -> Kotlin Any? */
|
|
132
|
+
private fun readArrayValue(array: ReadableArray, index: Int): Any? {
|
|
133
|
+
return when (array.getType(index)) {
|
|
134
|
+
ReadableType.Null -> null
|
|
135
|
+
ReadableType.Boolean -> array.getBoolean(index)
|
|
136
|
+
ReadableType.String -> array.getString(index)
|
|
137
|
+
ReadableType.Number -> normalizeNumber(array.getDouble(index))
|
|
138
|
+
ReadableType.Map -> toKotlinMap(array.getMap(index))
|
|
139
|
+
ReadableType.Array -> toKotlinList(array.getArray(index))
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Double(JS number) -> Int/Long/Double
|
|
145
|
+
* - NaN/Infinity -> Double
|
|
146
|
+
* - целое -> Int если влезает, иначе Long
|
|
147
|
+
* - дробное -> Double
|
|
148
|
+
*/
|
|
149
|
+
private fun normalizeNumber(d: Double): Any {
|
|
150
|
+
if (!d.isFinite()) return d
|
|
151
|
+
|
|
152
|
+
val asLong = d.toLong()
|
|
153
|
+
val isInteger = d == asLong.toDouble()
|
|
154
|
+
if (!isInteger) return d
|
|
155
|
+
|
|
156
|
+
return if (asLong in Int.MIN_VALUE.toLong()..Int.MAX_VALUE.toLong()) {
|
|
157
|
+
asLong.toInt()
|
|
158
|
+
} else {
|
|
159
|
+
asLong
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// Converter.swift
|
|
2
|
+
import Foundation
|
|
3
|
+
|
|
4
|
+
enum Converter {
|
|
5
|
+
|
|
6
|
+
// MARK: - Public
|
|
7
|
+
|
|
8
|
+
/// Any? -> Any?
|
|
9
|
+
/// - NSNull -> nil
|
|
10
|
+
/// - NSNumber -> Bool / Int / Int64 / Double
|
|
11
|
+
/// - NSString -> String (and if it looks like JSON "{...}" or "[...]" it will be parsed into object)
|
|
12
|
+
/// - NSDictionary/NSArray -> recursively converted
|
|
13
|
+
/// - Date -> ISO8601 string
|
|
14
|
+
/// - Data -> base64 string
|
|
15
|
+
static func toAny(_ value: Any?) -> Any? {
|
|
16
|
+
guard let value else { return nil }
|
|
17
|
+
if value is NSNull { return nil }
|
|
18
|
+
|
|
19
|
+
// Bool may come as NSNumber, detect it explicitly.
|
|
20
|
+
if let b = value as? NSNumber, isCFBoolean(b) {
|
|
21
|
+
return b.boolValue
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if let n = value as? NSNumber {
|
|
25
|
+
return normalizeNumber(n)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if let s = value as? NSString {
|
|
29
|
+
return parseJSONIfPossible(String(s))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if let d = value as? Date {
|
|
33
|
+
return ISO8601DateFormatter().string(from: d)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if let data = value as? Data {
|
|
37
|
+
return data.base64EncodedString()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if let dict = value as? NSDictionary {
|
|
41
|
+
return toAnyDict(dict)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if let arr = value as? NSArray {
|
|
45
|
+
return toAnyArray(arr)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Swift-native containers (defensive).
|
|
49
|
+
if let dict = value as? [String: Any?] {
|
|
50
|
+
var out: [String: Any?] = [:]
|
|
51
|
+
out.reserveCapacity(dict.count)
|
|
52
|
+
for (k, v) in dict {
|
|
53
|
+
out[k] = toAny(v)
|
|
54
|
+
}
|
|
55
|
+
return out
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if let dict = value as? [String: Any] {
|
|
59
|
+
var out: [String: Any] = [:]
|
|
60
|
+
out.reserveCapacity(dict.count)
|
|
61
|
+
for (k, v) in dict {
|
|
62
|
+
out[k] = toAny(v) ?? NSNull()
|
|
63
|
+
}
|
|
64
|
+
return out
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if let arr = value as? [Any] {
|
|
68
|
+
return arr.map { toAny($0) ?? NSNull() }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Fallback: keep it safe for logging/bridge, but avoid relying on this.
|
|
72
|
+
return String(describing: value)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/// NSDictionary? -> [String: Any?]?
|
|
76
|
+
static func toAnyDict(_ dict: NSDictionary?) -> [String: Any?]? {
|
|
77
|
+
guard let dict, dict.count > 0 else { return nil }
|
|
78
|
+
var out: [String: Any?] = [:]
|
|
79
|
+
out.reserveCapacity(dict.count)
|
|
80
|
+
|
|
81
|
+
for (k, v) in dict {
|
|
82
|
+
guard let key = k as? String else { continue }
|
|
83
|
+
out[key] = toAny(v)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return out.isEmpty ? nil : out
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// NSArray? -> [Any]?
|
|
90
|
+
static func toAnyArray(_ arr: NSArray?) -> [Any]? {
|
|
91
|
+
guard let arr, arr.count > 0 else { return nil }
|
|
92
|
+
var out: [Any] = []
|
|
93
|
+
out.reserveCapacity(arr.count)
|
|
94
|
+
|
|
95
|
+
for item in arr {
|
|
96
|
+
out.append(toAny(item) ?? NSNull())
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return out.isEmpty ? nil : out
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// MARK: - UserDefaults helpers
|
|
103
|
+
|
|
104
|
+
/// Converts value into a type supported by UserDefaults:
|
|
105
|
+
/// String/Bool/Int/Double/Data/Date/Array/Dictionary (property list).
|
|
106
|
+
/// If it cannot be represented as a property list, it falls back to JSON string when possible.
|
|
107
|
+
static func toPropertyListValue(_ value: Any?) -> Any? {
|
|
108
|
+
guard let value = toAny(value) else { return nil }
|
|
109
|
+
|
|
110
|
+
// Supported plist primitives.
|
|
111
|
+
if value is String || value is Bool || value is Int || value is Int64 || value is Double || value is Float || value is Data || value is Date {
|
|
112
|
+
// Int64 is stored as NSNumber in UserDefaults.
|
|
113
|
+
if let i64 = value as? Int64 { return NSNumber(value: i64) }
|
|
114
|
+
if let f = value as? Float { return NSNumber(value: f) }
|
|
115
|
+
return value
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if let n = value as? NSNumber {
|
|
119
|
+
if isCFBoolean(n) { return n.boolValue }
|
|
120
|
+
return n
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if let arr = value as? [Any] {
|
|
124
|
+
let mapped: [Any] = arr.compactMap { element in
|
|
125
|
+
if element is NSNull { return nil }
|
|
126
|
+
return toPropertyListValue(element)
|
|
127
|
+
}
|
|
128
|
+
return mapped
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if let dict = value as? [String: Any?] {
|
|
132
|
+
var mapped: [String: Any] = [:]
|
|
133
|
+
mapped.reserveCapacity(dict.count)
|
|
134
|
+
for (k, v) in dict {
|
|
135
|
+
if let pv = toPropertyListValue(v) {
|
|
136
|
+
mapped[k] = pv
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return mapped
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if let dict = value as? [String: Any] {
|
|
143
|
+
var mapped: [String: Any] = [:]
|
|
144
|
+
mapped.reserveCapacity(dict.count)
|
|
145
|
+
for (k, v) in dict {
|
|
146
|
+
if let pv = toPropertyListValue(v) {
|
|
147
|
+
mapped[k] = pv
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return mapped
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Fallback: JSON string.
|
|
154
|
+
if JSONSerialization.isValidJSONObject(value) {
|
|
155
|
+
if let data = try? JSONSerialization.data(withJSONObject: value, options: []),
|
|
156
|
+
let json = String(data: data, encoding: .utf8) {
|
|
157
|
+
return json
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return String(describing: value)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// MARK: - Internals
|
|
165
|
+
|
|
166
|
+
private static func isCFBoolean(_ n: NSNumber) -> Bool {
|
|
167
|
+
CFGetTypeID(n) == CFBooleanGetTypeID()
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/// NSNumber(JS number) -> Int / Int64 / Double
|
|
171
|
+
/// - If it has fractional part -> Double
|
|
172
|
+
/// - Else Int if it fits, otherwise Int64
|
|
173
|
+
private static func normalizeNumber(_ n: NSNumber) -> Any {
|
|
174
|
+
let d = n.doubleValue
|
|
175
|
+
if !d.isFinite { return d }
|
|
176
|
+
|
|
177
|
+
let asInt64 = Int64(d)
|
|
178
|
+
let isInteger = d == Double(asInt64)
|
|
179
|
+
if !isInteger { return d }
|
|
180
|
+
|
|
181
|
+
if asInt64 >= Int64(Int.min) && asInt64 <= Int64(Int.max) {
|
|
182
|
+
return Int(asInt64)
|
|
183
|
+
} else {
|
|
184
|
+
return asInt64
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/// If a string looks like a JSON object/array, tries to parse and recursively normalize it.
|
|
189
|
+
private static func parseJSONIfPossible(_ s: String) -> Any {
|
|
190
|
+
let trimmed = s.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
191
|
+
guard !trimmed.isEmpty else { return "" }
|
|
192
|
+
|
|
193
|
+
guard let first = trimmed.first, first == "{" || first == "[" else {
|
|
194
|
+
return s
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
guard let data = trimmed.data(using: .utf8) else { return s }
|
|
198
|
+
guard let obj = try? JSONSerialization.jsonObject(with: data, options: []) else { return s }
|
|
199
|
+
|
|
200
|
+
// obj will be NSArray/NSDictionary -> normalize through the same converter.
|
|
201
|
+
return toAny(obj) ?? s
|
|
202
|
+
}
|
|
203
|
+
}
|
package/ios/Sdk.h
CHANGED
|
@@ -3,17 +3,18 @@
|
|
|
3
3
|
#ifdef __OBJC__
|
|
4
4
|
|
|
5
5
|
#import <Foundation/Foundation.h>
|
|
6
|
+
#import <React/RCTBridgeModule.h>
|
|
6
7
|
|
|
7
8
|
#ifdef RCT_NEW_ARCH_ENABLED
|
|
8
9
|
#import <Sdk/Sdk.h>
|
|
10
|
+
#endif
|
|
9
11
|
|
|
10
12
|
NS_EXTENSION_UNAVAILABLE_IOS("Altcraft RN bridge is unavailable in app extensions.")
|
|
13
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
11
14
|
@interface Sdk : NSObject <NativeSdkSpec>
|
|
12
15
|
#else
|
|
13
|
-
NS_EXTENSION_UNAVAILABLE_IOS("Altcraft RN bridge is unavailable in app extensions.")
|
|
14
16
|
@interface Sdk : NSObject
|
|
15
17
|
#endif
|
|
16
|
-
|
|
17
18
|
@end
|
|
18
19
|
|
|
19
|
-
#endif // __OBJC__
|
|
20
|
+
#endif // __OBJC__
|
package/ios/Sdk.mm
CHANGED
|
@@ -156,6 +156,23 @@ static void CallSetAppGroup(id instance, NSString * _Nullable groupName) {
|
|
|
156
156
|
func(instance, sel, groupName);
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
+
///Stores a value in UserDefaults via Swift module.
|
|
160
|
+
static void CallSetUserDefaultsValue(id instance,
|
|
161
|
+
NSString * _Nullable suiteName,
|
|
162
|
+
NSString *key,
|
|
163
|
+
id _Nullable value) {
|
|
164
|
+
if (!instance) return;
|
|
165
|
+
|
|
166
|
+
SEL sel = SdkSel(@"setUserDefaultsValueWithSuiteName:key:value:");
|
|
167
|
+
if (!SdkResponds(instance, sel)) return;
|
|
168
|
+
|
|
169
|
+
IMP imp = [instance methodForSelector:sel];
|
|
170
|
+
if (!imp) return;
|
|
171
|
+
|
|
172
|
+
void (*func)(id, SEL, id, id, id) = (void (*)(id, SEL, id, id, id))imp;
|
|
173
|
+
func(instance, sel, suiteName, key, value);
|
|
174
|
+
}
|
|
175
|
+
|
|
159
176
|
/// Deletes device token for a provider on Swift module.
|
|
160
177
|
static void CallDeleteDeviceToken(id instance, NSString * _Nullable provider, void (^completion)(BOOL ok)) {
|
|
161
178
|
if (!instance) { completion(NO); return; }
|
|
@@ -253,7 +270,7 @@ static void CallPushSubscription(id instance,
|
|
|
253
270
|
|
|
254
271
|
#pragma mark - Mobile Event calls
|
|
255
272
|
|
|
256
|
-
///
|
|
273
|
+
///Sends mobile event to Swift module (includes `utm`).
|
|
257
274
|
static void CallMobileEvent(id instance,
|
|
258
275
|
NSString *sid,
|
|
259
276
|
NSString *eventName,
|
|
@@ -261,19 +278,20 @@ static void CallMobileEvent(id instance,
|
|
|
261
278
|
NSDictionary * _Nullable payload,
|
|
262
279
|
NSDictionary * _Nullable matching,
|
|
263
280
|
NSString * _Nullable matchingType,
|
|
264
|
-
NSDictionary * _Nullable profileFields
|
|
281
|
+
NSDictionary * _Nullable profileFields,
|
|
282
|
+
NSDictionary * _Nullable utm) {
|
|
265
283
|
if (!instance) return;
|
|
266
284
|
|
|
267
|
-
SEL sel = SdkSel(@"mobileEvent:eventName:sendMessageId:payload:matching:matchingType:profileFields:");
|
|
285
|
+
SEL sel = SdkSel(@"mobileEvent:eventName:sendMessageId:payload:matching:matchingType:profileFields:utm:");
|
|
268
286
|
if (!SdkResponds(instance, sel)) return;
|
|
269
287
|
|
|
270
288
|
IMP imp = [instance methodForSelector:sel];
|
|
271
289
|
if (!imp) return;
|
|
272
290
|
|
|
273
|
-
void (*func)(id, SEL, id, id, id, id, id, id, id) =
|
|
274
|
-
(void (*)(id, SEL, id, id, id, id, id, id, id))imp;
|
|
291
|
+
void (*func)(id, SEL, id, id, id, id, id, id, id, id) =
|
|
292
|
+
(void (*)(id, SEL, id, id, id, id, id, id, id, id))imp;
|
|
275
293
|
|
|
276
|
-
func(instance, sel, sid, eventName, sendMessageId, payload, matching, matchingType, profileFields);
|
|
294
|
+
func(instance, sel, sid, eventName, sendMessageId, payload, matching, matchingType, profileFields, utm);
|
|
277
295
|
}
|
|
278
296
|
|
|
279
297
|
#pragma mark - Promise helpers
|
|
@@ -459,111 +477,6 @@ static void CallClear(id instance,
|
|
|
459
477
|
CallPromise0Args(instance, @"clearWithResolver:rejecter:", resolve, reject);
|
|
460
478
|
}
|
|
461
479
|
|
|
462
|
-
#pragma mark - Events: Swift -> NotificationCenter -> ObjC++ -> JS
|
|
463
|
-
|
|
464
|
-
/// Notification name used by Swift layer to publish events.
|
|
465
|
-
static NSString * const SdkEventsNotificationName = @"AltcraftSdkEventNotification";
|
|
466
|
-
|
|
467
|
-
static id _Nullable gSdkEventsObserver = nil;
|
|
468
|
-
static id<RCTCallableJSModules> _Nullable gCallableJSModules = nil;
|
|
469
|
-
|
|
470
|
-
/// Returns callableJSModules from a module instance if present.
|
|
471
|
-
static id<RCTCallableJSModules> _Nullable SdkGetCallableFromModule(id moduleInstance) {
|
|
472
|
-
if (!moduleInstance) return nil;
|
|
473
|
-
|
|
474
|
-
SEL sel = SdkSel(@"callableJSModules");
|
|
475
|
-
if (![moduleInstance respondsToSelector:sel]) return nil;
|
|
476
|
-
|
|
477
|
-
id (*msgSend)(id, SEL) = (id (*)(id, SEL))objc_msgSend;
|
|
478
|
-
id v = msgSend(moduleInstance, sel);
|
|
479
|
-
return (id<RCTCallableJSModules>)v;
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
/// Returns bridge from a module instance if present (Old Arch).
|
|
483
|
-
static id _Nullable SdkGetBridgeAny(id moduleInstance) {
|
|
484
|
-
if (!moduleInstance) return nil;
|
|
485
|
-
|
|
486
|
-
SEL sel = SdkSel(@"bridge");
|
|
487
|
-
if (![moduleInstance respondsToSelector:sel]) return nil;
|
|
488
|
-
|
|
489
|
-
id (*msgSend)(id, SEL) = (id (*)(id, SEL))objc_msgSend;
|
|
490
|
-
return msgSend(moduleInstance, sel);
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
/// Checks if bridge supports enqueueing JS calls.
|
|
494
|
-
static BOOL SdkBridgeCanEnqueue(id bridgeObj) {
|
|
495
|
-
if (!bridgeObj) return NO;
|
|
496
|
-
|
|
497
|
-
SEL sel = SdkSel(@"enqueueJSCall:method:args:completion:");
|
|
498
|
-
return [bridgeObj respondsToSelector:sel];
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
/// Emits event using bridge enqueue (Old Arch).
|
|
502
|
-
static void SdkBridgeEnqueueEmit(id bridgeObj, NSDictionary *payload) {
|
|
503
|
-
SEL sel = SdkSel(@"enqueueJSCall:method:args:completion:");
|
|
504
|
-
if (![bridgeObj respondsToSelector:sel]) return;
|
|
505
|
-
|
|
506
|
-
NSArray *args = @[@"AltcraftSdkEvent", payload ?: @{}];
|
|
507
|
-
|
|
508
|
-
void (*func)(id, SEL, id, id, id, id) = (void (*)(id, SEL, id, id, id, id))objc_msgSend;
|
|
509
|
-
func(bridgeObj, sel, @"RCTDeviceEventEmitter", @"emit", args, (id)nil);
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
/// Emits event to JS via callableJSModules (preferred) or bridge enqueue.
|
|
513
|
-
static void SdkEmitEventToJS(id moduleInstance, NSDictionary *payload) {
|
|
514
|
-
NSDictionary *safePayload = (payload && [payload isKindOfClass:[NSDictionary class]]) ? payload : @{};
|
|
515
|
-
|
|
516
|
-
// 1) New Arch: callableJSModules.
|
|
517
|
-
id<RCTCallableJSModules> callable = gCallableJSModules;
|
|
518
|
-
if (!callable) {
|
|
519
|
-
callable = SdkGetCallableFromModule(moduleInstance);
|
|
520
|
-
if (callable) gCallableJSModules = callable;
|
|
521
|
-
}
|
|
522
|
-
if (callable) {
|
|
523
|
-
[callable invokeModule:@"RCTDeviceEventEmitter"
|
|
524
|
-
method:@"emit"
|
|
525
|
-
withArgs:@[@"AltcraftSdkEvent", safePayload]];
|
|
526
|
-
return;
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
// 2) Old Arch: bridge enqueueJSCall.
|
|
530
|
-
id bridgeObj = SdkGetBridgeAny(moduleInstance);
|
|
531
|
-
if (SdkBridgeCanEnqueue(bridgeObj)) {
|
|
532
|
-
SdkBridgeEnqueueEmit(bridgeObj, safePayload);
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
/// Installs NotificationCenter observer to forward events to JS.
|
|
537
|
-
static void SdkInstallEventsObserverIfNeeded(id moduleInstance) {
|
|
538
|
-
// Refresh callable cache if possible.
|
|
539
|
-
id<RCTCallableJSModules> callable = SdkGetCallableFromModule(moduleInstance);
|
|
540
|
-
if (callable) gCallableJSModules = callable;
|
|
541
|
-
|
|
542
|
-
if (gSdkEventsObserver != nil) return;
|
|
543
|
-
|
|
544
|
-
gSdkEventsObserver =
|
|
545
|
-
[[NSNotificationCenter defaultCenter] addObserverForName:SdkEventsNotificationName
|
|
546
|
-
object:nil
|
|
547
|
-
queue:[NSOperationQueue mainQueue]
|
|
548
|
-
usingBlock:^(NSNotification *note) {
|
|
549
|
-
NSDictionary *payload = note.userInfo;
|
|
550
|
-
|
|
551
|
-
// Debug visibility for event flow (safe to keep).
|
|
552
|
-
NSLog(@"[AltcraftSdk] event %@", payload);
|
|
553
|
-
|
|
554
|
-
SdkEmitEventToJS(moduleInstance, payload);
|
|
555
|
-
}];
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
/// Removes NotificationCenter observer.
|
|
559
|
-
static void SdkRemoveEventsObserverIfNeeded(void) {
|
|
560
|
-
if (!gSdkEventsObserver) return;
|
|
561
|
-
|
|
562
|
-
[[NSNotificationCenter defaultCenter] removeObserver:gSdkEventsObserver];
|
|
563
|
-
gSdkEventsObserver = nil;
|
|
564
|
-
gCallableJSModules = nil;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
480
|
#pragma mark - Typed config -> NSDictionary (New Arch)
|
|
568
481
|
|
|
569
482
|
#ifdef RCT_NEW_ARCH_ENABLED
|
|
@@ -709,15 +622,11 @@ static NSDictionary *SdkConvertAltcraftConfigToNSDictionary(JS::NativeSdk::Altcr
|
|
|
709
622
|
/// Required by RN event emitter API (no-op).
|
|
710
623
|
- (void)removeListeners:(double)count { (void)count; }
|
|
711
624
|
|
|
712
|
-
#pragma mark - Events API
|
|
625
|
+
#pragma mark - Events API (Swift activates emitting; ObjC++ does NOT forward events)
|
|
713
626
|
|
|
714
|
-
/// Subscribes to SDK events
|
|
627
|
+
/// Subscribes to SDK events in Swift layer (idempotent).
|
|
715
628
|
- (void)subscribeToEvents
|
|
716
629
|
{
|
|
717
|
-
// Install observer and keep emitting to JS.
|
|
718
|
-
SdkInstallEventsObserverIfNeeded(self);
|
|
719
|
-
|
|
720
|
-
// Ask Swift to subscribe and start posting notifications.
|
|
721
630
|
id module = SdkAppModuleSharedInstance();
|
|
722
631
|
EnsureProvidersInstalled(module);
|
|
723
632
|
|
|
@@ -731,22 +640,20 @@ static NSDictionary *SdkConvertAltcraftConfigToNSDictionary(JS::NativeSdk::Altcr
|
|
|
731
640
|
func(module, sel);
|
|
732
641
|
}
|
|
733
642
|
|
|
734
|
-
/// Unsubscribes from SDK events
|
|
643
|
+
/// Unsubscribes from SDK events in Swift layer (idempotent).
|
|
735
644
|
- (void)unsubscribeFromEvent
|
|
736
645
|
{
|
|
737
646
|
id module = SdkAppModuleSharedInstance();
|
|
738
647
|
EnsureProvidersInstalled(module);
|
|
739
648
|
|
|
740
649
|
SEL sel = SdkSel(@"unsubscribeFromEvent");
|
|
741
|
-
if (SdkResponds(module, sel))
|
|
742
|
-
IMP imp = [module methodForSelector:sel];
|
|
743
|
-
if (imp) {
|
|
744
|
-
void (*func)(id, SEL) = (void (*)(id, SEL))imp;
|
|
745
|
-
func(module, sel);
|
|
746
|
-
}
|
|
747
|
-
}
|
|
650
|
+
if (!SdkResponds(module, sel)) return;
|
|
748
651
|
|
|
749
|
-
|
|
652
|
+
IMP imp = [module methodForSelector:sel];
|
|
653
|
+
if (!imp) return;
|
|
654
|
+
|
|
655
|
+
void (*func)(id, SEL) = (void (*)(id, SEL))imp;
|
|
656
|
+
func(module, sel);
|
|
750
657
|
}
|
|
751
658
|
|
|
752
659
|
#pragma mark - Common (JWT / AppGroup)
|
|
@@ -872,14 +779,15 @@ static NSDictionary *SdkConvertAltcraftConfigToNSDictionary(JS::NativeSdk::Altcr
|
|
|
872
779
|
|
|
873
780
|
#pragma mark - MobileEvent (void)
|
|
874
781
|
|
|
875
|
-
/// Sends a custom mobile event to backend.
|
|
782
|
+
/// Sends a custom mobile event to backend (now includes `utm`).
|
|
876
783
|
- (void)mobileEvent:(NSString *)sid
|
|
877
784
|
eventName:(NSString *)eventName
|
|
878
785
|
sendMessageId:(NSString * _Nullable)sendMessageId
|
|
879
|
-
payload:(NSDictionary *)payload
|
|
880
|
-
matching:(NSDictionary *)matching
|
|
786
|
+
payload:(NSDictionary * _Nullable)payload
|
|
787
|
+
matching:(NSDictionary * _Nullable)matching
|
|
881
788
|
matchingType:(NSString * _Nullable)matchingType
|
|
882
|
-
profileFields:(NSDictionary *)profileFields
|
|
789
|
+
profileFields:(NSDictionary * _Nullable)profileFields
|
|
790
|
+
utm:(NSDictionary * _Nullable)utm
|
|
883
791
|
{
|
|
884
792
|
id module = SdkAppModuleSharedInstance();
|
|
885
793
|
EnsureProvidersInstalled(module);
|
|
@@ -891,7 +799,8 @@ static NSDictionary *SdkConvertAltcraftConfigToNSDictionary(JS::NativeSdk::Altcr
|
|
|
891
799
|
(NSDictionary *)SdkNilIfNSNull(payload),
|
|
892
800
|
(NSDictionary *)SdkNilIfNSNull(matching),
|
|
893
801
|
(NSString *)SdkNilIfNSNull(matchingType),
|
|
894
|
-
(NSDictionary *)SdkNilIfNSNull(profileFields)
|
|
802
|
+
(NSDictionary *)SdkNilIfNSNull(profileFields),
|
|
803
|
+
(NSDictionary *)SdkNilIfNSNull(utm));
|
|
895
804
|
}
|
|
896
805
|
|
|
897
806
|
#pragma mark - Promises
|
|
@@ -1070,6 +979,39 @@ static NSDictionary *SdkConvertAltcraftConfigToNSDictionary(JS::NativeSdk::Altcr
|
|
|
1070
979
|
resolve((id)kCFNull);
|
|
1071
980
|
}
|
|
1072
981
|
|
|
982
|
+
#pragma mark - UserDefaults
|
|
983
|
+
|
|
984
|
+
/// We forward to Swift `SdkModule.setUserDefaultsValueWithSuiteName:key:value:`
|
|
985
|
+
/// so App Group logic is centralized in Swift and works consistently.
|
|
986
|
+
- (void)setUserDefaultsValue:(NSString * _Nullable)suiteName
|
|
987
|
+
key:(NSString *)key
|
|
988
|
+
value:(NSString * _Nullable)value
|
|
989
|
+
{
|
|
990
|
+
id module = SdkAppModuleSharedInstance();
|
|
991
|
+
EnsureProvidersInstalled(module);
|
|
992
|
+
|
|
993
|
+
NSString *sn = (NSString *)SdkNilIfNSNull(suiteName);
|
|
994
|
+
NSString *k = (NSString *)SdkNilIfNSNull(key);
|
|
995
|
+
id v = SdkNilIfNSNull(value); // value already NSString* or nil
|
|
996
|
+
|
|
997
|
+
if (!module) {
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
if (!k || k.length == 0) {
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
if (![NSThread isMainThread]) {
|
|
1006
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
1007
|
+
CallSetUserDefaultsValue(module, sn, k, v);
|
|
1008
|
+
});
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
CallSetUserDefaultsValue(module, sn, k, v);
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1073
1015
|
#pragma mark - iOS-only stubs (not implemented)
|
|
1074
1016
|
|
|
1075
1017
|
/// iOS stub: delivery tracking is handled by server/other platforms.
|
|
@@ -1091,7 +1033,7 @@ static NSDictionary *SdkConvertAltcraftConfigToNSDictionary(JS::NativeSdk::Altcr
|
|
|
1091
1033
|
}
|
|
1092
1034
|
|
|
1093
1035
|
/// iOS stub: retry control is managed in Swift layer / system scheduling.
|
|
1094
|
-
- (void)
|
|
1036
|
+
- (void)unlockInitOperationsInThisSession
|
|
1095
1037
|
{
|
|
1096
1038
|
// Intentionally not implemented on iOS.
|
|
1097
1039
|
}
|