From 539375c4e98b23af315defcb7cfa9aabf07b2944 Mon Sep 17 00:00:00 2001 From: Joao Dordio Date: Tue, 29 Oct 2024 15:53:02 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=97=91=EF=B8=8F=20Removed=20onComplet?= =?UTF-8?q?ion=20handlers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- swift-sdk/Internal/InternalIterableAPI.swift | 100 ++++++++----------- 1 file changed, 44 insertions(+), 56 deletions(-) diff --git a/swift-sdk/Internal/InternalIterableAPI.swift b/swift-sdk/Internal/InternalIterableAPI.swift index ed03265ed..fd5874f9c 100644 --- a/swift-sdk/Internal/InternalIterableAPI.swift +++ b/swift-sdk/Internal/InternalIterableAPI.swift @@ -133,6 +133,10 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider { func setEmail(_ email: String?, authToken: String? = nil, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil, identityResolution: IterableIdentityResolution? = nil) { ITBInfo() + self.identityResolution = identityResolution + self._successCallback = successHandler + self._failureCallback = failureHandler + if self._email == email && email != nil { self.checkAndUpdateAuthToken(authToken) return @@ -146,34 +150,20 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider { self._email = email self._userId = nil - - self.onLogin(authToken) { [weak self] in - guard let config = self?.config else { - return - } - let merge = identityResolution?.mergeOnAnonymousToKnown ?? config.identityResolution.mergeOnAnonymousToKnown - let replay = identityResolution?.replayOnVisitorToKnown ?? config.identityResolution.replayOnVisitorToKnown - if config.enableAnonTracking, let email = email { - self?.attemptAndProcessMerge( - merge: merge ?? true, - replay: replay ?? true, - destinationUser: email, - isEmail: true, - failureHandler: failureHandler - ) - self?.localStorage.userIdAnnon = nil - } - } - - self._successCallback = successHandler - self._failureCallback = failureHandler + self.localStorage.userIdAnnon = nil + self.storeIdentifierData() + self.onLogin(authToken) } func setUserId(_ userId: String?, authToken: String? = nil, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil, isAnon: Bool = false, identityResolution: IterableIdentityResolution? = nil) { ITBInfo() + self.identityResolution = identityResolution + self._successCallback = successHandler + self._failureCallback = failureHandler + if self._userId == userId && userId != nil { self.checkAndUpdateAuthToken(authToken) return @@ -188,32 +178,12 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider { self._email = nil self._userId = userId - self.onLogin(authToken) { [weak self] in - guard let config = self?.config else { - return - } - if config.enableAnonTracking { - if let userId = userId, userId != (self?.localStorage.userIdAnnon ?? "") { - let merge = identityResolution?.mergeOnAnonymousToKnown ?? config.identityResolution.mergeOnAnonymousToKnown - let replay = identityResolution?.replayOnVisitorToKnown ?? config.identityResolution.replayOnVisitorToKnown - self?.attemptAndProcessMerge( - merge: merge ?? true, - replay: replay ?? true, - destinationUser: userId, - isEmail: false, - failureHandler: failureHandler - ) - } - - if !isAnon { - self?.localStorage.userIdAnnon = nil - } - } + if !isAnon { + self.localStorage.userIdAnnon = nil } - self._successCallback = successHandler - self._failureCallback = failureHandler self.storeIdentifierData() + self.onLogin(authToken) } @@ -651,6 +621,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider { private var _email: String? private var _payloadData: [AnyHashable: Any]? private var _userId: String? + private var identityResolution: IterableIdentityResolution? private var _successCallback: OnSuccessHandler? = nil private var _failureCallback: OnFailureHandler? = nil @@ -736,31 +707,30 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider { localStorage.userId = _userId } - private func onLogin(_ authToken: String? = nil, onloginSuccess onloginSuccessCallBack: (()->())? = nil) { + private func onLogin(_ authToken: String? = nil) { ITBInfo() self.authManager.pauseAuthRetries(false) - if let authToken = authToken { + + if let authToken { self.authManager.setNewToken(authToken) - completeUserLogin(onloginSuccessCallBack: onloginSuccessCallBack) + completeUserLogin(with: authToken) } else if isEitherUserIdOrEmailSet() && config.authDelegate != nil { - requestNewAuthToken(onloginSuccessCallBack: onloginSuccessCallBack) + requestNewAuthToken() } else { - completeUserLogin(onloginSuccessCallBack: onloginSuccessCallBack) + completeUserLogin() } } - private func requestNewAuthToken(onloginSuccessCallBack: (()->())? = nil) { + private func requestNewAuthToken() { ITBInfo() authManager.requestNewAuthToken(hasFailedPriorAuth: false, onSuccess: { [weak self] token in - if token != nil { - self?.completeUserLogin(onloginSuccessCallBack: onloginSuccessCallBack) - } + self?.completeUserLogin(with: token) }, shouldIgnoreRetryPolicy: true) } - private func completeUserLogin(onloginSuccessCallBack: (()->())? = nil) { + private func completeUserLogin(with token: String? = nil) { ITBInfo() guard isEitherUserIdOrEmailSet() else { return @@ -773,8 +743,26 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider { } _ = inAppManager.scheduleSync() - if onloginSuccessCallBack != nil { - onloginSuccessCallBack!() + + if config.authDelegate == nil || token != nil { + attemptMergeAndEventReplay() + } + } + + private func attemptMergeAndEventReplay() { + let merge = identityResolution?.mergeOnAnonymousToKnown ?? config.identityResolution.mergeOnAnonymousToKnown + let replay = identityResolution?.replayOnVisitorToKnown ?? config.identityResolution.replayOnVisitorToKnown + + let destinationUser = email ?? userId ?? nil + + if config.enableAnonTracking { + self.attemptAndProcessMerge( + merge: merge ?? true, + replay: replay ?? true, + destinationUser: destinationUser, + isEmail: email != nil, + failureHandler: self._failureCallback + ) } } From 7fef7b1d2a50844c2833bb87c0c7a7932eeb50a8 Mon Sep 17 00:00:00 2001 From: Joao Dordio Date: Wed, 30 Oct 2024 16:20:39 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=9A=A7=20WIP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- swift-sdk/Internal/AuthManager.swift | 6 ++++-- swift-sdk/Internal/InternalIterableAPI.swift | 7 +++++-- swift-sdk/IterableAuthManagerProtocol.swift | 2 +- tests/common/MockAuthManager.swift | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/swift-sdk/Internal/AuthManager.swift b/swift-sdk/Internal/AuthManager.swift index da5d4b613..38efdb768 100644 --- a/swift-sdk/Internal/AuthManager.swift +++ b/swift-sdk/Internal/AuthManager.swift @@ -75,10 +75,12 @@ class AuthManager: IterableAuthManagerProtocol { return isLastAuthTokenValid && !shouldIgnoreRetryPolicy } - func setNewToken(_ newToken: String) { + func setNewToken(_ newToken: String, onCompletion: @escaping (String?) -> Void) { ITBInfo() - onAuthTokenReceived(retrievedAuthToken: newToken) + onAuthTokenReceived(retrievedAuthToken: newToken) { token in + onCompletion(token) + } } func logoutUser() { diff --git a/swift-sdk/Internal/InternalIterableAPI.swift b/swift-sdk/Internal/InternalIterableAPI.swift index fd5874f9c..25938793e 100644 --- a/swift-sdk/Internal/InternalIterableAPI.swift +++ b/swift-sdk/Internal/InternalIterableAPI.swift @@ -713,8 +713,11 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider { self.authManager.pauseAuthRetries(false) if let authToken { - self.authManager.setNewToken(authToken) - completeUserLogin(with: authToken) + self.authManager.setNewToken(authToken) { [weak self] token in + guard let self else { return } + + self.completeUserLogin(with: token) + } } else if isEitherUserIdOrEmailSet() && config.authDelegate != nil { requestNewAuthToken() } else { diff --git a/swift-sdk/IterableAuthManagerProtocol.swift b/swift-sdk/IterableAuthManagerProtocol.swift index 8f0e5c433..7eb38142c 100644 --- a/swift-sdk/IterableAuthManagerProtocol.swift +++ b/swift-sdk/IterableAuthManagerProtocol.swift @@ -9,7 +9,7 @@ import Foundation func resetFailedAuthCount() func requestNewAuthToken(hasFailedPriorAuth: Bool, onSuccess: ((String?) -> Void)?, shouldIgnoreRetryPolicy: Bool) func scheduleAuthTokenRefreshTimer(interval: TimeInterval, isScheduledRefresh: Bool, successCallback: AuthTokenRetrievalHandler?) - func setNewToken(_ newToken: String) + func setNewToken(_ newToken: String, onCompletion: @escaping (String?) -> Void) func logoutUser() func handleAuthFailure(failedAuthToken: String?, reason: AuthFailureReason) func pauseAuthRetries(_ pauseAuthRetry: Bool) diff --git a/tests/common/MockAuthManager.swift b/tests/common/MockAuthManager.swift index 76bc00c2f..578332e0f 100644 --- a/tests/common/MockAuthManager.swift +++ b/tests/common/MockAuthManager.swift @@ -55,7 +55,7 @@ class MockAuthManager: IterableAuthManagerProtocol { } - func setNewToken(_ newToken: String) { + func setNewToken(_ newToken: String, onCompletion: @escaping (String?) -> Void) { } From 96a36eaf0a4f64d62540aaec22af000feab3850a Mon Sep 17 00:00:00 2001 From: Evan Greer Date: Sun, 3 Nov 2024 15:49:03 -0700 Subject: [PATCH 3/3] updates config naming --- swift-sdk/Internal/InternalIterableAPI.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swift-sdk/Internal/InternalIterableAPI.swift b/swift-sdk/Internal/InternalIterableAPI.swift index 7d061d138..b6db38c80 100644 --- a/swift-sdk/Internal/InternalIterableAPI.swift +++ b/swift-sdk/Internal/InternalIterableAPI.swift @@ -767,7 +767,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider { let destinationUser = email ?? userId ?? nil - if config.enableAnonTracking { + if config.enableAnonActivation { self.attemptAndProcessMerge( merge: merge ?? true, replay: replay ?? true,