From aa2b8659bf882d3ad350909326501a97ef3ea44c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Buczek?= Date: Thu, 4 Nov 2021 21:31:58 +0100 Subject: [PATCH] Fix #4470: Sync UX hotfix. (#4471) --- BraveShared/BraveStrings.swift | 4 ++ .../Sync/SyncPairCameraViewController.swift | 62 ++++++++++++------- .../Sync/SyncPairWordsViewController.swift | 19 +++++- 3 files changed, 59 insertions(+), 26 deletions(-) diff --git a/BraveShared/BraveStrings.swift b/BraveShared/BraveStrings.swift index 225ca81276c..9f9dab15363 100644 --- a/BraveShared/BraveStrings.swift +++ b/BraveShared/BraveStrings.swift @@ -628,6 +628,10 @@ extension Strings { public static let syncRemoveOtherDeviceTitle = NSLocalizedString("SyncRemoveOtherDeviceTitle", tableName: "BraveShared", bundle: Bundle.braveShared, value: "Remove %@ from Sync Chain?", comment: "Title for removing other device from Sync") public static let syncRemoveOtherDeviceMessage = NSLocalizedString("SyncRemoveOtherDeviceMessage", tableName: "BraveShared", bundle: Bundle.braveShared, value: "Removing the device from the Sync Chain will not clear previously synced data from the device.", comment: "Message for removing other device from Sync") public static let syncRemoveDeviceDefaultName = NSLocalizedString("SyncRemoveDeviceDefaultName", tableName: "BraveShared", bundle: Bundle.braveShared, value: "Device", comment: "Default name for a device") + public static let syncJoinChainCodewordsWarning = NSLocalizedString("syncJoinChainCodewordsWarning", tableName: "BraveShared", bundle: Bundle.braveShared, value: "This will share your Brave data, including potentially your passwords, with the device that generated these code words. Please double check that these code words were generated by a device that you own. Are you sure you want to do this?", comment: "Default name for a device") + public static let syncJoinChainCameraWarning = NSLocalizedString("syncJoinChainCameraWarning", tableName: "BraveShared", bundle: Bundle.braveShared, value: "This will share your Brave data, including potentially your passwords, with the device that generated this QR code. Please double check that this QR code was generated by a device that you own. Are you sure you want to do this?", comment: "Default name for a device") + public static let syncJoinChainWarningTitle = NSLocalizedString("syncJoinChainWarningTitle", tableName: "BraveShared", bundle: Bundle.braveShared, value: "Warning", comment: "Title for pairing sync device") + } extension Strings { diff --git a/Client/Frontend/Sync/SyncPairCameraViewController.swift b/Client/Frontend/Sync/SyncPairCameraViewController.swift index cb30488a678..cefbedda80f 100644 --- a/Client/Frontend/Sync/SyncPairCameraViewController.swift +++ b/Client/Frontend/Sync/SyncPairCameraViewController.swift @@ -44,12 +44,8 @@ class SyncPairCameraViewController: SyncViewController { cameraView.layer.cornerRadius = 4 cameraView.layer.cornerCurve = .continuous cameraView.layer.masksToBounds = true - cameraView.scanCallback = { data in - - if !DeviceInfo.hasConnectivity() { - self.present(SyncAlerts.noConnection, animated: true) - return - } + cameraView.scanCallback = { [weak self] data in + guard let self = self else { return } // TODO: Functional, but needs some cleanup struct Scanner { static var lock = false } @@ -60,27 +56,47 @@ class SyncPairCameraViewController: SyncViewController { } Scanner.lock = true - self.cameraView.cameraOverlaySucess() - // Freezing the camera frame after QR has been scanned. - self.cameraView.captureSession?.stopRunning() - // Vibrate. - AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) - - // Forced timeout - DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(25.0) * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC), execute: { - Scanner.lock = false - self.cameraView.cameraOverlayError() - }) + let alert = UIAlertController(title: Strings.syncJoinChainWarningTitle, + message: Strings.syncJoinChainCameraWarning, + preferredStyle: .alert) - // If multiple calls get in here due to race conditions it isn't a big deal + let okAction = UIAlertAction(title: Strings.yes, style: .default, handler: { _ in + if !DeviceInfo.hasConnectivity() { + self.present(SyncAlerts.noConnection, animated: true) + return + } + self.cameraView.cameraOverlaySucess() + // Freezing the camera frame after QR has been scanned. + self.cameraView.captureSession?.stopRunning() + + // Vibrate. + AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) + + // Forced timeout + DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(25.0) * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC), execute: { + Scanner.lock = false + self.cameraView.cameraOverlayError() + }) + + // If multiple calls get in here due to race conditions it isn't a big deal + + let codeWords = BraveSyncAPI.shared.syncCode(fromHexSeed: data) + if codeWords.isEmpty { + self.cameraView.cameraOverlayError() + } else { + self.syncHandler?(codeWords) + } + }) - let codeWords = BraveSyncAPI.shared.syncCode(fromHexSeed: data) - if codeWords.isEmpty { - self.cameraView.cameraOverlayError() - } else { - self.syncHandler?(codeWords) + let cancelAction = UIAlertAction(title: Strings.CancelString, style: .cancel) { _ in + Scanner.lock = false } + + alert.addAction(okAction) + alert.addAction(cancelAction) + + self.present(alert, animated: true) } stackView.addArrangedSubview(cameraView) diff --git a/Client/Frontend/Sync/SyncPairWordsViewController.swift b/Client/Frontend/Sync/SyncPairWordsViewController.swift index 7b3ac8f654a..6ce280bbc15 100644 --- a/Client/Frontend/Sync/SyncPairWordsViewController.swift +++ b/Client/Frontend/Sync/SyncPairWordsViewController.swift @@ -152,9 +152,22 @@ class SyncPairWordsViewController: SyncViewController { } @objc func SEL_done() { - doIfConnected { - checkCodes() - } + let alert = UIAlertController(title: Strings.syncJoinChainWarningTitle, + message: Strings.syncJoinChainCodewordsWarning, + preferredStyle: .alert) + + let okAction = UIAlertAction(title: Strings.yes, style: .default, handler: { [weak self] _ in + self?.doIfConnected { + self?.checkCodes() + } + }) + + let cancelAction = UIAlertAction(title: Strings.CancelString, style: .cancel) + + alert.addAction(okAction) + alert.addAction(cancelAction) + + present(alert, animated: true) } private func checkCodes() {