Skip to content

Commit eae48f7

Browse files
committed
refactor: enhance ReactIterableAPI by implementing URL, custom action, in-app, and auth delegates; improve event handling and logging
1 parent 658f44f commit eae48f7

1 file changed

Lines changed: 139 additions & 140 deletions

File tree

ios/RNIterableAPI/ReactIterableAPI.swift

Lines changed: 139 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ open class ReactIterableAPI: RCTEventEmitter {
6969
}
7070

7171
@objc(initializeWithApiKey:config:)
72-
public func initializeWithApiKey(apiKey: String, config: NSDictionary) {
72+
func initializeWithApiKey(apiKey: String, config: NSDictionary) {
7373
NSLog("initialize called from swift")
7474
ITBInfo()
7575

@@ -520,44 +520,43 @@ open class ReactIterableAPI: RCTEventEmitter {
520520
let launchOptions = createLaunchOptions()
521521
let iterableConfig = IterableConfig.from(dict: configDict)
522522

523-
// TODO: add this back in later
524-
// if let urlHandlerPresent = configDict["urlHandlerPresent"] as? Bool, urlHandlerPresent == true {
525-
// iterableConfig.urlDelegate = self
526-
// }
523+
if let urlHandlerPresent = configDict["urlHandlerPresent"] as? Bool, urlHandlerPresent == true {
524+
iterableConfig.urlDelegate = self
525+
}
527526

528-
// if let customActionHandlerPresent = configDict["customActionHandlerPresent"] as? Bool,
529-
// customActionHandlerPresent == true
530-
// {
531-
// iterableConfig.customActionDelegate = self
532-
// }
527+
if let customActionHandlerPresent = configDict["customActionHandlerPresent"] as? Bool,
528+
customActionHandlerPresent == true
529+
{
530+
iterableConfig.customActionDelegate = self
531+
}
533532

534-
// if let inAppHandlerPresent = configDict["inAppHandlerPresent"] as? Bool,
535-
// inAppHandlerPresent == true
536-
// {
537-
// iterableConfig.inAppDelegate = self
538-
// }
533+
if let inAppHandlerPresent = configDict["inAppHandlerPresent"] as? Bool,
534+
inAppHandlerPresent == true
535+
{
536+
iterableConfig.inAppDelegate = self
537+
}
539538

540-
// if let authHandlerPresent = configDict["authHandlerPresent"] as? Bool, authHandlerPresent {
541-
// iterableConfig.authDelegate = self
542-
// }
539+
if let authHandlerPresent = configDict["authHandlerPresent"] as? Bool, authHandlerPresent {
540+
iterableConfig.authDelegate = self
541+
}
543542

544543
// connect new inbox in-app payloads to the RN SDK
545-
// NotificationCenter.default.addObserver(
546-
// self, selector: #selector(receivedIterableInboxChanged),
547-
// name: Notification.Name.iterableInboxChanged, object: nil)
548-
549-
// DispatchQueue.main.async {
550-
// IterableAPI.initialize2(
551-
// apiKey: apiKey,
552-
// launchOptions: launchOptions,
553-
// config: iterableConfig,
554-
// apiEndPointOverride: apiEndPointOverride
555-
// ) { result in
556-
// resolver(result)
557-
// }
544+
NotificationCenter.default.addObserver(
545+
self, selector: #selector(receivedIterableInboxChanged),
546+
name: Notification.Name.iterableInboxChanged, object: nil)
558547

559-
// IterableAPI.setDeviceAttribute(name: "reactNativeSDKVersion", value: version)
560-
// }
548+
DispatchQueue.main.async {
549+
IterableAPI.initialize2(
550+
apiKey: apiKey,
551+
launchOptions: launchOptions,
552+
config: iterableConfig,
553+
apiEndPointOverride: apiEndPointOverride
554+
) { result in
555+
resolver(result)
556+
}
557+
558+
IterableAPI.setDeviceAttribute(name: "reactNativeSDKVersion", value: version)
559+
}
561560
}
562561

563562
@objc(receivedIterableInboxChanged)
@@ -590,110 +589,110 @@ open class ReactIterableAPI: RCTEventEmitter {
590589
}
591590
}
592591

593-
// extension ReactIterableAPI: IterableURLDelegate {
594-
// public func handle(iterableURL url: URL, inContext context: IterableActionContext) -> Bool {
595-
// ITBInfo()
596-
// guard shouldEmit else {
597-
// return false
598-
// }
599-
// let contextDict = ReactIterableAPI.contextToDictionary(context: context)
600-
// sendEvent(
601-
// withName: EventName.handleUrlCalled.rawValue,
602-
// body: [
603-
// "url": url.absoluteString,
604-
// "context": contextDict,
605-
// ] as [String: Any])
606-
// return true
607-
// }
608-
609-
// private static func contextToDictionary(context: IterableActionContext) -> [AnyHashable: Any] {
610-
// var result = [AnyHashable: Any]()
611-
// let actionDict = actionToDictionary(action: context.action)
612-
// result["action"] = actionDict
613-
// result["source"] = context.source.rawValue
614-
// return result
615-
// }
616-
617-
// private static func actionToDictionary(action: IterableAction) -> [AnyHashable: Any] {
618-
// var actionDict = [AnyHashable: Any]()
619-
// actionDict["type"] = action.type
620-
// if let data = action.data {
621-
// actionDict["data"] = data
622-
// }
623-
// if let userInput = action.userInput {
624-
// actionDict["userInput"] = userInput
625-
// }
626-
// return actionDict
627-
// }
628-
// }
629-
630-
// extension ReactIterableAPI: IterableCustomActionDelegate {
631-
// public func handle(
632-
// iterableCustomAction action: IterableAction, inContext context: IterableActionContext
633-
// )
634-
// -> Bool
635-
// {
636-
// ITBInfo()
637-
// let actionDict = ReactIterableAPI.actionToDictionary(action: action)
638-
// let contextDict = ReactIterableAPI.contextToDictionary(context: context)
639-
// sendEvent(
640-
// withName: EventName.handleCustomActionCalled.rawValue,
641-
// body: [
642-
// "action": actionDict,
643-
// "context": contextDict,
644-
// ])
645-
// return true
646-
// }
647-
// }
648-
649-
// extension ReactIterableAPI: IterableInAppDelegate {
650-
// public func onNew(message: IterableInAppMessage) -> InAppShowResponse {
651-
// ITBInfo()
652-
// guard shouldEmit else {
653-
// return .show
654-
// }
655-
// sendEvent(
656-
// withName: EventName.handleInAppCalled.rawValue,
657-
// body: message.toDict())
658-
// let timeoutResult = inAppHandlerSemaphore.wait(timeout: .now() + 2.0)
659-
// if timeoutResult == .success {
660-
// ITBInfo("inAppShowResponse: \(inAppShowResponse == .show)")
661-
// return inAppShowResponse
662-
// } else {
663-
// ITBInfo("timed out")
664-
// return .show
665-
// }
666-
// }
667-
// }
668-
669-
// extension ReactIterableAPI: IterableAuthDelegate {
670-
// public func onAuthTokenRequested(completion: @escaping AuthTokenRetrievalHandler) {
671-
// ITBInfo()
672-
// DispatchQueue.global(qos: .userInitiated).async {
673-
// self.sendEvent(
674-
// withName: EventName.handleAuthCalled.rawValue,
675-
// body: nil as Any?)
676-
// let authTokenRetrievalResult = self.authHandlerSemaphore.wait(timeout: .now() + 30.0)
677-
// if authTokenRetrievalResult == .success {
678-
// ITBInfo("authTokenRetrieval successful")
679-
// DispatchQueue.main.async {
680-
// completion(self.passedAuthToken)
681-
// }
682-
// self.sendEvent(
683-
// withName: EventName.handleAuthSuccessCalled.rawValue,
684-
// body: nil as Any?)
685-
// } else {
686-
// ITBInfo("authTokenRetrieval timed out")
687-
// DispatchQueue.main.async {
688-
// completion(nil)
689-
// }
690-
// self.sendEvent(
691-
// withName: EventName.handleAuthFailureCalled.rawValue,
692-
// body: nil as Any?)
693-
// }
694-
// }
695-
// }
696-
697-
// public func onTokenRegistrationFailed(_ reason: String?) {
698-
// }
699-
// }
592+
extension ReactIterableAPI: IterableURLDelegate {
593+
public func handle(iterableURL url: URL, inContext context: IterableActionContext) -> Bool {
594+
ITBInfo()
595+
guard shouldEmit else {
596+
return false
597+
}
598+
let contextDict = ReactIterableAPI.contextToDictionary(context: context)
599+
sendEvent(
600+
withName: EventName.handleUrlCalled.rawValue,
601+
body: [
602+
"url": url.absoluteString,
603+
"context": contextDict,
604+
] as [String: Any])
605+
return true
606+
}
607+
608+
private static func contextToDictionary(context: IterableActionContext) -> [AnyHashable: Any] {
609+
var result = [AnyHashable: Any]()
610+
let actionDict = actionToDictionary(action: context.action)
611+
result["action"] = actionDict
612+
result["source"] = context.source.rawValue
613+
return result
614+
}
615+
616+
private static func actionToDictionary(action: IterableAction) -> [AnyHashable: Any] {
617+
var actionDict = [AnyHashable: Any]()
618+
actionDict["type"] = action.type
619+
if let data = action.data {
620+
actionDict["data"] = data
621+
}
622+
if let userInput = action.userInput {
623+
actionDict["userInput"] = userInput
624+
}
625+
return actionDict
626+
}
627+
}
628+
629+
extension ReactIterableAPI: IterableCustomActionDelegate {
630+
public func handle(
631+
iterableCustomAction action: IterableAction, inContext context: IterableActionContext
632+
)
633+
-> Bool
634+
{
635+
ITBInfo()
636+
let actionDict = ReactIterableAPI.actionToDictionary(action: action)
637+
let contextDict = ReactIterableAPI.contextToDictionary(context: context)
638+
sendEvent(
639+
withName: EventName.handleCustomActionCalled.rawValue,
640+
body: [
641+
"action": actionDict,
642+
"context": contextDict,
643+
])
644+
return true
645+
}
646+
}
647+
648+
extension ReactIterableAPI: IterableInAppDelegate {
649+
public func onNew(message: IterableInAppMessage) -> InAppShowResponse {
650+
ITBInfo()
651+
guard shouldEmit else {
652+
return .show
653+
}
654+
sendEvent(
655+
withName: EventName.handleInAppCalled.rawValue,
656+
body: message.toDict())
657+
let timeoutResult = inAppHandlerSemaphore.wait(timeout: .now() + 2.0)
658+
if timeoutResult == .success {
659+
ITBInfo("inAppShowResponse: \(inAppShowResponse == .show)")
660+
return inAppShowResponse
661+
} else {
662+
ITBInfo("timed out")
663+
return .show
664+
}
665+
}
666+
}
667+
668+
extension ReactIterableAPI: IterableAuthDelegate {
669+
public func onAuthTokenRequested(completion: @escaping AuthTokenRetrievalHandler) {
670+
ITBInfo()
671+
DispatchQueue.global(qos: .userInitiated).async {
672+
self.sendEvent(
673+
withName: EventName.handleAuthCalled.rawValue,
674+
body: nil as Any?)
675+
let authTokenRetrievalResult = self.authHandlerSemaphore.wait(timeout: .now() + 30.0)
676+
if authTokenRetrievalResult == .success {
677+
ITBInfo("authTokenRetrieval successful")
678+
DispatchQueue.main.async {
679+
completion(self.passedAuthToken)
680+
}
681+
self.sendEvent(
682+
withName: EventName.handleAuthSuccessCalled.rawValue,
683+
body: nil as Any?)
684+
} else {
685+
ITBInfo("authTokenRetrieval timed out")
686+
DispatchQueue.main.async {
687+
completion(nil)
688+
}
689+
self.sendEvent(
690+
withName: EventName.handleAuthFailureCalled.rawValue,
691+
body: nil as Any?)
692+
}
693+
}
694+
}
695+
696+
public func onTokenRegistrationFailed(_ reason: String?) {
697+
}
698+
}

0 commit comments

Comments
 (0)