Эх сурвалжийг харах

inject dcaccount qr code into WelcomViewController, avoid blocking the UI when calling dcContext.setConfigFromQR()

cyberta 3 жил өмнө
parent
commit
3517814e42

+ 5 - 5
deltachat-ios/AppDelegate.swift

@@ -189,9 +189,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
         switch url.scheme?.lowercased() {
         case "dcaccount":
-            _ = self.dcAccounts.add()
-            accountCreationQrCode = url.absoluteString
-            reloadDcContext()
+            self.reloadDcContext(accountCode: url.absoluteString)
             return true
         case "openpgp4fpr":
             // Hack to format url properly
@@ -585,7 +583,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         }
     }
 
-    func reloadDcContext() {
+    /// - Parameters:
+    ///   - accountCode: optional string representation of dcaccounts: url, used to setup a new account
+    func reloadDcContext(accountCode: String? = nil) {
         setStockTranslations()
         locationManager.reloadDcContext()
         notificationManager.reloadDcContext()
@@ -594,7 +594,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         if dcAccounts.getSelected().isConfigured() {
             appCoordinator.resetTabBarRootViewControllers()
         } else {
-            appCoordinator.presentWelcomeController()
+            appCoordinator.presentWelcomeController(accountCode: accountCode)
         }
     }
 

+ 17 - 5
deltachat-ios/Controller/QrPageController.swift

@@ -3,6 +3,7 @@ import DcCore
 
 class QrPageController: UIPageViewController {
     private let dcContext: DcContext
+    private let dcAccounts: DcAccounts
     var progressObserver: NSObjectProtocol?
     var qrCodeReaderController: QrCodeReaderController?
 
@@ -38,8 +39,9 @@ class QrPageController: UIPageViewController {
         return control
     }()
 
-    init(dcContext: DcContext) {
-        self.dcContext = dcContext
+    init(dcAccounts: DcAccounts) {
+        self.dcAccounts = dcAccounts
+        self.dcContext = dcAccounts.getSelected()
         super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [:])
     }
 
@@ -210,9 +212,19 @@ extension QrPageController: QrCodeReaderDelegate {
             present(alert, animated: true, completion: nil)
 
         case DC_QR_ACCOUNT:
-            let alert = UIAlertController(title: String.localized("qraccount_use_on_new_install"), message: nil, preferredStyle: .alert)
-            alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default))
-            present(alert, animated: true)
+            if let domain = qrParsed.text1 {
+                let title = String.localizedStringWithFormat(String.localized("qraccount_ask_create_and_login_another"), domain)
+                let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
+                alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default))
+                alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { [weak self] _ in
+                    guard let self = self else { return }
+                    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
+                        _ = self.dcAccounts.add()
+                        appDelegate.reloadDcContext(accountCode: code)
+                    }
+                }))
+                present(alert, animated: true)
+            }
 
         case DC_QR_WEBRTC_INSTANCE:
             guard let domain = qrParsed.text1 else { return }

+ 21 - 19
deltachat-ios/Controller/WelcomeViewController.swift

@@ -4,6 +4,7 @@ import DcCore
 class WelcomeViewController: UIViewController, ProgressAlertHandler {
     private var dcContext: DcContext
     private let dcAccounts: DcAccounts
+    private let accountCode: String?
     private var backupProgressObserver: NSObjectProtocol?
     var progressObserver: NSObjectProtocol?
     var onProgressSuccess: VoidFunction?
@@ -59,10 +60,11 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
 
     private var qrCodeReader: QrCodeReaderController?
     weak var progressAlert: UIAlertController?
-    
-    init(dcAccounts: DcAccounts) {
+
+    init(dcAccounts: DcAccounts, accountCode: String? = nil) {
         self.dcAccounts = dcAccounts
         self.dcContext = dcAccounts.getSelected()
+        self.accountCode = accountCode
         super.init(nibName: nil, bundle: nil)
         self.navigationItem.title = String.localized(canCancel ? "add_account" : "welcome_desktop")
         onProgressSuccess = { [weak self] in
@@ -89,16 +91,9 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
             navigationItem.leftBarButtonItem = cancelButton
         }
         navigationItem.rightBarButtonItem = moreButton
-    }
-    
-    override func viewDidAppear(_ animated: Bool) {
-        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
-            if !appDelegate.accountCreationQrCode.isEmpty {
-                self.handleQrCode(appDelegate.accountCreationQrCode)
-            }
-            appDelegate.accountCreationQrCode = ""
+        if let accountCode = accountCode {
+            createAccountFromQRCode(qrCode: accountCode)
         }
-        super.viewDidAppear(animated)
     }
 
     override func viewDidLayoutSubviews() {
@@ -160,14 +155,21 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
 
         if accountId != 0 {
             self.dcContext = dcAccounts.get(id: accountId)
-            let success = dcContext.setConfigFromQR(qrCode: qrCode)
-            if success {
-                addProgressAlertListener(dcAccounts: dcAccounts, progressName: dcNotificationConfigureProgress, onSuccess: handleLoginSuccess)
-                showProgressAlert(title: String.localized("login_header"), dcContext: dcContext)
-                dcAccounts.stopIo()
-                dcContext.configure()
-            } else {
-                accountCreationErrorAlert()
+            DispatchQueue.global().async { [weak self] in
+                guard let self = self else { return }
+                let success = self.dcContext.setConfigFromQR(qrCode: qrCode)
+                DispatchQueue.main.async {
+                    if success {
+                        self.addProgressAlertListener(dcAccounts: self.dcAccounts,
+                                                      progressName: dcNotificationConfigureProgress,
+                                                      onSuccess: self.handleLoginSuccess)
+                        self.showProgressAlert(title: String.localized("login_header"), dcContext: self.dcContext)
+                        self.dcAccounts.stopIo()
+                        self.dcContext.configure()
+                    } else {
+                        self.accountCreationErrorAlert()
+                    }
+                }
             }
         }
     }

+ 3 - 3
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -33,7 +33,7 @@ class AppCoordinator {
     }()
 
     private func createQrNavigationController() -> UINavigationController {
-        let root = QrPageController(dcContext: dcAccounts.getSelected())
+        let root = QrPageController(dcAccounts: dcAccounts)
         let nav = UINavigationController(rootViewController: root)
         let settingsImage = UIImage(named: "qr_code")
         nav.tabBarItem = UITabBarItem(title: String.localized("qr_code"), image: settingsImage, tag: qrTab)
@@ -155,8 +155,8 @@ class AppCoordinator {
         UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .semibold)]
     }
 
-    func presentWelcomeController() {
-        loginNavController.setViewControllers([WelcomeViewController(dcAccounts: dcAccounts)], animated: true)
+    func presentWelcomeController(accountCode: String? = nil) {
+        loginNavController.setViewControllers([WelcomeViewController(dcAccounts: dcAccounts, accountCode: accountCode)], animated: true)
         window.rootViewController = loginNavController
         window.makeKeyAndVisible()