-
Notifications
You must be signed in to change notification settings - Fork 24
chore: remove session manager completion handlers - WPB-23401 #4301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: chore/surface-errors-loading-sessions-WPB-23354
Are you sure you want to change the base?
Changes from all commits
cd55c5a
2f0669c
8f6f654
e35e865
1aabe8a
c6c0c7c
dd0f98d
58b7054
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -471,7 +471,7 @@ | |
| object: nil, | ||
| queue: nil | ||
| ) { [weak self] _ in | ||
| WireLogger.sessionManager.debug("Received memory warning, tearing down background user sessions.") | ||
|
Check warning on line 474 in wire-ios-sync-engine/Source/SessionManager/SessionManager.swift
|
||
| self?.tearDownAllBackgroundSessions() | ||
| } | ||
|
|
||
|
|
@@ -688,70 +688,62 @@ | |
| } | ||
|
|
||
| /// Select the account to be the active account. | ||
| /// - completion: runs when the user session was loaded | ||
| /// - tearDownCompletion: runs when the UI no longer holds any references to the previous user session. | ||
| public func select( | ||
| _ account: Account, | ||
| completion: ((ZMUserSession?) -> Void)? = nil, | ||
| tearDownCompletion: (() -> Void)? = nil | ||
| ) { | ||
| @MainActor | ||
| public func select(_ account: Account) async -> ZMUserSession? { | ||
| guard !isSelectingAccount else { | ||
| completion?(nil) | ||
| return | ||
| return nil | ||
| } | ||
|
|
||
| confirmSwitchingAccount { [weak self] isConfirmed in | ||
| let isConfirmed = await confirmSwitchingAccount() | ||
|
|
||
| guard isConfirmed else { | ||
| completion?(nil) | ||
| return | ||
| } | ||
| guard isConfirmed else { | ||
| return nil | ||
| } | ||
|
|
||
| self?.isSelectingAccount = true | ||
| let selectedAccount = self?.accountManager.selectedAccount | ||
| isSelectingAccount = true | ||
| let selectedAccount = accountManager.selectedAccount | ||
|
|
||
| guard let delegate = self?.delegate else { | ||
| completion?(nil) | ||
| return | ||
| } | ||
| guard let delegate = delegate else { | ||
|
Check warning on line 706 in wire-ios-sync-engine/Source/SessionManager/SessionManager.swift
|
||
| return nil | ||
| } | ||
|
|
||
| return await withCheckedContinuation { continuation in | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: if you follow the path |
||
| delegate.sessionManagerWillOpenAccount( | ||
| account, | ||
| from: selectedAccount, | ||
| userSessionCanBeTornDown: { [weak self] in | ||
| self?.activeUserSession = nil | ||
| tearDownCompletion?() | ||
|
|
||
| Task { @MainActor [weak self] in | ||
| guard let self else { | ||
| completion?(nil) | ||
| continuation.resume(returning: nil) | ||
| return | ||
| } | ||
|
|
||
| accountManager.select(account) | ||
| let session = await loadSession(for: account) | ||
| isSelectingAccount = false | ||
|
|
||
| if let session { | ||
| completion?(session) | ||
| } else { | ||
| completion?(nil) | ||
| } | ||
| continuation.resume(returning: session) | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| public func addAccount(userInfo: [String: Any]? = nil, completion: (() -> Void)? = nil) { | ||
| confirmSwitchingAccount { [weak self] isConfirmed in | ||
| guard isConfirmed else { return } | ||
| let error = NSError(userSessionErrorCode: .addAccountRequested, userInfo: userInfo) | ||
| self?.delegate?.sessionManagerWillLogout( | ||
| @MainActor | ||
| public func addAccount(userInfo: [String: Any]? = nil) async { | ||
| let isConfirmed = await confirmSwitchingAccount() | ||
| guard isConfirmed else { return } | ||
|
|
||
| let error = NSError(userSessionErrorCode: .addAccountRequested, userInfo: userInfo) | ||
| await withCheckedContinuation { continuation in | ||
| delegate?.sessionManagerWillLogout( | ||
| environment: nil, | ||
| error: error | ||
| ) { [weak self] in | ||
| self?.activeUserSession = nil | ||
| completion?() | ||
| continuation.resume() | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -778,9 +770,10 @@ | |
| WireLogger.sessionManager.debug("Deleting account \(account.userIdentifier)...") | ||
| if let secondAccount = accountManager.inactiveAccounts.first { | ||
| // Deleted an account but we can switch to another account | ||
| select(secondAccount, tearDownCompletion: { [weak self] in | ||
| self?.tearDownSessionAndDelete(account: account, eraseData: eraseData) | ||
| }) | ||
| Task { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: why not make |
||
| _ = await select(secondAccount) | ||
| tearDownSessionAndDelete(account: account, eraseData: eraseData) | ||
| } | ||
| } else if accountManager.selectedAccount != account { | ||
| // Deleted an inactive account, there's no need notify the UI | ||
| tearDownSessionAndDelete(account: account, eraseData: eraseData) | ||
|
|
@@ -795,9 +788,10 @@ | |
| } | ||
|
|
||
| fileprivate func tearDownSessionAndDelete(account: Account, eraseData: Bool) { | ||
| tearDownBackgroundSession(for: account.userIdentifier) { | ||
| Task { | ||
| await tearDownBackgroundSession(for: account.userIdentifier) | ||
| if eraseData { | ||
| self.deleteAccountData(for: account) | ||
| deleteAccountData(for: account) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -809,7 +803,9 @@ | |
| if session == activeUserSession { | ||
| logoutCurrentSession(deleteCookie: true, deleteAccount: false, error: error) | ||
| } else { | ||
| tearDownBackgroundSession(for: account.userIdentifier) | ||
| Task { | ||
| await tearDownBackgroundSession(for: account.userIdentifier) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1089,27 +1085,24 @@ | |
| } | ||
|
|
||
| /// The active user session will be torn down and the app goes into migration state. | ||
| public func prepareForRestoreWithMigration(completion: @escaping () -> Void) { | ||
| @MainActor | ||
| public func prepareForRestoreWithMigration() async { | ||
| guard let delegate else { | ||
| WireLogger.sessionManager.debug("SessionManager.delegate is nil, aborting migration preparation") | ||
| return completion() | ||
| return | ||
| } | ||
|
|
||
| WireLogger.sessionManager.debug("SessionManager.delegate.sessionManagerWillMigrateAccount ...") | ||
| delegate.sessionManagerWillMigrateAccount { [self] in | ||
| await delegate.sessionManagerWillMigrateAccount() | ||
|
|
||
| WireLogger.sessionManager.debug("... userSessionCanBeTornDown { ... }") | ||
| WireLogger.sessionManager.debug("... userSessionCanBeTornDown { ... }") | ||
|
|
||
| if let accountID = activeUserSession?.account.userIdentifier { | ||
| tearDownBackgroundSession(for: accountID) { [self] in | ||
| activeUserSession = nil | ||
| accountTokens.removeValue(forKey: accountID) | ||
| completion() | ||
| } | ||
| } else { | ||
| activeUserSession = nil | ||
| completion() | ||
| } | ||
| if let accountID = activeUserSession?.account.userIdentifier { | ||
| await tearDownBackgroundSession(for: accountID) | ||
| activeUserSession = nil | ||
| accountTokens.removeValue(forKey: accountID) | ||
| } else { | ||
| activeUserSession = nil | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1274,23 +1267,24 @@ | |
| notifyNewUserSessionCreated(newSession) | ||
| } | ||
|
|
||
| func tearDownBackgroundSession(for accountId: UUID, completion: (() -> Void)? = nil) { | ||
| @MainActor | ||
| func tearDownBackgroundSession(for accountId: UUID) async { | ||
| guard let userSession = backgroundUserSessions[accountId] else { | ||
| WireLogger.sessionManager | ||
| .error("No session to tear down for \(accountId), known sessions: \(backgroundUserSessions)") | ||
| completion?() | ||
| return | ||
| } | ||
| tearDownObservers(account: accountId) | ||
| backgroundUserSessions[accountId] = nil | ||
|
|
||
| dispatchGroup.enter() | ||
| userSession.close(deleteCookie: false) { [weak self] in | ||
| self?.notifyUserSessionDestroyed(accountId) | ||
| completion?() | ||
| self?.dispatchGroup.leave() | ||
| await withCheckedContinuation { continuation in | ||
| userSession.close(deleteCookie: false) { [weak self] in | ||
| self?.notifyUserSessionDestroyed(accountId) | ||
| continuation.resume() | ||
| self?.dispatchGroup.leave() | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // Tears down and releases all background user sessions. | ||
|
|
@@ -1299,8 +1293,10 @@ | |
| activeUserSession != session | ||
| } | ||
|
|
||
| backgroundSessions.keys.forEach { | ||
| tearDownBackgroundSession(for: $0) | ||
| Task { | ||
| for key in backgroundSessions.keys { | ||
| await tearDownBackgroundSession(for: key) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1823,22 +1819,28 @@ | |
|
|
||
| public extension SessionManager { | ||
|
|
||
| func confirmSwitchingAccount(completion: @escaping (_ isConfirmed: Bool) -> Void) { | ||
| @MainActor | ||
| func confirmSwitchingAccount() async -> Bool { | ||
| guard | ||
| let switchingDelegate, | ||
| let activeUserSession, | ||
| activeUserSession.isCallOngoing | ||
| else { | ||
| // no confirmation to show if no call is ongoing | ||
| return completion(true) | ||
| return true | ||
| } | ||
|
|
||
| switchingDelegate.confirmSwitchingAccount { confirmed in | ||
| if confirmed { | ||
| activeUserSession.callCenter?.endAllCalls() | ||
| let confirmed = await withCheckedContinuation { continuation in | ||
| switchingDelegate.confirmSwitchingAccount { confirmed in | ||
| continuation.resume(returning: confirmed) | ||
| } | ||
| completion(confirmed) | ||
| } | ||
|
|
||
| if confirmed { | ||
| activeUserSession.callCenter?.endAllCalls() | ||
| } | ||
|
|
||
| return confirmed | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: not really part of the scope here but I'm curious to see what you think. Calling this method will ask a ui delegate to present an alert and request user confirmation. Do you think this kind of back and forth communication makes sense? Or would it be better to have a single direction: ui tries to select an account, if confirmation is needed, throw error, ui handles, gets confirmation, tries again passing in a confirmation flag. Something liKe: