Selaa lähdekoodia

fixed alert retain cycle

nayooti 5 vuotta sitten
vanhempi
commit
9dcf3a5650

+ 3 - 12
deltachat-ios/Controller/AccountSetupController.swift

@@ -73,16 +73,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
 
     // MARK: - the progress dialog
 
-    lazy var progressAlert: UIAlertController = {
-        let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)
-        alert.addAction(UIAlertAction(
-            title: String.localized("cancel"),
-            style: .cancel,
-            handler: { _ in
-                self.dcContext.stopOngoingProcess()
-        }))
-        return alert
-    }()
+    var progressAlert: UIAlertController?
 
     // MARK: - cells
 
@@ -584,7 +575,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
 
         print("oAuth-Flag when loggin in: \(dcContext.getAuthFlags())")
         dcContext.configure()
-        showProgressAlert(title: String.localized("login_header"))
+        showProgressAlert(title: String.localized("login_header"), dcContext: dcContext)
     }
 
     @objc func closeButtonPressed() {
@@ -729,7 +720,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
 
             if let file = dcContext.imexHasBackup(filePath: documents[0]) {
                 logger.info("restoring backup: \(file)")
-                showProgressAlert(title: String.localized("import_backup_title"))
+                showProgressAlert(title: String.localized("import_backup_title"), dcContext: dcContext)
                 dcContext.imex(what: DC_IMEX_IMPORT_BACKUP, directory: file)
             }
             else {

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

@@ -41,16 +41,7 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
         return nav
     }()
 
-    lazy var progressAlert: UIAlertController = {
-        let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)
-        alert.addAction(UIAlertAction(
-            title: String.localized("cancel"),
-            style: .cancel,
-            handler: { _ in
-                self.dcContext.stopOngoingProcess()
-        }))
-        return alert
-    }()
+    weak var progressAlert: UIAlertController?
 
     init(dcContext: DcContext) {
         self.dcContext = dcContext
@@ -59,6 +50,10 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
             self.coordinator?.handleQRAccountCreationSuccess()
         }
     }
+
+    deinit {
+        print("WelcomeViewController deinit")
+    }
     
     required init?(coder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
@@ -129,7 +124,7 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
         scannedQrCode = nil
         if success {
             addProgressAlertListener(onSuccess: handleLoginSuccess)
-            showProgressAlert(title: String.localized("login_header"))
+            showProgressAlert(title: String.localized("login_header"), dcContext: dcContext)
             dcContext.configure()
         } else {
             accountCreationErrorAlert()

+ 13 - 8
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -26,12 +26,6 @@ class AppCoordinator: NSObject, Coordinator {
         return tabBarController
     }()
 
-    private lazy var welcomeController: WelcomeViewController = {
-        let welcomeController = WelcomeViewController(dcContext: dcContext)
-        welcomeController.coordinator = self
-        return welcomeController
-    }()
-
     private lazy var loginController: UIViewController = {
         let accountSetupController = AccountSetupController(dcContext: dcContext, editView: false)
         let nav = UINavigationController(rootViewController: accountSetupController)
@@ -83,6 +77,8 @@ class AppCoordinator: NSObject, Coordinator {
         return nav
     }()
 
+
+    private var welcomeController: WelcomeViewController?
     private var profileInfoNavigationController: UINavigationController?
 
     init(window: UIWindow, dcContext: DcContext) {
@@ -145,15 +141,24 @@ class AppCoordinator: NSObject, Coordinator {
         // but even when this changes in ios, we need the reset as we allow account-deletion also in-app.
         UIApplication.shared.applicationIconBadgeNumber = 0
 
-        window.rootViewController = welcomeController
+        let wc = makeWelcomeController()
+        self.welcomeController = wc
+        window.rootViewController = wc
         window.makeKeyAndVisible()
     }
 
     func presentTabBarController() {
         window.rootViewController = tabBarController
+        welcomeController = nil
         window.makeKeyAndVisible()
         showTab(index: chatsTab)
     }
+
+    private func makeWelcomeController() -> WelcomeViewController {
+        let welcomeController = WelcomeViewController(dcContext: dcContext)
+        welcomeController.coordinator = self
+        return welcomeController
+    }
 }
 
 // MARK: - WelcomeCoordinator
@@ -170,7 +175,7 @@ extension AppCoordinator: WelcomeCoordinator {
             loginController.coordinator?.onLoginSuccess = handleLoginSuccess
         }
         loginController.modalPresentationStyle = .fullScreen
-        welcomeController.present(loginController, animated: true, completion: nil)
+        welcomeController?.present(loginController, animated: true, completion: nil)
     }
 
     func handleLoginSuccess() {

+ 19 - 12
deltachat-ios/Handler/ProgressAlertHandler.swift

@@ -2,34 +2,46 @@ import UIKit
 import DcCore
 
 protocol ProgressAlertHandler: UIViewController {
-    var progressAlert: UIAlertController { get }
+    var progressAlert: UIAlertController? { get set }
     var configureProgressObserver: Any? { get set }
-    func showProgressAlert(title: String)
+    func showProgressAlert(title: String, dcContext: DcContext)
     func updateProgressAlertValue(value: Int?)
     func updateProgressAlert(error: String?)
     func updateProgressAlertSuccess(completion: VoidFunction?)
     func addProgressAlertListener(onSuccess: @escaping VoidFunction)
-    func progressAlertWillDismiss()
 }
 
 extension ProgressAlertHandler {
 
-    func showProgressAlert(title: String) {
+    func showProgressAlert(title: String, dcContext: DcContext) {
+        self.progressAlert = makeProgressAlert(dcContext: dcContext)
+        guard let progressAlert = progressAlert else { return }
         progressAlert.actions[0].isEnabled = true
         progressAlert.title = title
         progressAlert.message = String.localized("one_moment")
         present(progressAlert, animated: true, completion: nil)
     }
 
+    private func makeProgressAlert(dcContext: DcContext) -> UIAlertController {
+        let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)
+        alert.addAction(UIAlertAction(
+            title: String.localized("cancel"),
+            style: .cancel,
+            handler: { _ in
+                dcContext.stopOngoingProcess()
+        }))
+        return alert
+    }
+
     func updateProgressAlertValue(value: Int?) {
         if let value = value {
-            progressAlert.message = String.localized("one_moment") + " " + String(value/10) + "%"
+            progressAlert?.message = String.localized("one_moment") + " " + String(value/10) + "%"
         }
     }
 
     func updateProgressAlert(error message: String?) {
         DispatchQueue.main.async(execute: {
-            self.progressAlert.dismiss(animated: false)
+            self.progressAlert?.dismiss(animated: false)
             let errorAlert = UIAlertController(title: String.localized("error"), message: message, preferredStyle: .alert)
             errorAlert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
             self.present(errorAlert, animated: true, completion: nil)
@@ -38,10 +50,9 @@ extension ProgressAlertHandler {
 
     func updateProgressAlertSuccess(completion onComplete: VoidFunction?) {
         updateProgressAlertValue(value: 1000)
-        progressAlertWillDismiss()
         // delay so the user has time to read the success message
         DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
-            self.progressAlert.dismiss(animated: true) {
+            self.progressAlert?.dismiss(animated: true) {
                 onComplete?()
             }
         })
@@ -65,9 +76,5 @@ extension ProgressAlertHandler {
             }
         }
     }
-
-    func progressAlertWillDismiss() {
-        // can be overwritten if needed
-    }
 }