Browse Source

alert is now presented above qrReader

nayooti 5 years ago
parent
commit
9794754ce4

+ 2 - 3
deltachat-ios/Controller/QrCodeReaderController.swift

@@ -136,9 +136,8 @@ extension QrCodeReaderController: AVCaptureMetadataOutputObjectsDelegate {
         if let metadataObj = metadataObjects[0] as? AVMetadataMachineReadableCodeObject {
             if supportedCodeTypes.contains(metadataObj.type) {
                 if metadataObj.stringValue != nil {
-                    dismiss(animated: true) {
-                        self.delegate?.handleQrCode(metadataObj.stringValue!)
-                    }
+                    self.delegate?.handleQrCode(metadataObj.stringValue!)
+                    self.captureSession.stopRunning()
                 }
             }
         }

+ 72 - 68
deltachat-ios/Controller/QrViewController.swift

@@ -241,76 +241,80 @@ class QrViewController: UITableViewController {
     }
 }
 
-//MARK: - QRCodeDelegate
+// MARK: - QRCodeDelegate
 extension QrViewController: QrCodeReaderDelegate {
 
-      func handleQrCode(_ code: String) {
-          let qrParsed: DcLot = self.dcContext.checkQR(qrCode: code)
-          let state = Int32(qrParsed.state)
-          switch state {
-          case DC_QR_ASK_VERIFYCONTACT:
-              let nameAndAddress = DcContact(id: qrParsed.id).nameNAddr
-              joinSecureJoin(alertMessage: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), nameAndAddress), code: code)
-
-          case DC_QR_ASK_VERIFYGROUP:
-              let groupName = qrParsed.text1 ?? "ErrGroupName"
-              joinSecureJoin(alertMessage: String.localizedStringWithFormat(String.localized("qrscan_ask_join_group"), groupName), code: code)
-
-          case DC_QR_FPR_WITHOUT_ADDR:
-              let msg = String.localized("qrscan_no_addr_found") + "\n\n" +
-                  String.localized("qrscan_fingerprint_label") + ":\n" + (qrParsed.text1 ?? "")
-              let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
-              alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
-              present(alert, animated: true, completion: nil)
-
-          case DC_QR_FPR_MISMATCH:
-              let nameAndAddress = DcContact(id: qrParsed.id).nameNAddr
-              let msg = String.localizedStringWithFormat(String.localized("qrscan_fingerprint_mismatch"), nameAndAddress)
-              let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
-              alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
-              present(alert, animated: true, completion: nil)
-
-          case DC_QR_ADDR, DC_QR_FPR_OK:
-              let nameAndAddress = DcContact(id: qrParsed.id).nameNAddr
-              let msg = String.localizedStringWithFormat(String.localized(state==DC_QR_ADDR ? "ask_start_chat_with" : "qrshow_x_verified"), nameAndAddress)
-              let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
-              alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
-                  let chatId = self.dcContext.createChatByContactId(contactId: qrParsed.id)
-                  self.coordinator?.showChat(chatId: chatId)
-              }))
-              alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
-              present(alert, animated: true, completion: nil)
-
-          case DC_QR_TEXT:
-              let msg = String.localizedStringWithFormat(String.localized("qrscan_contains_text"), qrParsed.text1 ?? "")
-              let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
-              alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
-              present(alert, animated: true, completion: nil)
-
-          case DC_QR_URL:
-              let url = qrParsed.text1 ?? ""
-              let msg = String.localizedStringWithFormat(String.localized("qrscan_contains_url"), url)
-              let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
-              alert.addAction(UIAlertAction(title: String.localized("open"), style: .default, handler: { _ in
-                  if let url = URL(string: url) {
-                      UIApplication.shared.open(url)
-                  }
-              }))
-              alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
-              present(alert, animated: true, completion: nil)
-
-          default:
-              var msg = String.localizedStringWithFormat(String.localized("qrscan_contains_text"), code)
-              if state == DC_QR_ERROR {
-                  if let errorMsg = qrParsed.text1 {
-                      msg = errorMsg + "\n\n" + msg
-                  }
-              }
-              let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
-              alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
-              present(alert, animated: true, completion: nil)
-          }
-      }
+    func handleQrCode(_ code: String) {
+        qrCodeReaderController.dismiss(animated: true) {
+            self.processQrCode(code)
+        }
+    }
 
+    private func processQrCode(_ code: String) {
+        let qrParsed: DcLot = self.dcContext.checkQR(qrCode: code)
+        let state = Int32(qrParsed.state)
+        switch state {
+        case DC_QR_ASK_VERIFYCONTACT:
+            let nameAndAddress = DcContact(id: qrParsed.id).nameNAddr
+            joinSecureJoin(alertMessage: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), nameAndAddress), code: code)
+
+        case DC_QR_ASK_VERIFYGROUP:
+            let groupName = qrParsed.text1 ?? "ErrGroupName"
+            joinSecureJoin(alertMessage: String.localizedStringWithFormat(String.localized("qrscan_ask_join_group"), groupName), code: code)
+
+        case DC_QR_FPR_WITHOUT_ADDR:
+            let msg = String.localized("qrscan_no_addr_found") + "\n\n" +
+                String.localized("qrscan_fingerprint_label") + ":\n" + (qrParsed.text1 ?? "")
+            let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
+            alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
+            present(alert, animated: true, completion: nil)
+
+        case DC_QR_FPR_MISMATCH:
+            let nameAndAddress = DcContact(id: qrParsed.id).nameNAddr
+            let msg = String.localizedStringWithFormat(String.localized("qrscan_fingerprint_mismatch"), nameAndAddress)
+            let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
+            alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
+            present(alert, animated: true, completion: nil)
+
+        case DC_QR_ADDR, DC_QR_FPR_OK:
+            let nameAndAddress = DcContact(id: qrParsed.id).nameNAddr
+            let msg = String.localizedStringWithFormat(String.localized(state==DC_QR_ADDR ? "ask_start_chat_with" : "qrshow_x_verified"), nameAndAddress)
+            let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
+            alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
+                let chatId = self.dcContext.createChatByContactId(contactId: qrParsed.id)
+                self.coordinator?.showChat(chatId: chatId)
+            }))
+            alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
+            present(alert, animated: true, completion: nil)
+
+        case DC_QR_TEXT:
+            let msg = String.localizedStringWithFormat(String.localized("qrscan_contains_text"), qrParsed.text1 ?? "")
+            let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
+            alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
+            present(alert, animated: true, completion: nil)
+
+        case DC_QR_URL:
+            let url = qrParsed.text1 ?? ""
+            let msg = String.localizedStringWithFormat(String.localized("qrscan_contains_url"), url)
+            let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
+            alert.addAction(UIAlertAction(title: String.localized("open"), style: .default, handler: { _ in
+                if let url = URL(string: url) {
+                    UIApplication.shared.open(url)
+                }
+            }))
+            alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
+            present(alert, animated: true, completion: nil)
 
+        default:
+            var msg = String.localizedStringWithFormat(String.localized("qrscan_contains_text"), code)
+            if state == DC_QR_ERROR {
+                if let errorMsg = qrParsed.text1 {
+                    msg = errorMsg + "\n\n" + msg
+                }
+            }
+            let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
+            alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
+            present(alert, animated: true, completion: nil)
+        }
+    }
 }

+ 28 - 6
deltachat-ios/Controller/WelcomeViewController.swift

@@ -22,12 +22,19 @@ class WelcomeViewController: UIViewController {
         }
         view.onScanQRCode = {
             [unowned self] in
-            self.coordinator?.showQR()
+            self.showQRReader()
         }
         view.translatesAutoresizingMaskIntoConstraints = false
         return view
     }()
 
+    private lazy var qrCodeReaderNav: UIViewController = {
+        let controller = QrCodeReaderController()
+        controller.delegate = self
+        let nav = UINavigationController(rootViewController: controller)
+        return nav
+    }()
+
     // will be shown while transitioning to tabBarController
     private var activityIndicator: UIActivityIndicatorView = {
         let view: UIActivityIndicatorView
@@ -105,7 +112,13 @@ class WelcomeViewController: UIViewController {
         scrollView.isHidden = transitioning
     }
 
-    func createAccountFromQRCode(_ action: UIAlertAction) {
+    // MARK: - actions
+
+    func showQRReader() {
+        present(qrCodeReaderNav, animated: true)
+    }
+
+    func createAccountFromQRCode() {
         guard let code = scannedQrCode else {
             return
         }
@@ -113,15 +126,17 @@ class WelcomeViewController: UIViewController {
         scannedQrCode = nil
         if success {
             coordinator?.handleLoginSuccess()
+        } else {
+            setTransitionState(false)
         }
     }
 }
 
 extension WelcomeViewController: QrCodeReaderDelegate {
     func handleQrCode(_ code: String) {
-        self.scannedQrCode = code
         let lot = dcContext.checkQR(qrCode: code)
         if let domain = lot.text1, lot.state == DC_QR_ACCOUNT {
+            self.scannedQrCode = code
             confirmAccountCreationAlert(accountDomain: domain)
         }
     }
@@ -135,17 +150,24 @@ extension WelcomeViewController: QrCodeReaderDelegate {
             title: String.localized("cancel"),
             style: .cancel,
             handler: { [unowned self] _ in
-                self.scannedQrCode = nil
+                self.qrCodeReaderNav.dismiss(animated: true) {
+                    self.scannedQrCode = nil
+                }
             }
         )
         let okAction = UIAlertAction(
             title: String.localized("ok"),
             style: .default,
-            handler: createAccountFromQRCode(_:)
+            handler: { [unowned self] _ in
+                self.setTransitionState(true)
+                self.qrCodeReaderNav.dismiss(animated: true) {
+                    self.createAccountFromQRCode()
+                }
+            }
         )
         alert.addAction(okAction)
         alert.addAction(cancelAction)
-        present(alert, animated: true)
+        qrCodeReaderNav.present(alert, animated: true)
     }
 }
 

+ 2 - 10
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -173,18 +173,11 @@ extension AppCoordinator: WelcomeCoordinator {
         loginController.modalPresentationStyle = .fullScreen
         welcomeController.present(loginController, animated: true, completion: nil)
     }
-
-    func showQR() {
-        let qrController = QrCodeReaderController()
-        qrController.delegate = welcomeController
-        let nav = UINavigationController(rootViewController: qrController)
-        nav.modalPresentationStyle = .fullScreen
-        welcomeController.present(nav, animated: true)
-    }
+    
 
     func handleLoginSuccess() {
         welcomeController.setTransitionState(true) // this will hide welcomeController's content
-        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
+        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
             self.loginController.dismiss(animated: true) { // this is ignored if loginController is not shown
                 self.presentTabBarController()
                 self.welcomeController.setTransitionState(false)
@@ -880,6 +873,5 @@ protocol EditContactCoordinatorProtocol: class {
 
 protocol WelcomeCoordinator: class {
     func showLogin()
-    func showQR()
     func handleLoginSuccess()
 }