WIP: scan qr code screen

pull/891/head
ryanzhao 2 years ago
parent 42bfb63c19
commit 2910dad390

@ -45,9 +45,10 @@ struct LoadAccountView: View {
} }
} }
private func continueWithSeed(seed: Data) { private func continueWithSeed(seed: Data, onError: (() -> ())?) {
if (seed.count != 16) { if (seed.count != 16) {
errorString = "recovery_password_error_generic".localized() errorString = "recovery_password_error_generic".localized()
onError?()
return return
} }
let (ed25519KeyPair, x25519KeyPair) = try! Identity.generate(from: seed) let (ed25519KeyPair, x25519KeyPair) = try! Identity.generate(from: seed)
@ -65,9 +66,9 @@ struct LoadAccountView: View {
self.host.controller?.navigationController?.pushViewController(viewController, animated: true) self.host.controller?.navigationController?.pushViewController(viewController, animated: true)
} }
func continueWithhexEncodedSeed() { func continueWithhexEncodedSeed(onError: (() -> ())?) {
let seed = Data(hex: hexEncodedSeed) let seed = Data(hex: hexEncodedSeed)
continueWithSeed(seed: seed) continueWithSeed(seed: seed, onError: onError)
} }
func continueWithMnemonic() { func continueWithMnemonic() {
@ -91,7 +92,7 @@ struct LoadAccountView: View {
return return
} }
let seed = Data(hex: hexEncodedSeed) 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 hexEncodedSeed: String
@Binding var error: String? @Binding var error: String?
@State var hasCameraAccess: Bool = (AVCaptureDevice.authorizationStatus(for: .video) == .authorized) @State var hasCameraAccess: Bool = (AVCaptureDevice.authorizationStatus(for: .video) == .authorized)
var continueWithhexEncodedSeed: (() -> Void)? var continueWithhexEncodedSeed: (((() -> ())?) -> Void)?
init( init(
_ hexEncodedSeed: Binding<String>, _ hexEncodedSeed: Binding<String>,
error: Binding<String?>, error: Binding<String?>,
continueWithhexEncodedSeed: (() -> Void)? continueWithhexEncodedSeed: (((() -> ())?) -> Void)?
) { ) {
self._hexEncodedSeed = hexEncodedSeed self._hexEncodedSeed = hexEncodedSeed
self._error = error self._error = error
@ -262,12 +263,30 @@ struct ScanQRCodeView: View{
ZStack{ ZStack{
if hasCameraAccess { if hasCameraAccess {
VStack { VStack {
QRCodeScanningVC_SwiftUI(scanDelegate: nil) QRCodeScanningVC_SwiftUI { string, onError in
hexEncodedSeed = string
continueWithhexEncodedSeed?(onError)
}
} }
.frame( .frame(
maxWidth: .infinity, maxWidth: .infinity,
maxHeight: .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 { } else {
VStack( VStack(
alignment: .center, alignment: .center,

@ -211,20 +211,38 @@ import SwiftUI
struct QRCodeScanningVC_SwiftUI: UIViewControllerRepresentable { struct QRCodeScanningVC_SwiftUI: UIViewControllerRepresentable {
typealias UIViewControllerType = QRCodeScanningViewController typealias UIViewControllerType = QRCodeScanningViewController
public weak var scanDelegate: QRScannerDelegate? let scanQRCodeVC = QRCodeScanningViewController()
var didDetectQRCode: (String, (() -> ())?) -> ()
public init(scanDelegate: QRScannerDelegate?) {
self.scanDelegate = scanDelegate
}
func makeUIViewController(context: Context) -> QRCodeScanningViewController { func makeUIViewController(context: Context) -> QRCodeScanningViewController {
let scanQRCodeVC = QRCodeScanningViewController()
scanQRCodeVC.scanDelegate = scanDelegate
return scanQRCodeVC return scanQRCodeVC
} }
func updateUIViewController(_ scanQRCodeVC: QRCodeScanningViewController, context: Context) { func updateUIViewController(_ scanQRCodeVC: QRCodeScanningViewController, context: Context) {
scanQRCodeVC.startCapture() 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)
}
}
} }

Loading…
Cancel
Save