Skip to content
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

Simplify connecting sync accounts when two exist #3747

Closed
wants to merge 12 commits into from
Closed
14 changes: 14 additions & 0 deletions Core/PixelEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,13 @@ extension Pixel {
case syncSecureStorageDecodingError
case syncAccountRemoved(reason: String)

case syncAskUserToSwitchAccount
case syncUserAcceptedSwitchingAccount
case syncUserCancelledSwitchingAccount
case syncUserSwitchedAccount
case syncUserSwitchedLogoutError
case syncUserSwitchedLoginError

case syncGetOtherDevices
case syncGetOtherDevicesCopy
case syncGetOtherDevicesShare
Expand Down Expand Up @@ -1620,6 +1627,13 @@ extension Pixel.Event {
case .syncSecureStorageDecodingError: return "sync_secure_storage_decoding_error"
case .syncAccountRemoved(let reason): return "sync_account_removed_reason_\(reason)"

case .syncAskUserToSwitchAccount: return "sync_ask_user_to_switch_account"
case .syncUserAcceptedSwitchingAccount: return "sync_user_accepted_switching_account"
case .syncUserCancelledSwitchingAccount: return "sync_user_cancelled_switching_account"
case .syncUserSwitchedAccount: return "sync_user_switched_account"
case .syncUserSwitchedLogoutError: return "sync_user_switched_logout_error"
case .syncUserSwitchedLoginError: return "sync_user_switched_login_error"

case .syncGetOtherDevices: return "sync_get_other_devices"
case .syncGetOtherDevicesCopy: return "sync_get_other_devices_copy"
case .syncGetOtherDevicesShare: return "sync_get_other_devices_share"
Expand Down
41 changes: 41 additions & 0 deletions DuckDuckGo/SyncSettingsViewController+SyncDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,47 @@ extension SyncSettingsViewController: SyncManagementViewModelDelegate {
}
}

@MainActor
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 {
Pixel.fire(pixel: .syncUserAcceptedSwitchingAccount)
await self?.switchAccounts(recoveryKey: recoveryKey)
}
}
alertController.addAction(title: UserText.actionCancel, style: .cancel) {
Pixel.fire(pixel: .syncUserCancelledSwitchingAccount)
}
// Gives time to the is syncing view to appear
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
self?.dismissPresentedViewController { [weak self] in
self?.present(alertController, animated: true, completion: nil)
Pixel.fire(pixel: .syncAskUserToSwitchAccount)
}
}
}

func switchAccounts(recoveryKey: SyncCode.RecoveryKey) async {
Task { [weak self] in
do {
try await self?.syncService.disconnect()
} catch {
Pixel.fire(pixel: .syncUserSwitchedLogoutError)
}

do {
try await self?.loginAndShowDeviceConnected(recoveryKey: recoveryKey)
} catch {
Pixel.fire(pixel: .syncUserSwitchedLoginError)
}
Pixel.fire(pixel: .syncUserSwitchedAccount)
}
}

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 @@ -348,7 +348,7 @@
}
}

func syncCodeEntered(code: String) async -> Bool {

Check failure on line 351 in DuckDuckGo/SyncSettingsViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Function should have complexity 10 or less; currently complexity is 11 (cyclomatic_complexity)
var shouldShowSyncEnabled = true
guard let syncCode = try? SyncCode.decodeBase64String(code) else {
return false
Expand All @@ -361,7 +361,11 @@
return true
} catch {
if self.rootView.model.isSyncEnabled {
handleError(.unableToMergeTwoAccounts, error: error, event: .syncLoginExistingAccountError)
if rootView.model.devices.count > 1 {
promptToSwitchAccounts(recoveryKey: recoveryKey)
} else {
await switchAccounts(recoveryKey: recoveryKey)
}
} else {
handleError(.unableToSyncToServer, error: error, event: .syncLoginError)
}
Expand Down
3 changes: 3 additions & 0 deletions DuckDuckGo/UserText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,9 @@ But if you *do* want a peek under the hood, you can find more information about
static let unknownErrorTryAgainMessage = NSLocalizedString("error.unknown.try.again", value: "An unknown error has occurred", comment: "Generic error message on a dialog for when the cause is not known.")
public static let syncPausedAlertOkButton = NSLocalizedString("alert.sync-paused-alert-ok-button", value: "OK", comment: "Confirmation button in alert")
public static let syncPausedAlertLearnMoreButton = NSLocalizedString("alert.sync-paused-alert-learn-more-button", value: "Learn More", comment: "Learn more button in alert")
public static let syncAlertSwitchAccountTitle = NSLocalizedString("alert.sync-switch-account-title", value: "Switch to a different Sync?", comment: "Switch account title in alert")
public static let syncAlertSwitchAccountMessage = NSLocalizedString("alert.sync-switch-account-message", value: "This device is already synced, are you sure you want to sync it with a different back up or device? Switching won't remove any data already synced to this device.", comment: "Description for switching sync accounts when there's two")
public static let syncAlertSwitchAccountButton = NSLocalizedString("alert.sync-switch-account-button", value: "Switch Sync", comment: "Switch account button in alert")
public static let syncErrorAlertTitle = NSLocalizedString("alert.sync-error", value: "Sync & Backup Error", comment: "Title for sync error alert")
public static let unableToSyncToServerDescription = NSLocalizedString("alert.unable-to-sync-to-server-description", value: "Unable to connect to the server.", comment: "Description for unable to sync to server error")
public static let unableToSyncWithOtherDeviceDescription = NSLocalizedString("alert.unable-to-sync-with-other-device-description", value: "Unable to Sync with another device.", comment: "Description for unable to sync with another device error")
Expand Down
9 changes: 9 additions & 0 deletions DuckDuckGo/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@
/* Title for alert shown when sync paused for an error */
"alert.sync-paused-title" = "Sync is Paused";

/* Switch account button in alert */
"alert.sync-switch-account-button" = "Switch Sync";

/* Description for switching sync accounts when there's two */
"alert.sync-switch-account-message" = "This device is already synced, are you sure you want to sync it with a different back up or device? Switching won't remove any data already synced to this device.";

/* Switch account title in alert */
"alert.sync-switch-account-title" = "Switch to a different Sync?";

/* Description for alert shown when sync error occurs because of too many requests */
"alert.sync-too-many-requests-error-description" = "Sync & Backup is temporarily unavailable.";

Expand Down
Loading