@@ -49,7 +49,7 @@ class ReactIterableAPI: RCTEventEmitter {
4949 func initialize( apiKey: String , config configDict: [ AnyHashable : Any ] ) {
5050 ITBInfo ( )
5151 let launchOptions = createLaunchOptions ( )
52- let iterableConfig = ReactIterableAPI . createIterableConfig ( from: configDict)
52+ let iterableConfig = IterableConfig . from ( dict : configDict)
5353 if let urlDelegatePresent = configDict [ " urlDelegatePresent " ] as? Bool , urlDelegatePresent == true {
5454 iterableConfig. urlDelegate = self
5555 }
@@ -59,9 +59,6 @@ class ReactIterableAPI: RCTEventEmitter {
5959 if let inAppDelegatePresent = configDict [ " inAppDelegatePresent " ] as? Bool , inAppDelegatePresent == true {
6060 iterableConfig. inAppDelegate = self
6161 }
62- if let inAppDisplayInterval = configDict [ " inAppDisplayInterval " ] as? Double {
63- iterableConfig. inAppDisplayInterval = inAppDisplayInterval
64- }
6562
6663 DispatchQueue . main. async {
6764 IterableAPI . initialize ( apiKey: apiKey, launchOptions: launchOptions, config: iterableConfig)
@@ -136,7 +133,7 @@ class ReactIterableAPI: RCTEventEmitter {
136133 return
137134 }
138135
139- resolver ( attributionInfo . toDictionary ( ) )
136+ resolver ( SerializationUtil . encodableToDictionary ( encodable : attributionInfo ) )
140137 }
141138
142139 @objc ( setAttributionInfo: )
@@ -147,7 +144,7 @@ class ReactIterableAPI: RCTEventEmitter {
147144 return
148145 }
149146
150- IterableAPI . attributionInfo = dict . toDecodable ( )
147+ IterableAPI . attributionInfo = SerializationUtil . dictionaryToDecodable ( dict : dict )
151148 }
152149
153150 @objc ( trackPushOpenWithPayload: dataFields: )
@@ -172,14 +169,14 @@ class ReactIterableAPI: RCTEventEmitter {
172169 dataFields: [ AnyHashable : Any ] ? ) {
173170 ITBInfo ( )
174171 IterableAPI . track ( purchase: total,
175- items: items. compactMap ( ReactIterableAPI . dictionaryToCommerceItem ) ,
172+ items: items. compactMap ( CommerceItem . from ( dict : ) ) ,
176173 dataFields: dataFields)
177174 }
178175
179176 @objc ( getInAppMessages: rejecter: )
180177 func getInAppMessages( resolver: RCTPromiseResolveBlock , rejecter: RCTPromiseRejectBlock ) {
181178 ITBInfo ( )
182- resolver ( IterableAPI . inAppManager. getMessages ( ) . map { ReactIterableAPI . inAppMessageToDict ( message : $0 ) } )
179+ resolver ( IterableAPI . inAppManager. getMessages ( ) . map { $0 . toDict ( ) } )
183180 }
184181
185182 @objc ( trackEvent: )
@@ -217,101 +214,6 @@ class ReactIterableAPI: RCTEventEmitter {
217214 result [ UIApplication . LaunchOptionsKey. remoteNotification] = remoteNotification
218215 return result
219216 }
220-
221- private static func createIterableConfig( from dict: [ AnyHashable : Any ] ? ) -> IterableConfig {
222- let config = IterableConfig ( )
223- guard let dict = dict else {
224- return config
225- }
226-
227- if let pushIntegrationName = dict [ " pushIntegrationName " ] as? String {
228- config. pushIntegrationName = pushIntegrationName
229- }
230- if let sandboxPushIntegrationName = dict [ " sandboxPushIntegrationName " ] as? String {
231- config. sandboxPushIntegrationName = sandboxPushIntegrationName
232- }
233- if let intValue = dict [ " pushPlatform " ] as? Int , let pushPlatform = PushServicePlatform ( rawValue: intValue) {
234- config. pushPlatform = pushPlatform
235- }
236- if let autoPushRegistration = dict [ " autoPushRegistration " ] as? Bool {
237- config. autoPushRegistration = autoPushRegistration
238- }
239- if let checkForDeferredDeeplink = dict [ " checkForDeferredDeeplink " ] as? Bool {
240- config. checkForDeferredDeeplink = checkForDeferredDeeplink
241- }
242-
243- return config
244- }
245-
246- private static func dictionaryToCommerceItem( dict: [ AnyHashable : Any ] ) -> CommerceItem ? {
247- guard let id = dict [ " id " ] as? String else {
248- return nil
249- }
250- guard let name = dict [ " name " ] as? String else {
251- return nil
252- }
253- guard let price = dict [ " price " ] as? NSNumber else {
254- return nil
255- }
256- guard let quantity = dict [ " quantity " ] as? UInt else {
257- return nil
258- }
259-
260- return CommerceItem ( id: id, name: name, price: price, quantity: quantity)
261- }
262-
263- private static func inAppTriggerToDict( trigger: IterableInAppTrigger ) -> [ AnyHashable : Any ] {
264- var dict = [ AnyHashable: Any] ( )
265- dict [ " type " ] = trigger. type. rawValue
266- return dict
267- }
268-
269- private static func edgeInsetsToDict( edgeInsets: UIEdgeInsets ) -> [ AnyHashable : Any ] {
270- var dict = [ AnyHashable: Any] ( )
271- dict [ " top " ] = edgeInsets. top
272- dict [ " left " ] = edgeInsets. left
273- dict [ " bottom " ] = edgeInsets. bottom
274- dict [ " right " ] = edgeInsets. right
275- return dict
276- }
277-
278- private static func inAppMessageToDict( message: IterableInAppMessage ) -> [ AnyHashable : Any ] {
279- var dict = [ AnyHashable: Any] ( )
280- dict [ " messageId " ] = message. messageId
281- dict [ " campaignId " ] = message. campaignId
282- dict [ " trigger " ] = inAppTriggerToDict ( trigger: message. trigger)
283- dict [ " createdAt " ] = message. createdAt. map { $0. iterableIntValue }
284- dict [ " expiresAt " ] = message. expiresAt. map { $0. iterableIntValue }
285- dict [ " saveToInbox " ] = message. saveToInbox
286- dict [ " inboxMetadata " ] = inboxMetadataToDict ( metadata: message. inboxMetadata)
287- dict [ " customPayload " ] = message. customPayload
288- dict [ " read " ] = message. read
289- return dict
290- }
291-
292- private static func inAppContentToDict( content: IterableInAppContent ) -> [ AnyHashable : Any ] {
293- guard let htmlInAppContent = content as? IterableHtmlInAppContent else {
294- return [ : ]
295- }
296-
297- var dict = [ AnyHashable: Any] ( )
298- dict [ " type " ] = htmlInAppContent. type. rawValue
299- dict [ " edgeInsets " ] = edgeInsetsToDict ( edgeInsets: htmlInAppContent. edgeInsets)
300- dict [ " backgroundAlpha " ] = htmlInAppContent. backgroundAlpha
301- dict [ " html " ] = htmlInAppContent. html
302- return dict
303- }
304-
305- private static func inboxMetadataToDict( metadata: IterableInboxMetadata ? ) -> [ AnyHashable : Any ] ? {
306- guard let metadata = metadata else {
307- return nil
308- }
309- var dict = [ AnyHashable: Any] ( )
310- dict [ " title " ] = metadata. title
311- dict [ " subtitle " ] = metadata. subtitle
312- dict [ " icon " ] = metadata. icon
313- return dict
314- }
315217}
316218
317219extension ReactIterableAPI : IterableURLDelegate {
@@ -372,7 +274,7 @@ extension ReactIterableAPI: IterableInAppDelegate {
372274 return . show
373275 }
374276
375- let messageDict = ReactIterableAPI . inAppMessageToDict ( message : message )
277+ let messageDict = message . toDict ( )
376278 sendEvent ( withName: EventName . handleInAppCalled. rawValue, body: messageDict)
377279 let timeoutResult = inAppDelegateSemapohore. wait ( timeout: . now( ) + 2.0 )
378280
@@ -386,41 +288,3 @@ extension ReactIterableAPI: IterableInAppDelegate {
386288 }
387289}
388290
389- // TODO: make public
390- extension Encodable {
391- public func toDictionary( ) -> [ String : Any ] ? {
392- guard let data = try ? JSONEncoder ( ) . encode ( self ) else {
393- return nil
394- }
395-
396- return try ? JSONSerialization . jsonObject ( with: data, options: [ . allowFragments] ) as? [ String : Any ]
397- }
398- }
399-
400- // TODO: make public
401- extension Dictionary where Key == AnyHashable , Value == Any {
402- public func toDecodable< T> ( ) -> T ? where T: Decodable {
403- guard let data = try ? JSONSerialization . data ( withJSONObject: self , options: [ ] ) else {
404- return nil
405- }
406-
407- return try ? JSONDecoder ( ) . decode ( T . self, from: data)
408-
409- }
410- }
411-
412- // TODO: make public
413- extension Date {
414- public var iterableIntValue : Int {
415- return Int ( self . timeIntervalSince1970 * 1000 )
416- }
417- }
418-
419- // TODO: make public
420- extension Int {
421- public var iterableDateValue : Date {
422- let seconds = Double ( self ) / 1000.0 // ms -> seconds
423-
424- return Date ( timeIntervalSince1970: seconds)
425- }
426- }
0 commit comments