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

wire up sync account creation and recovery to backend #1561

Merged
merged 10 commits into from
Mar 28, 2023
Prev Previous commit
Next Next commit
use updated BSK to connect the sync account
  • Loading branch information
brindy committed Mar 22, 2023
commit 3afcc2d58628772c04c45a80bc71f31fcac7612b
26 changes: 20 additions & 6 deletions DuckDuckGo/SyncSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ extension SyncSettingsViewController: SyncManagementViewModelDelegate {
model.delegate = self

let controller = DismissibleHostingController(rootView: ScanOrPasteCodeView(model: model)) { [weak self] in
self?.rootView.model.codeCollectionCancelled()
assert(self?.navigationController?.visibleViewController is DismissibleHostingController<ScanOrPasteCodeView>)
self?.navigationController?.topViewController?.dismiss(animated: true)
}

let navController = UIDevice.current.userInterfaceIdiom == .phone
Expand Down Expand Up @@ -235,7 +236,11 @@ extension SyncSettingsViewController: SyncManagementViewModelDelegate {
alert.addAction(title: UserText.syncTurnOffConfirmAction, style: .destructive) {
Task { @MainActor in
// TODO handle error disconnecting
try await self.syncService.disconnect()
do {
try await self.syncService.disconnect()
} catch {
print(error.localizedDescription)
}
continuation.resume(returning: true)
}
}
Expand Down Expand Up @@ -294,11 +299,20 @@ extension SyncSettingsViewController: ScanOrPasteCodeViewModelDelegate {
}

func syncCodeEntered(code: String) async -> Bool {
print(#function, code)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more unneeded print statement

do {
try await syncService.login(recoveryKey: code, deviceName: deviceName, deviceType: deviceType)
navigationController?.topViewController?.dismiss(animated: true)
showDeviceConnected()
return true
guard let syncCode = try? SyncCode.decodeBase64String(code) else {
return false
}

if let recoveryKey = syncCode.recovery {
try await syncService.login(recoveryKey, deviceName: deviceName, deviceType: deviceType)
navigationController?.topViewController?.dismiss(animated: true)
showDeviceConnected()
return true
}

// TODO handle connect code
} catch {
if !(error is SyncError) {
assertionFailure(error.localizedDescription)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ public class ScanOrPasteCodeViewModel: ObservableObject {
self.isInRecoveryMode = isInRecoveryMode
}

func codeScanned(_ code: String) {
Task {
await delegate?.syncCodeEntered(code: code)
}
func codeScanned(_ code: String) async -> Bool {
return await delegate?.syncCodeEntered(code: code) == true
}

func cameraUnavailable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import AVFoundation

struct QRCodeScannerView: UIViewRepresentable {

var onQRCodeScanned: (String) -> Void
var onQRCodeScanned: (String) async -> Bool
var onCameraUnavailable: () -> Void

func makeCoordinator() -> Coordinator {
Expand All @@ -47,6 +47,7 @@ struct QRCodeScannerView: UIViewRepresentable {
let session: AVCaptureSession
let metadataOutput = AVCaptureMetadataOutput()
let cameraView: QRCodeScannerView
var captureCodes = true

init(_ cameraView: QRCodeScannerView) {
self.cameraView = cameraView
Expand Down Expand Up @@ -94,12 +95,17 @@ struct QRCodeScannerView: UIViewRepresentable {

assert(Thread.isMainThread)

guard metadataObjects.count == 1,
guard captureCodes,
metadataObjects.count == 1,
let codeObject = metadataObjects[0] as? AVMetadataMachineReadableCodeObject,
let code = codeObject.stringValue else { return }

// TODO check that calling this with a valid code ends camera entry - it should since it all should happen on the main thread
cameraView.onQRCodeScanned(code)
captureCodes = false
Task { @MainActor in
if await cameraView.onQRCodeScanned(code) {
captureCodes = true
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct ScanOrPasteCodeView: View {
Group {
if model.showCamera {
QRCodeScannerView {
model.codeScanned($0)
return await model.codeScanned($0)
} onCameraUnavailable: {
model.cameraUnavailable()
}
Expand Down