Browse Source

allow adding accounts on database-level

B. Petersen 4 years ago
parent
commit
bcd67815fd

+ 1 - 0
DcCore/DcCore/Extensions/UserDefaults+Extensions.swift

@@ -2,6 +2,7 @@ import Foundation
 public extension UserDefaults {
     static var hasExtensionAttemptedToSend = "hasExtensionAttemptedToSend"
     static var currAccountDbName = "currAccountDbName"
+    static var prevAccountDbName = "prevAccountDbName"
     static var shared: UserDefaults? {
         return UserDefaults(suiteName: "group.chat.delta.ios")
     }

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

@@ -23,6 +23,24 @@ public class AccountManager {
     public init() {
     }
 
+    private func getUniqueDbName() -> String? {
+        let filemanager = FileManager.default
+        let databaseHelper = DatabaseHelper()
+        if databaseHelper.updateSucceeded(), let sharedDir = databaseHelper.sharedDir {
+            var index = 1
+            while true {
+                let test = String(format: "messenger-%d.db", index)
+                let testBlobdir = String(format: "messenger-%d.db-blobs", index)
+                if !filemanager.fileExists(atPath: sharedDir.appendingPathComponent(test).path) &&
+                   !filemanager.fileExists(atPath: sharedDir.appendingPathComponent(testBlobdir).path) {
+                    return test
+                }
+                index += 1
+            }
+        }
+        return nil
+    }
+
     private func maybeGetAccount(dbFile: String) -> Account? {
         let testContext = DcContext()
         testContext.openDatabase(dbFile: dbFile)
@@ -37,7 +55,10 @@ public class AccountManager {
     }
 
     private func resetDcContext() {
+        // create an empty DcContext object - this will be set up then, starting with
+        // getSelectedAccount()
 
+        // TODO
     }
 
 
@@ -80,7 +101,16 @@ public class AccountManager {
 
     // pause the current account and let the user create a new one.
     // this function is not needed on the very first account creation.
-    public func beginAccountCreation() {
+    public func beginAccountCreation() -> Bool {
+        guard let userDefaults = UserDefaults.shared else { return false }
+
+        let prevDbName = userDefaults.string(forKey: UserDefaults.currAccountDbName) ?? defaultDbName
+        guard let inCreationDbName = getUniqueDbName() else { return false }
+
+        userDefaults.set(prevDbName, forKey: UserDefaults.prevAccountDbName)
+        userDefaults.set(inCreationDbName, forKey: UserDefaults.currAccountDbName)
+
         resetDcContext()
+        return true
     }
 }

+ 1 - 3
DcCore/DcCore/Helper/DatabaseHelper.swift

@@ -101,7 +101,7 @@ public class DatabaseHelper {
         }
     }
 
-    public func updateDatabaseLocation() -> String? {
+    public func updateDatabaseLocation() {
       let filemanager = FileManager.default
       if filemanager.fileExists(atPath: localDbFile) {
           do {
@@ -110,10 +110,8 @@ public class DatabaseHelper {
               moveBlobsFolder()
           } catch let error {
               DcContext.shared.logger?.error("Could not update DB location. Share extension will probably not work. \n\(error.localizedDescription)")
-              return localDbFile
           }
       }
-      return sharedDbFile
     }
 
     public func updateSucceeded() -> Bool {

+ 3 - 3
deltachat-ios/AppDelegate.swift

@@ -151,9 +151,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
     }
 
     func openDatabase() {
-        guard let databaseLocation = DatabaseHelper().updateDatabaseLocation() else {
-            fatalError("Database could not be opened")
-        }
+        DatabaseHelper().updateDatabaseLocation()
+
+        let databaseLocation = AccountManager().getSelectedAccount()
         logger.info("open: \(databaseLocation)")
         dcContext.openDatabase(dbFile: databaseLocation)
     }

+ 16 - 10
deltachat-ios/Controller/SettingsController.swift

@@ -432,15 +432,17 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
         present(alert, animated: true, completion: nil)
     }
 
+    private func presentError(message: String) {
+        let error = UIAlertController(title: nil, message: message, preferredStyle: .alert)
+        error.addAction(UIAlertAction(title: String.localized("ok"), style: .cancel))
+        present(error, animated: true)
+    }
+
     private func showSwitchAccountMenu() {
         let accounts = AccountManager().getAccounts()
         if accounts.isEmpty {
-            let error = UIAlertController(title: String.localized("switch_account"),
-                message: "Cannot switch or add accounts as the 'shared folder' is not set up. " +
-                "To fix this, make sure, there is enough free space available and restart Delta Chat.",
-                preferredStyle: .alert)
-            error.addAction(UIAlertAction(title: String.localized("ok"), style: .cancel))
-            present(error, animated: true)
+            presentError(message: "Cannot switch or add accounts as the 'shared folder' is not set up. " +
+            "To fix this, make sure, there is enough free space available and restart Delta Chat.")
             return
         }
 
@@ -449,17 +451,21 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
         for account in accounts {
             var title = account.displayname.isEmpty ? account.addr : "\(account.displayname) (\(account.addr))"
             title += account.current ? " ✓" : ""
-            alert.addAction(UIAlertAction(title: title, style: .default, handler: nil))
+            alert.addAction(UIAlertAction(title: title, style: .default, handler: nil)) // TODO
         }
 
         // delete account
         if accounts.count > 1 {
-            alert.addAction(UIAlertAction(title: String.localized("delete_account"), style: .default, handler: nil))
+            alert.addAction(UIAlertAction(title: String.localized("delete_account"), style: .default, handler: nil)) // TODO
         }
 
         // add account
-        alert.addAction(UIAlertAction(title: String.localized("add_account"), style: .default, handler: { _ in
-            AccountManager().beginAccountCreation()
+        alert.addAction(UIAlertAction(title: String.localized("add_account"), style: .default, handler: { [weak self] _ in
+            if !AccountManager().beginAccountCreation() {
+                self?.presentError(message: "Cannot begin account creation.")
+            } else {
+                // TODO
+            }
         }))
 
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))