Skip to content

Commit

Permalink
Don't prompt to switch when 1 device
Browse files Browse the repository at this point in the history
  • Loading branch information
graeme committed Dec 17, 2024
1 parent 82d1e4b commit fed45fe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
34 changes: 19 additions & 15 deletions DuckDuckGo/SyncSettingsViewController+SyncDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,13 @@ extension SyncSettingsViewController: SyncManagementViewModelDelegate {
}

@MainActor
func handlePairingTwoSeparateAccounts(recoveryKey: SyncCode.RecoveryKey) {
func promptToSwitchAccounts(recoveryKey: SyncCode.RecoveryKey) {
let alertController = UIAlertController(
title: UserText.syncAlertSwitchAccountTitle,
message: UserText.syncAlertSwitchAccountMessage,
preferredStyle: .alert)
alertController.addAction(title: UserText.syncAlertSwitchAccountButton, style: .default) { [weak self] in
Task { [weak self] in
do {
try await self?.syncService.disconnect()
} catch {
// TODO: Send sync_user_switched_logout_error pixel
}

do {
try await self?.loginAndShowDeviceConnected(recoveryKey: recoveryKey)
} catch {
// TODO: Send sync_user_switched_login_error pixel
}
// TODO: Send sync_user_switched_account_pixel
}
self?.switchAccounts(recoveryKey: recoveryKey)
}
alertController.addAction(title: UserText.actionCancel, style: .cancel)
// Gives time to the is syncing view to appear
Expand All @@ -213,6 +200,23 @@ extension SyncSettingsViewController: SyncManagementViewModelDelegate {
}
}

func switchAccounts(recoveryKey: SyncCode.RecoveryKey) {
Task { [weak self] in
do {
try await self?.syncService.disconnect()
} catch {
// TODO: Send sync_user_switched_logout_error pixel
}

do {
try await self?.loginAndShowDeviceConnected(recoveryKey: recoveryKey)
} catch {
// TODO: Send sync_user_switched_login_error pixel
}
// TODO: Send sync_user_switched_account_pixel
}
}

private func getErrorType(from errorString: String?) -> AsyncErrorType? {
guard let errorString = errorString else {
return nil
Expand Down
6 changes: 5 additions & 1 deletion DuckDuckGo/SyncSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,11 @@ extension SyncSettingsViewController: ScanOrPasteCodeViewModelDelegate {
return true
} catch {
if self.rootView.model.isSyncEnabled {
handlePairingTwoSeparateAccounts(recoveryKey: recoveryKey)
if rootView.model.devices.count > 1 {
promptToSwitchAccounts(recoveryKey: recoveryKey)
} else {
switchAccounts(recoveryKey: recoveryKey)
}
} else {
handleError(.unableToSyncToServer, error: error, event: .syncLoginError)
}
Expand Down

0 comments on commit fed45fe

Please sign in to comment.