Skip to content

Commit

Permalink
Make sure flow isnt ended before switch finishes
Browse files Browse the repository at this point in the history
  • Loading branch information
graeme committed Dec 20, 2024
1 parent 12519f9 commit aaae1d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
40 changes: 21 additions & 19 deletions DuckDuckGo/Preferences/Model/SyncPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,10 @@ extension SyncPreferences: ManagementDialogModelDelegate {
managementDialogModel.shouldShowSwitchAccountsMessage = true
PixelKit.fire(SyncSwitchAccountPixelKitEvent.syncAskUserToSwitchAccount.withoutMacPrefix)
} else {
switchAccounts(recoveryKey: recoveryKey)
managementDialogModel.endFlow()
Task { @MainActor in
await switchAccounts(recoveryKey: recoveryKey)
managementDialogModel.endFlow()
}
}
PixelKit.fire(DebugEvent(GeneralPixel.syncLoginExistingAccountError(error: SyncError.accountAlreadyExists)))
}
Expand All @@ -775,27 +777,27 @@ extension SyncPreferences: ManagementDialogModelDelegate {
guard let recoveryKey = try? SyncCode.decodeBase64String(recoveryCode).recovery else {
return
}
switchAccounts(recoveryKey: recoveryKey)
Task {
await switchAccounts(recoveryKey: recoveryKey)
managementDialogModel.endFlow()
}
}

private func switchAccounts(recoveryKey: SyncCode.RecoveryKey) {
Task { [weak self] in
guard let self else { return }
do {
try await syncService.disconnect()
} catch {
PixelKit.fire(SyncSwitchAccountPixelKitEvent.syncUserSwitchedLogoutError.withoutMacPrefix)
}
private func switchAccounts(recoveryKey: SyncCode.RecoveryKey) async {
do {
try await syncService.disconnect()
} catch {
PixelKit.fire(SyncSwitchAccountPixelKitEvent.syncUserSwitchedLogoutError.withoutMacPrefix)
}

do {
let device = deviceInfo()
let registeredDevices = try await syncService.login(recoveryKey, deviceName: device.name, deviceType: device.type)
await mapDevices(registeredDevices)
} catch {
PixelKit.fire(SyncSwitchAccountPixelKitEvent.syncUserSwitchedLoginError.withoutMacPrefix)
}
PixelKit.fire(SyncSwitchAccountPixelKitEvent.syncUserSwitchedAccount.withoutMacPrefix)
do {
let device = deviceInfo()
let registeredDevices = try await syncService.login(recoveryKey, deviceName: device.name, deviceType: device.type)
await mapDevices(registeredDevices)
} catch {
PixelKit.fire(SyncSwitchAccountPixelKitEvent.syncUserSwitchedLoginError.withoutMacPrefix)
}
PixelKit.fire(SyncSwitchAccountPixelKitEvent.syncUserSwitchedAccount.withoutMacPrefix)
}

func switchAccountsCancelled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public final class ManagementDialogModel: ObservableObject {

public func userConfirmedSwitchAccounts(recoveryCode: String) {
delegate?.userConfirmedSwitchAccounts(recoveryCode: recoveryCode)
doEndFlow()
}

private func doEndFlow() {
Expand Down

0 comments on commit aaae1d0

Please sign in to comment.