From 67ffec997749d6325375329aac7c1d8819b52779 Mon Sep 17 00:00:00 2001 From: Graeme Arthur <2030310+graeme@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:25:18 +0100 Subject: [PATCH] Don't prompt to switch when 1 device --- DuckDuckGo/Preferences/Model/SyncPreferences.swift | 14 +++++++++++--- .../SyncUI/ViewModels/ManagementDialogModel.swift | 6 +++--- .../Sources/SyncUI/Views/ManagementDialog.swift | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/DuckDuckGo/Preferences/Model/SyncPreferences.swift b/DuckDuckGo/Preferences/Model/SyncPreferences.swift index 921cad6e6d..67e44a7bde 100644 --- a/DuckDuckGo/Preferences/Model/SyncPreferences.swift +++ b/DuckDuckGo/Preferences/Model/SyncPreferences.swift @@ -578,7 +578,12 @@ extension SyncPreferences: ManagementDialogModelDelegate { try await loginAndShowPresentedDialog(recoveryKey, isRecovery: fromRecoveryScreen) } catch { if case SyncError.accountAlreadyExists = error { - managementDialogModel.shouldShowSwitchAccountsMessage = true + if devices.count > 1 { + managementDialogModel.shouldShowSwitchAccountsMessage = true + } else { + switchAccounts(recoveryKey: recoveryKey) + managementDialogModel.endFlow() + } PixelKit.fire(DebugEvent(GeneralPixel.syncLoginExistingAccountError(error: error))) } else { managementDialogModel.syncErrorMessage = SyncErrorMessage(type: .unableToSyncToOtherDevice) @@ -760,10 +765,14 @@ extension SyncPreferences: ManagementDialogModelDelegate { recoverDevice(recoveryCode: code, fromRecoveryScreen: fromRecoveryScreen) } - func switchSync(recoveryCode: String) { + func switchAccounts(recoveryCode: String) { guard let recoveryKey = try? SyncCode.decodeBase64String(recoveryCode).recovery else { return } + switchAccounts(recoveryKey: recoveryKey) + } + + func switchAccounts(recoveryKey: SyncCode.RecoveryKey) { Task { [weak self] in guard let self else { return } do { @@ -782,5 +791,4 @@ extension SyncPreferences: ManagementDialogModelDelegate { // TODO: Send sync_user_switched_account_pixel } } - } diff --git a/LocalPackages/SyncUI/Sources/SyncUI/ViewModels/ManagementDialogModel.swift b/LocalPackages/SyncUI/Sources/SyncUI/ViewModels/ManagementDialogModel.swift index 3c7c6f66d6..4522ee2352 100644 --- a/LocalPackages/SyncUI/Sources/SyncUI/ViewModels/ManagementDialogModel.swift +++ b/LocalPackages/SyncUI/Sources/SyncUI/ViewModels/ManagementDialogModel.swift @@ -33,7 +33,7 @@ public protocol ManagementDialogModelDelegate: AnyObject { func enterRecoveryCodePressed() func copyCode() func openSystemPasswordSettings() - func switchSync(recoveryCode: String) + func switchAccounts(recoveryCode: String) } public final class ManagementDialogModel: ObservableObject { @@ -64,9 +64,9 @@ public final class ManagementDialogModel: ObservableObject { currentDialog = nil } - public func switchSyncAccounts(recoveryCode: String) { + public func switchAccounts(recoveryCode: String) { shouldShowSwitchAccountsMessage = false - delegate?.switchSync(recoveryCode: recoveryCode) + delegate?.switchAccounts(recoveryCode: recoveryCode) endFlow() } diff --git a/LocalPackages/SyncUI/Sources/SyncUI/Views/ManagementDialog.swift b/LocalPackages/SyncUI/Sources/SyncUI/Views/ManagementDialog.swift index a6693a7f01..7b4a743ff1 100644 --- a/LocalPackages/SyncUI/Sources/SyncUI/Views/ManagementDialog.swift +++ b/LocalPackages/SyncUI/Sources/SyncUI/Views/ManagementDialog.swift @@ -75,7 +75,7 @@ public struct ManagementDialog: View { title: Text(UserText.syncAlertSwitchAccountTitle), message: Text(UserText.syncAlertSwitchAccountMessage), primaryButton: .default(Text(UserText.syncAlertSwitchAccountButton)) { - model.switchSyncAccounts(recoveryCode: recoveryCodeModel.recoveryCode) + model.switchAccounts(recoveryCode: recoveryCodeModel.recoveryCode) }, secondaryButton: .cancel { model.endFlow()