@@ -116,9 +116,9 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
116
116
return self . register ( token: token, appName: appName, pushServicePlatform: self . config. pushPlatform)
117
117
} ) . onSuccess { ( json) in
118
118
onSuccess ? ( json)
119
- } . onFailure { ( error) in
119
+ } . onError { ( error) in
120
120
if let sendError = error as? SendRequestError {
121
- onFailure ? ( sendError. errorMessage , sendError. data)
121
+ onFailure ? ( sendError. reason , sendError. data)
122
122
} else {
123
123
onFailure ? ( " failed to create user " , nil )
124
124
}
@@ -132,7 +132,7 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
132
132
guard email != nil || userId != nil else {
133
133
ITBError ( " Both email and userId are nil " )
134
134
onFailure ? ( " Both email and userId are nil " , nil )
135
- return SendRequestError . createFailedFuture ( reason: " Both email and userId are nil " )
135
+ return SendRequestError . createErroredFuture ( reason: " Both email and userId are nil " )
136
136
}
137
137
138
138
hexToken = ( token as NSData ) . iteHexadecimalString ( )
@@ -178,7 +178,7 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
178
178
ITBInfo ( " sending registerToken request with args \( args) " )
179
179
return
180
180
createPostRequest ( forPath: . ITBL_PATH_REGISTER_DEVICE_TOKEN, withBody: args)
181
- . map { sendRequest ( $0, onSuccess: onSuccess, onFailure: onFailure) } ?? SendRequestError . createFailedFuture ( reason: " Couldn't create register request " )
181
+ . map { sendRequest ( $0, onSuccess: onSuccess, onFailure: onFailure) } ?? SendRequestError . createErroredFuture ( reason: " Couldn't create register request " )
182
182
}
183
183
184
184
func disableDeviceForCurrentUser( ) {
@@ -242,7 +242,7 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
242
242
243
243
return createPostRequest ( forPath: . ITBL_PATH_CREATE_USER, withBody: args) . map {
244
244
sendRequest ( $0)
245
- } ?? SendRequestError . createFailedFuture ( reason: " Could not create createUser Reqeust " )
245
+ } ?? SendRequestError . createErroredFuture ( reason: " Could not create createUser Reqeust " )
246
246
}
247
247
248
248
func trackPurchase( _ total: NSNumber , items: [ CommerceItem ] ) {
@@ -399,56 +399,34 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
399
399
}
400
400
}
401
401
402
- func spawn( inAppNotification callbackBlock: ITEActionBlock ? ) {
403
- let onSuccess : OnSuccessHandler = { payload in
404
- guard let payload = payload else {
405
- return
406
- }
407
- guard let dialogOptions = IterableInAppManager . getNextMessageFromPayload ( payload) else {
408
- ITBError ( " No notifications found for inApp payload \( payload) " )
409
- return
410
- }
411
- guard let message = dialogOptions [ . ITBL_IN_APP_CONTENT] as? [ AnyHashable : Any ] else {
412
- return
413
- }
414
- guard let messageId = dialogOptions [ . ITBL_KEY_MESSAGE_ID] as? String else {
415
- return
416
- }
417
- guard let html = message [ . ITBL_IN_APP_HTML] as? String else {
418
- return
419
- }
420
- guard html. range ( of: AnyHashable . ITBL_IN_APP_HREF, options: [ . caseInsensitive] ) != nil else {
421
- ITBError ( " No href tag found in in-app html payload \( html) " )
422
- self . inAppConsume ( messageId)
423
- return
424
- }
425
-
426
- let inAppDisplaySettings = message [ . ITBL_IN_APP_DISPLAY_SETTINGS] as? [ AnyHashable : Any ]
427
- let backgroundAlpha = IterableInAppManager . getBackgroundAlpha ( fromInAppSettings: inAppDisplaySettings)
428
- let edgeInsets = IterableInAppManager . getPaddingFromPayload ( inAppDisplaySettings)
429
-
430
- let notificationMetadata = IterableNotificationMetadata . metadata ( fromInAppOptions: messageId)
431
-
432
- DispatchQueue . main. async {
433
- let opened = IterableInAppManager . showIterableNotificationHTML ( html, trackParams: notificationMetadata, backgroundAlpha: backgroundAlpha, padding: edgeInsets, callbackBlock: callbackBlock)
434
- if opened {
435
- self . inAppConsume ( messageId)
402
+ @discardableResult func spawn( inAppNotification callbackBlock: ITEActionBlock ? ) -> Future < IterableInAppManager . ShowInAppResult > {
403
+ return getInAppMessages ( 1 )
404
+ . map { IterableInAppManager . parseInApp ( fromPayload: $0) }
405
+ . flatMap { IterableInAppManager . showInApp ( parseResult: $0, callbackBlock: callbackBlock) }
406
+ . onSuccess {
407
+ switch $0 {
408
+ case . success( opened: let opened, messageId: let messageId) :
409
+ if opened {
410
+ self . inAppConsume ( messageId)
411
+ }
412
+ case . failure( reason: let reason, messageId: let messageId) :
413
+ if let messageId = messageId {
414
+ self . inAppConsume ( messageId)
415
+ }
416
+ ITBError ( reason)
436
417
}
437
418
}
438
- }
439
-
440
- getInAppMessages ( 1 , onSuccess: onSuccess, onFailure: IterableAPIInternal . defaultOnFailure ( identifier: " getInAppMessages " ) )
441
419
}
442
420
443
- func getInAppMessages( _ count: NSNumber ) {
444
- getInAppMessages ( count, onSuccess: IterableAPIInternal . defaultOnSucess ( identifier: " getMessages " ) , onFailure: IterableAPIInternal . defaultOnFailure ( identifier: " getMessages " ) )
421
+ @ discardableResult func getInAppMessages( _ count: NSNumber ) -> Future < SendRequestValue > {
422
+ return getInAppMessages ( count, onSuccess: IterableAPIInternal . defaultOnSucess ( identifier: " getMessages " ) , onFailure: IterableAPIInternal . defaultOnFailure ( identifier: " getMessages " ) )
445
423
}
446
424
447
- func getInAppMessages( _ count: NSNumber , onSuccess: OnSuccessHandler ? , onFailure: OnFailureHandler ? ) {
425
+ @ discardableResult func getInAppMessages( _ count: NSNumber , onSuccess: OnSuccessHandler ? , onFailure: OnFailureHandler ? ) -> Future < SendRequestValue > {
448
426
guard email != nil || userId != nil else {
449
427
ITBError ( " Both email and userId are nil " )
450
428
onFailure ? ( " Both email and userId are nil " , nil )
451
- return
429
+ return SendRequestError . createErroredFuture ( reason : " Both email and userId are nil " )
452
430
}
453
431
454
432
var args : [ AnyHashable : Any ] = [
@@ -459,9 +437,9 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
459
437
460
438
addEmailOrUserId ( args: & args)
461
439
462
- if let request = createGetRequest ( forPath: . ITBL_PATH_GET_INAPP_MESSAGES, withArgs: args as! [ String : String ] ) {
463
- sendRequest ( request , onSuccess: onSuccess, onFailure: onFailure)
464
- }
440
+ return createGetRequest ( forPath: . ITBL_PATH_GET_INAPP_MESSAGES, withArgs: args as! [ String : String ] ) . map {
441
+ sendRequest ( $0 , onSuccess: onSuccess, onFailure: onFailure)
442
+ } ?? SendRequestError . createErroredFuture ( reason : " Could not create get request for getInApp " )
465
443
}
466
444
467
445
func trackInAppOpen( _ messageId: String ) {
@@ -533,9 +511,9 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
533
511
@discardableResult func sendRequest( _ request: URLRequest , onSuccess: OnSuccessHandler ? = nil , onFailure: OnFailureHandler ? = nil ) -> Future < SendRequestValue > {
534
512
return NetworkHelper . sendRequest ( request, usingSession: networkSession) . onSuccess { ( json) in
535
513
onSuccess ? ( json)
536
- } . onFailure { ( failureInfo ) in
537
- if let sendError = failureInfo as? SendRequestError {
538
- onFailure ? ( sendError. errorMessage , sendError. data)
514
+ } . onError { ( error ) in
515
+ if let sendError = error as? SendRequestError {
516
+ onFailure ? ( sendError. reason , sendError. data)
539
517
} else {
540
518
onFailure ? ( " send request failed " , nil )
541
519
}
@@ -796,9 +774,9 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
796
774
797
775
NetworkHelper . sendRequest ( request, usingSession: networkSession) . onSuccess { ( json) in
798
776
self . handleDDL ( json: json)
799
- } . onFailure { ( failureInfo ) in
800
- if let sendError = failureInfo as? SendRequestError , let errorMessage = sendError. errorMessage {
801
- ITBError ( errorMessage )
777
+ } . onError { ( error ) in
778
+ if let sendError = error as? SendRequestError , let reason = sendError. reason {
779
+ ITBError ( reason )
802
780
} else {
803
781
ITBError ( " failed to send handleDDl request " )
804
782
}
0 commit comments