Prechádzať zdrojové kódy

basic account switching

B. Petersen 5 rokov pred
rodič
commit
de90aa845b

+ 34 - 1
DcCore/DcCore/Helper/AccountManager.swift

@@ -1,7 +1,7 @@
 import Foundation
 
 public class Account {
-    private let dbName: String
+    let dbName: String // only name, no full path
     public let displayname: String
     public let addr: String
     public let configured: Bool
@@ -54,6 +54,20 @@ public class AccountManager {
                        configured: testContext.isConfigured())
     }
 
+    public func switchAccount(account: Account) -> Bool {
+        if let userDefaults = UserDefaults.shared {
+            let prevDbName = userDefaults.string(forKey: UserDefaults.currAccountDbName) ?? defaultDbName
+
+            // we remember prev_account_db_name in case the account is not configured
+            // and the user want to rollback to the working account
+            userDefaults.set(prevDbName, forKey: UserDefaults.prevAccountDbName)
+            userDefaults.set(account.dbName, forKey: UserDefaults.currAccountDbName)
+            resetDcContext()
+            return true
+        }
+        return false
+    }
+
     private func resetDcContext() {
         // create an empty DcContext object - this will be set up then, starting with
         // getSelectedAccount()
@@ -115,9 +129,28 @@ public class AccountManager {
         return true
     }
 
+    // check if there is an account creation started by beginAccountCreation().
     public func canRollbackAccountCreation() -> Bool {
         guard let userDefaults = UserDefaults.shared else { return false }
         let prevDbName = userDefaults.string(forKey: UserDefaults.prevAccountDbName) ?? ""
         return !prevDbName.isEmpty
     }
+
+    // cancels account creation started by beginAccountCreation(),
+    // returns true when account creation was canceled,
+    // returns false when there is no account to cancel or on errors
+    public func rollbackAccountCreation() -> Bool {
+        if let userDefaults = UserDefaults.shared, let sharedDir = DatabaseHelper().sharedDir {
+            let prevDbName = userDefaults.string(forKey: UserDefaults.prevAccountDbName) ?? ""
+            let inCreationDbName = userDefaults.string(forKey: UserDefaults.currAccountDbName) ?? ""
+
+            if let prevAccount = maybeGetAccount(dbFile: sharedDir.appendingPathComponent(prevDbName).path) {
+                if switchAccount(account: prevAccount) {
+                    // TODO: delete inCreationDbName
+                    return true
+                }
+            }
+        }
+        return false
+    }
 }

+ 7 - 1
deltachat-ios/Controller/SettingsController.swift

@@ -450,8 +450,14 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
         let alert = UIAlertController(title: String.localized("switch_account"), message: nil, preferredStyle: .safeActionSheet)
         for account in accounts {
             var title = account.displayname.isEmpty ? account.addr : "\(account.displayname) (\(account.addr))"
+            title += account.configured ? "" : " (not configured)"
             title += account.current ? " ✓" : ""
-            alert.addAction(UIAlertAction(title: title, style: .default, handler: nil)) // TODO
+            alert.addAction(UIAlertAction(title: title, style: .default, handler: { _ in
+                if AccountManager().switchAccount(account: account), let appDelegate = UIApplication.shared.delegate as? AppDelegate {
+                    appDelegate.installEventHandler()
+                    appDelegate.appCoordinator.presentTabBarController()
+                }
+            }))
         }
 
         // delete account

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

@@ -141,6 +141,12 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
     }
 
     @objc func rollbackButton() {
+        if AccountManager().rollbackAccountCreation() {
+            if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
+                appDelegate.installEventHandler()
+                appDelegate.appCoordinator.presentTabBarController()
+            }
+        }
     }
 }