-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathReactIterableAPI.swift
More file actions
296 lines (249 loc) · 9.79 KB
/
Copy pathReactIterableAPI.swift
File metadata and controls
296 lines (249 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
// Created by Tapash Majumder on 3/19/20.
// Copyright © 2020 Iterable. All rights reserved.
//
import Foundation
import IterableSDK;
@objc(ReactIterableAPI)
class ReactIterableAPI: RCTEventEmitter {
@objc static override func moduleName() -> String! {
return "RNIterableAPI"
}
override var methodQueue: DispatchQueue! {
_methodQueue
}
@objc override static func requiresMainQueueSetup() -> Bool {
false
}
enum EventName: String, CaseIterable {
case handleUrlCalled
case handleCustomActionCalled
case handleInAppCalled
}
override func supportedEvents() -> [String]! {
var result = [String]()
EventName.allCases.forEach {
result.append($0.rawValue)
}
return result
}
override func startObserving() {
ITBInfo()
shouldEmit = true
}
override func stopObserving() {
ITBInfo()
shouldEmit = false
}
@objc(initializeWithApiKey:config:)
func initialize(apiKey: String, config configDict: [AnyHashable: Any]) {
ITBInfo()
let launchOptions = createLaunchOptions()
let iterableConfig = IterableConfig.from(dict: configDict)
if let urlDelegatePresent = configDict["urlDelegatePresent"] as? Bool, urlDelegatePresent == true {
iterableConfig.urlDelegate = self
}
if let customActionDelegatePresent = configDict["customActionDelegatePresent"] as? Bool, customActionDelegatePresent == true {
iterableConfig.customActionDelegate = self
}
if let inAppDelegatePresent = configDict["inAppDelegatePresent"] as? Bool, inAppDelegatePresent == true {
iterableConfig.inAppDelegate = self
}
DispatchQueue.main.async {
IterableAPI.initialize(apiKey: apiKey, launchOptions: launchOptions, config: iterableConfig)
}
}
@objc(setEmail:)
func set(email: String?) {
ITBInfo()
IterableAPI.email = email
}
@objc(getEmail:rejecter:)
func getEmail(resolver: RCTPromiseResolveBlock, rejecter: RCTPromiseRejectBlock) {
ITBInfo()
resolver(IterableAPI.email)
}
@objc(setUserId:)
func set(userId: String?) {
ITBInfo()
IterableAPI.userId = userId
}
@objc(getUserId:rejecter:)
func getUserId(resolver: RCTPromiseResolveBlock, rejecter: RCTPromiseRejectBlock) {
ITBInfo()
resolver(IterableAPI.userId)
}
@objc(setUrlHandled:)
func set(urlHandled: Bool) {
ITBInfo()
self.urlHandled = urlHandled
urlDelegateSemaphore.signal()
}
@objc(setInAppShowResponse:)
func set(inAppShowResponse number: NSNumber) {
ITBInfo()
self.inAppShowResponse = InAppShowResponse.from(number: number)
inAppDelegateSemapohore.signal()
}
@objc(disableDeviceForCurrentUser)
func disableDeviceForCurrentUser() {
ITBInfo()
IterableAPI.disableDeviceForCurrentUser()
}
@objc(disableDeviceForAllUsers)
func disableDeviceForAllUsers() {
ITBInfo()
IterableAPI.disableDeviceForAllUsers()
}
@objc(getLastPushPayload:rejecter:)
func getLastPushPayload(resolver: RCTPromiseResolveBlock, rejecter: RCTPromiseRejectBlock) {
ITBInfo()
resolver(IterableAPI.lastPushPayload)
}
@objc(getAttributionInfo:rejecter:)
func getAttributionInfo(resolver: RCTPromiseResolveBlock, rejecter: RCTPromiseRejectBlock) {
ITBInfo()
guard let attributionInfo = IterableAPI.attributionInfo else {
resolver(NSNull())
return
}
resolver(SerializationUtil.encodableToDictionary(encodable: attributionInfo))
}
@objc(setAttributionInfo:)
func set(attributionInfo dict: [AnyHashable: Any]?) {
ITBInfo()
guard let dict = dict else {
IterableAPI.attributionInfo = nil
return
}
IterableAPI.attributionInfo = SerializationUtil.dictionaryToDecodable(dict: dict)
}
@objc(trackPushOpenWithPayload:dataFields:)
func trackPushOpen(payload: [AnyHashable: Any], dataFields: [AnyHashable: Any]?) {
ITBInfo()
IterableAPI.track(pushOpen: payload, dataFields: dataFields)
}
@objc(trackPushOpenWithCampaignId:templateId:messageId:appAlreadyRunning:dataFields:)
func trackPushOpen(campaignId: NSNumber,
templateId: NSNumber?,
messageId: String?,
appAlreadyRunning: Bool,
dataFields: [AnyHashable: Any]?) {
ITBInfo()
IterableAPI.track(pushOpen: campaignId, templateId: templateId, messageId: messageId, appAlreadyRunning: appAlreadyRunning, dataFields: dataFields)
}
@objc(trackPurchaseWithTotal:items:dataFields:)
func trackPurchase(total: NSNumber,
items: [[AnyHashable: Any]],
dataFields: [AnyHashable: Any]?) {
ITBInfo()
IterableAPI.track(purchase: total,
items: items.compactMap(CommerceItem.from(dict:)),
dataFields: dataFields)
}
@objc(trackInAppOpenWithMessageId:location:)
func trackInAppOpen(messageId: String, location number: NSNumber) {
ITBInfo()
guard let message = IterableAPI.inAppManager.getMessage(withId: messageId) else {
ITBError("Could not find message with id: \(messageId)")
return
}
IterableAPI.track(inAppOpen: message, location: InAppLocation.from(number: number))
}
@objc(getInAppMessages:rejecter:)
func getInAppMessages(resolver: RCTPromiseResolveBlock, rejecter: RCTPromiseRejectBlock) {
ITBInfo()
resolver(IterableAPI.inAppManager.getMessages().map{ $0.toDict() })
}
@objc(trackEvent:dataFields:)
func track(event: String, dataFields: [AnyHashable: Any]?) {
ITBInfo()
IterableAPI.track(event: event, dataFields)
}
private var shouldEmit = false
private let _methodQueue = DispatchQueue(label: String(describing: ReactIterableAPI.self))
// Handling url delegate
private var urlHandled = false
private var urlDelegateSemaphore = DispatchSemaphore(value: 0)
// Handling in-app delegate
private var inAppShowResponse = InAppShowResponse.show
private var inAppDelegateSemapohore = DispatchSemaphore(value: 0)
private func createLaunchOptions() -> [UIApplication.LaunchOptionsKey: Any]? {
guard let bridge = bridge else {
return nil
}
return ReactIterableAPI.createLaunchOptions(bridgeLaunchOptions: bridge.launchOptions)
}
private static func createLaunchOptions(bridgeLaunchOptions: [AnyHashable: Any]?) -> [UIApplication.LaunchOptionsKey: Any]? {
guard let bridgeLaunchOptions = bridgeLaunchOptions,
let remoteNotification = bridgeLaunchOptions[UIApplication.LaunchOptionsKey.remoteNotification.rawValue] else {
return nil
}
var result = [UIApplication.LaunchOptionsKey: Any]()
result[UIApplication.LaunchOptionsKey.remoteNotification] = remoteNotification
return result
}
}
extension ReactIterableAPI: IterableURLDelegate {
func handle(iterableURL url: URL, inContext context: IterableActionContext) -> Bool {
ITBInfo()
guard shouldEmit == true else {
return false
}
let contextDict = ReactIterableAPI.contextToDictionary(context: context)
sendEvent(withName: EventName.handleUrlCalled.rawValue, body: ["url": url.absoluteString, "context": contextDict])
let timeoutResult = urlDelegateSemaphore.wait(timeout: .now() + 2.0)
if timeoutResult == .success {
ITBInfo("urlHandled: \(urlHandled)")
return urlHandled
} else {
ITBInfo("timed out")
return false
}
}
private static func contextToDictionary(context: IterableActionContext) -> [AnyHashable: Any] {
var result = [AnyHashable: Any]()
let actionDict = actionToDictionary(action: context.action)
result["action"] = actionDict
result["source"] = context.source.rawValue
return result
}
private static func actionToDictionary(action: IterableAction) -> [AnyHashable: Any] {
var actionDict = [AnyHashable: Any]()
actionDict["type"] = action.type
if let data = action.data {
actionDict["data"] = data
}
if let userInput = action.userInput {
actionDict["userInput"] = userInput
}
return actionDict
}
}
extension ReactIterableAPI: IterableCustomActionDelegate {
func handle(iterableCustomAction action: IterableAction, inContext context: IterableActionContext) -> Bool {
ITBInfo()
let actionDict = ReactIterableAPI.actionToDictionary(action: action)
let contextDict = ReactIterableAPI.contextToDictionary(context: context)
sendEvent(withName: EventName.handleCustomActionCalled.rawValue, body: ["action": actionDict, "context": contextDict])
return true
}
}
extension ReactIterableAPI: IterableInAppDelegate {
func onNew(message: IterableInAppMessage) -> InAppShowResponse {
ITBInfo()
guard shouldEmit == true else {
return .show
}
let messageDict = message.toDict()
sendEvent(withName: EventName.handleInAppCalled.rawValue, body: messageDict)
let timeoutResult = inAppDelegateSemapohore.wait(timeout: .now() + 2.0)
if timeoutResult == .success {
ITBInfo("inAppShowResponse: \(inAppShowResponse == .show)")
return inAppShowResponse
} else {
ITBInfo("timed out")
return .show
}
}
}