Browse Source

Move import backup btn to welcome screen (#1577)

* move import backup btn to welcome screen

* remove import backup UI elements from AccountSetupController

* import backup within WelcomViewController without transition to AccountSetupController

* remove WelcomeViewController's scrollview adjustment when transitioning from portrait to landscape
cyBerta 3 years ago
parent
commit
30a4b6a647

+ 1 - 1
deltachat-ios.xcodeproj/project.pbxproj

@@ -907,6 +907,7 @@
 				3015634323A003BA00E9DEF4 /* AudioRecorderController.swift */,
 				AE18F28B228C17630007B1BE /* AccountSetup */,
 				AEE56D752253431E007DC082 /* AccountSetupController.swift */,
+				AE76E5ED242BF2EA003CF461 /* WelcomeViewController.swift */,
 				AE0D26FC1FB1FE88002FAFCE /* ChatListController.swift */,
 				7092474020B3869500AF8799 /* ContactDetailViewController.swift */,
 				AE9DAF0C22C1215D004C9591 /* EditContactController.swift */,
@@ -935,7 +936,6 @@
 				B20462E52440C99600367A57 /* SettingsAutodelSetController.swift */,
 				302D5453268B84CB00A8B271 /* SettingsVideoChatViewController.swift */,
 				30DAF71B275901610073C154 /* SettingsBackgroundSelectionController.swift */,
-				AE76E5ED242BF2EA003CF461 /* WelcomeViewController.swift */,
 				AE8F503424753DFE007FEE0B /* GalleryViewController.swift */,
 				30734325249A280B00BF9AD1 /* MediaQualityController.swift */,
 				30860EE826F49E64002651A6 /* DownloadOnDemandViewController.swift */,

+ 1 - 79
deltachat-ios/Controller/AccountSetupController.swift

@@ -6,9 +6,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
     private var dcContext: DcContext
     private let dcAccounts: DcAccounts
     private var skipOauth = false
-    private var backupProgressObserver: NSObjectProtocol?
     var progressObserver: NSObjectProtocol?
-    var onProgressSuccess: VoidFunction? // not needed here
     var onLoginSuccess: (() -> Void)?
 
     private var oauth2Observer: NSObjectProtocol?
@@ -26,7 +24,6 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
     private let tagSmtpPasswordCell = 10
     private let tagSmtpSecurityCell = 11
     private let tagCertCheckCell = 12
-    private let tagRestoreCell = 14
     private let tagViewLogCell = 15
 
     private let tagTextFieldEmail = 100
@@ -43,12 +40,10 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
 
     let basicSection = 100
     let advancedSection = 200
-    let restoreSection = 300
     let folderSection = 400
     private var sections = [Int]()
 
     private lazy var basicSectionCells: [UITableViewCell] = [emailCell, passwordCell]
-    private lazy var restoreCells: [UITableViewCell] = [restoreCell]
     private lazy var advancedSectionCells: [UITableViewCell] = [
         advancedShowCell,
         imapSecurityCell,
@@ -104,14 +99,6 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
         return cell
     }()
 
-    private lazy var restoreCell: UITableViewCell = {
-        let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
-        cell.textLabel?.text = String.localized("import_backup_title")
-        cell.accessoryType = .disclosureIndicator
-        cell.tag = tagRestoreCell
-        return cell
-    }()
-
     lazy var advancedShowCell: UITableViewCell = {
         let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
         cell.textLabel?.text = String.localized("menu_advanced")
@@ -316,6 +303,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
         return button
     }()
 
+
     // MARK: - constructor
     init(dcAccounts: DcAccounts, editView: Bool) {
         self.editView = editView
@@ -326,8 +314,6 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
         self.sections.append(advancedSection)
         if editView {
             self.sections.append(folderSection)
-        } else {
-            self.sections.append(restoreSection)
         }
 
         super.init(style: .grouped)
@@ -369,9 +355,6 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
     override func viewDidDisappear(_: Bool) {
 
         let nc = NotificationCenter.default
-        if let backupProgressObserver = self.backupProgressObserver {
-            nc.removeObserver(backupProgressObserver)
-        }
         if let configureProgressObserver = self.progressObserver {
             nc.removeObserver(configureProgressObserver)
         }
@@ -388,8 +371,6 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
     override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
         if sections[section] == basicSection {
             return basicSectionCells.count
-        } else if sections[section] == restoreSection {
-            return restoreCells.count
         } else if sections[section] == folderSection {
             return folderCells.count
         } else {
@@ -441,8 +422,6 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
 
         if sections[section] == basicSection {
             return basicSectionCells[row]
-        } else if sections[section] == restoreSection {
-            return restoreCells[row]
         } else if sections[section] == folderSection {
             return folderCells[row]
         } else {
@@ -460,9 +439,6 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
                     _ = showOAuthAlertIfNeeded(emailAddress: emailAdress, handleCancel: nil)
                 }
             }
-        case tagRestoreCell:
-            tableView.reloadData() // otherwise the disclosureIndicator may stay selected
-            restoreBackup()
         case tagAdvancedCell:
             toggleAdvancedSection()
         case tagImapSecurityCell:
@@ -658,28 +634,6 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
         }
     }
 
-    private func addProgressHudBackupListener() {
-        let nc = NotificationCenter.default
-        backupProgressObserver = nc.addObserver(
-            forName: dcNotificationImexProgress,
-            object: nil,
-            queue: nil
-        ) {
-            notification in
-            if let ui = notification.userInfo {
-                if ui["error"] as! Bool {
-                    self.dcAccounts.startIo()
-                    self.updateProgressAlert(error: ui["errorMessage"] as? String)
-                } else if ui["done"] as! Bool {
-                    self.dcAccounts.startIo()
-                    self.updateProgressAlertSuccess(completion: self.handleLoginSuccess)
-                } else {
-                    self.updateProgressAlertValue(value: ui["progress"] as? Int)
-                }
-            }
-        }
-    }
-
     private func evaluateAdvancedSetup() {
         for cell in advancedSectionCells {
             if let textFieldCell = cell as? TextFieldCell {
@@ -705,38 +659,6 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
         }
     }
 
-    private func restoreBackup() {
-        logger.info("restoring backup")
-        if dcContext.isConfigured() {
-            return
-        }
-        addProgressHudBackupListener()
-        let documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
-        if !documents.isEmpty {
-            logger.info("looking for backup in: \(documents[0])")
-
-            if let file = dcContext.imexHasBackup(filePath: documents[0]) {
-                logger.info("restoring backup: \(file)")
-                showProgressAlert(title: String.localized("import_backup_title"), dcContext: dcContext)
-                dcAccounts.stopIo()
-                dcContext.imex(what: DC_IMEX_IMPORT_BACKUP, directory: file)
-            }
-            else {
-                let alert = UIAlertController(
-                    title: String.localized("import_backup_title"),
-                    message: String.localizedStringWithFormat(
-                        String.localized("import_backup_no_backup_found"),
-                        "➔ Mac-Finder or iTunes ➔ iPhone ➔ " + String.localized("files") + " ➔ Delta Chat"), // iTunes was used up to Maverick 10.4
-                    preferredStyle: .alert)
-                alert.addAction(UIAlertAction(title: String.localized("ok"), style: .cancel))
-                present(alert, animated: true)
-            }
-        } else {
-            logger.error("no documents directory found")
-        }
-
-    }
-
     private func handleLoginSuccess() {
         // used when login hud successfully went through
         guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }

+ 72 - 5
deltachat-ios/Controller/WelcomeViewController.swift

@@ -4,6 +4,7 @@ import DcCore
 class WelcomeViewController: UIViewController, ProgressAlertHandler {
     private var dcContext: DcContext
     private let dcAccounts: DcAccounts
+    private var backupProgressObserver: NSObjectProtocol?
     var progressObserver: NSObjectProtocol?
     var onProgressSuccess: VoidFunction?
 
@@ -26,6 +27,10 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
             self.qrCodeReader = qrReader
             self.navigationController?.pushViewController(qrReader, animated: true)
         }
+        view.onImportBackup = { [weak self] in
+            guard let self = self else { return }
+            self.restoreBackup()
+        }
         view.translatesAutoresizingMaskIntoConstraints = false
         return view
     }()
@@ -94,10 +99,6 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
     override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
         super.viewWillTransition(to: size, with: coordinator)
         welcomeView.minContainerHeight = size.height - view.safeAreaInsets.top
-        scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
-        coordinator.animate(alongsideTransition: { [weak self] _ in
-            self?.scrollView.scrollToBottom(animated: true)
-        })
      }
 
     override func viewDidDisappear(_ animated: Bool) {
@@ -106,6 +107,9 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
             nc.removeObserver(observer)
             self.progressObserver = nil
         }
+        if let backupProgressObserver = self.backupProgressObserver {
+            nc.removeObserver(backupProgressObserver)
+        }
     }
 
     // MARK: - setup
@@ -166,6 +170,18 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
         onProgressSuccess?()
     }
 
+    private func handleBackupRestoreSuccess() {
+        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
+
+        if !UserDefaults.standard.bool(forKey: "notifications_disabled") {
+            appDelegate.registerForNotifications()
+        }
+
+        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
+            appDelegate.reloadDcContext()
+        }
+    }
+
     private func accountCreationErrorAlert() {
         let title = dcContext.lastErrorString
         let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
@@ -234,6 +250,57 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
 
         appDelegate.reloadDcContext()
     }
+
+    private func restoreBackup() {
+        logger.info("restoring backup")
+        if dcContext.isConfigured() {
+            return
+        }
+        addProgressHudBackupListener()
+        let documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
+        if !documents.isEmpty {
+            logger.info("looking for backup in: \(documents[0])")
+
+            if let file = dcContext.imexHasBackup(filePath: documents[0]) {
+                logger.info("restoring backup: \(file)")
+                showProgressAlert(title: String.localized("import_backup_title"), dcContext: dcContext)
+                dcAccounts.stopIo()
+                dcContext.imex(what: DC_IMEX_IMPORT_BACKUP, directory: file)
+            } else {
+                let alert = UIAlertController(
+                    title: String.localized("import_backup_title"),
+                    message: String.localizedStringWithFormat(
+                        String.localized("import_backup_no_backup_found"),
+                        "➔ Mac-Finder or iTunes ➔ iPhone ➔ " + String.localized("files") + " ➔ Delta Chat"), // iTunes was used up to Maverick 10.4
+                    preferredStyle: .alert)
+                alert.addAction(UIAlertAction(title: String.localized("ok"), style: .cancel))
+                present(alert, animated: true)
+            }
+        } else {
+            logger.error("no documents directory found")
+        }
+    }
+
+    private func addProgressHudBackupListener() {
+        let nc = NotificationCenter.default
+        backupProgressObserver = nc.addObserver(
+            forName: dcNotificationImexProgress,
+            object: nil,
+            queue: nil
+        ) { notification in
+            if let ui = notification.userInfo {
+                if let error = ui["error"] as? Bool, error {
+                    self.dcAccounts.startIo()
+                    self.updateProgressAlert(error: ui["errorMessage"] as? String)
+                } else if let done = ui["done"] as? Bool, done {
+                    self.dcAccounts.startIo()
+                    self.updateProgressAlertSuccess(completion: self.handleBackupRestoreSuccess)
+                } else {
+                    self.updateProgressAlertValue(value: ui["progress"] as? Int)
+                }
+            }
+        }
+    }
 }
 
 extension WelcomeViewController: QrCodeReaderDelegate {
@@ -334,7 +401,7 @@ class WelcomeContentView: UIView {
     }()
 
     private lazy var buttonStack: UIStackView = {
-        let stack = UIStackView(arrangedSubviews: [loginButton, qrCodeButton /*, importBackupButton */])
+        let stack = UIStackView(arrangedSubviews: [loginButton, qrCodeButton, importBackupButton])
         stack.axis = .vertical
         stack.spacing = 15
         return stack