浏览代码

linked qrScanner to welcomeScreen - redirect after login success

nayooti 5 年之前
父节点
当前提交
f9d9a56922

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

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

+ 76 - 77
deltachat-ios/Controller/QrViewController.swift

@@ -1,7 +1,7 @@
 import Foundation
 import UIKit
 
-class QrViewController: UITableViewController, QrCodeReaderDelegate {
+class QrViewController: UITableViewController {
     private let rowQRCode = 0
     private let rowScanQR = 1
 
@@ -129,82 +129,7 @@ class QrViewController: UITableViewController, QrCodeReaderDelegate {
         }
     }
 
-    //QRCodeDelegate
-    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)
-        }
-    }
-
-    private func joinSecureJoin(alertMessage: String, code: String) {
+      private func joinSecureJoin(alertMessage: String, code: String) {
         let alert = UIAlertController(title: alertMessage,
                                       message: nil,
                                       preferredStyle: .alert)
@@ -315,3 +240,77 @@ class QrViewController: UITableViewController, QrCodeReaderDelegate {
         navigationController?.pushViewController(chatVC, animated: true)
     }
 }
+
+//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)
+          }
+      }
+
+
+}

+ 56 - 0
deltachat-ios/Controller/WelcomeViewController.swift

@@ -4,6 +4,10 @@ class WelcomeViewController: UIViewController {
 
     weak var coordinator: WelcomeCoordinator?
 
+    private let dcContext: DcContext
+
+    private var scannedQrCode: String?
+
     private lazy var scrollView: UIScrollView = {
         let scrollView = UIScrollView()
         scrollView.showsVerticalScrollIndicator = false
@@ -37,6 +41,15 @@ class WelcomeViewController: UIViewController {
         return view
     }()
 
+    init(dcContext: DcContext) {
+        self.dcContext = dcContext
+        super.init(nibName: nil, bundle: nil)
+    }
+
+    required init?(coder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
     // MARK: - lifecycle
     override func viewDidLoad() {
         super.viewDidLoad()
@@ -91,6 +104,49 @@ class WelcomeViewController: UIViewController {
         activityIndicator.isHidden = !transitioning
         scrollView.isHidden = transitioning
     }
+
+    func createAccountFromQRCode(_ action: UIAlertAction) {
+        guard let code = scannedQrCode else {
+            return
+        }
+        let success = dcContext.configureAccountFromQR(qrCode: code)
+        scannedQrCode = nil
+        if success {
+            coordinator?.handleLoginSuccess()
+        }
+    }
+}
+
+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 {
+            confirmAccountCreationAlert(accountDomain: domain)
+        }
+    }
+
+    private func confirmAccountCreationAlert(accountDomain domain: String) {
+        let title = "Create new e-mail address on \"\(domain)\" and log in there?"
+
+        let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
+
+        let cancelAction = UIAlertAction(
+            title: String.localized("cancel"),
+            style: .cancel,
+            handler: { [unowned self] _ in
+                self.scannedQrCode = nil
+            }
+        )
+        let okAction = UIAlertAction(
+            title: String.localized("ok"),
+            style: .default,
+            handler: createAccountFromQRCode(_:)
+        )
+        alert.addAction(okAction)
+        alert.addAction(cancelAction)
+        present(alert, animated: true)
+    }
 }
 
 class WelcomeContentView: UIView {

+ 7 - 4
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -24,7 +24,7 @@ class AppCoordinator: NSObject, Coordinator {
     }()
 
     private lazy var welcomeController: WelcomeViewController = {
-        let welcomeController = WelcomeViewController()
+        let welcomeController = WelcomeViewController(dcContext: dcContext)
         welcomeController.coordinator = self
         return welcomeController
     }()
@@ -176,14 +176,16 @@ extension AppCoordinator: WelcomeCoordinator {
 
     func showQR() {
         let qrController = QrCodeReaderController()
+        qrController.delegate = welcomeController
         let nav = UINavigationController(rootViewController: qrController)
+        nav.modalPresentationStyle = .fullScreen
         welcomeController.present(nav, animated: true)
     }
 
-    private func handleLoginSuccess() {
+    func handleLoginSuccess() {
         welcomeController.setTransitionState(true) // this will hide welcomeController's content
-        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
-            self.loginController.dismiss(animated: true) {
+        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
+            self.loginController.dismiss(animated: true) { // this is ignored if loginController is not shown
                 self.presentTabBarController()
                 self.welcomeController.setTransitionState(false)
             }
@@ -879,4 +881,5 @@ protocol EditContactCoordinatorProtocol: class {
 protocol WelcomeCoordinator: class {
     func showLogin()
     func showQR()
+    func handleLoginSuccess()
 }

+ 10 - 0
deltachat-ios/DC/Wrapper.swift

@@ -139,6 +139,16 @@ class DcContext {
         return DcLot(dc_check_qr(contextPointer, qrCode))
     }
 
+    func configureAccountFromQR(qrCode: String) -> Bool {
+        let state = checkQR(qrCode: qrCode).state
+        if state != DC_QR_ACCOUNT {
+            return false
+        }
+        let success = dc_set_config_from_qr(contextPointer, qrCode)
+        configure()
+        return success == 1
+    }
+
     func stopOngoingProcess() {
         dc_stop_ongoing_process(contextPointer)
     }