diff --git a/Session/Onboarding/LoadAccountView.swift b/Session/Onboarding/LoadAccountView.swift index cfa249d34..722cc2fef 100644 --- a/Session/Onboarding/LoadAccountView.swift +++ b/Session/Onboarding/LoadAccountView.swift @@ -45,9 +45,10 @@ struct LoadAccountView: View { } } - private func continueWithSeed(seed: Data) { + private func continueWithSeed(seed: Data, onError: (() -> ())?) { if (seed.count != 16) { errorString = "recovery_password_error_generic".localized() + onError?() return } let (ed25519KeyPair, x25519KeyPair) = try! Identity.generate(from: seed) @@ -65,9 +66,9 @@ struct LoadAccountView: View { self.host.controller?.navigationController?.pushViewController(viewController, animated: true) } - func continueWithhexEncodedSeed() { + func continueWithhexEncodedSeed(onError: (() -> ())?) { let seed = Data(hex: hexEncodedSeed) - continueWithSeed(seed: seed) + continueWithSeed(seed: seed, onError: onError) } func continueWithMnemonic() { @@ -91,7 +92,7 @@ struct LoadAccountView: View { return } let seed = Data(hex: hexEncodedSeed) - continueWithSeed(seed: seed) + continueWithSeed(seed: seed, onError: nil) } } @@ -241,17 +242,17 @@ struct EnterRecoveryPasswordView: View{ } } -struct ScanQRCodeView: View{ +struct ScanQRCodeView: View { @Binding var hexEncodedSeed: String @Binding var error: String? @State var hasCameraAccess: Bool = (AVCaptureDevice.authorizationStatus(for: .video) == .authorized) - var continueWithhexEncodedSeed: (() -> Void)? + var continueWithhexEncodedSeed: (((() -> ())?) -> Void)? init( _ hexEncodedSeed: Binding, error: Binding, - continueWithhexEncodedSeed: (() -> Void)? + continueWithhexEncodedSeed: (((() -> ())?) -> Void)? ) { self._hexEncodedSeed = hexEncodedSeed self._error = error @@ -262,12 +263,30 @@ struct ScanQRCodeView: View{ ZStack{ if hasCameraAccess { VStack { - QRCodeScanningVC_SwiftUI(scanDelegate: nil) + QRCodeScanningVC_SwiftUI { string, onError in + hexEncodedSeed = string + continueWithhexEncodedSeed?(onError) + } } .frame( maxWidth: .infinity, maxHeight: .infinity ) + + if error?.isEmpty == false { + VStack { + Spacer() + + Text(error!) + .font(.system(size: Values.verySmallFontSize)) + .foregroundColor(themeColor: .textPrimary) + .multilineTextAlignment(.center) + .frame( + width: 320, + height: 44 + ) + } + } } else { VStack( alignment: .center, diff --git a/Session/Shared/QRCodeScanningViewController.swift b/Session/Shared/QRCodeScanningViewController.swift index 56efcc60a..5ed5a0b09 100644 --- a/Session/Shared/QRCodeScanningViewController.swift +++ b/Session/Shared/QRCodeScanningViewController.swift @@ -211,20 +211,38 @@ import SwiftUI struct QRCodeScanningVC_SwiftUI: UIViewControllerRepresentable { typealias UIViewControllerType = QRCodeScanningViewController - public weak var scanDelegate: QRScannerDelegate? - - public init(scanDelegate: QRScannerDelegate?) { - self.scanDelegate = scanDelegate - } + let scanQRCodeVC = QRCodeScanningViewController() + var didDetectQRCode: (String, (() -> ())?) -> () func makeUIViewController(context: Context) -> QRCodeScanningViewController { - let scanQRCodeVC = QRCodeScanningViewController() - scanQRCodeVC.scanDelegate = scanDelegate - return scanQRCodeVC } func updateUIViewController(_ scanQRCodeVC: QRCodeScanningViewController, context: Context) { scanQRCodeVC.startCapture() } + + func makeCoordinator() -> Coordinator { + Coordinator( + scanQRCodeVC: scanQRCodeVC, + didDetectQRCode: didDetectQRCode + ) + } + + class Coordinator: NSObject, QRScannerDelegate { + var didDetectQRCode: (String, (() -> ())?) -> () + + init( + scanQRCodeVC: QRCodeScanningViewController, + didDetectQRCode: @escaping (String, (() -> ())?) -> () + ) { + self.didDetectQRCode = didDetectQRCode + super.init() + scanQRCodeVC.scanDelegate = self + } + + func controller(_ controller: QRCodeScanningViewController, didDetectQRCodeWith string: String, onError: (() -> ())?) { + didDetectQRCode(string, onError) + } + } }