Bläddra i källkod

add add/delete/switch account functions

B. Petersen 4 år sedan
förälder
incheckning
7e91ed0914
2 ändrade filer med 43 tillägg och 20 borttagningar
  1. 9 0
      DcCore/DcCore/DC/Wrapper.swift
  2. 34 20
      deltachat-ios/Controller/SettingsController.swift

+ 9 - 0
DcCore/DcCore/DC/Wrapper.swift

@@ -538,6 +538,15 @@ public class DcContext {
         set { setConfig("displayname", newValue) }
     }
 
+    public var displaynameAndAddr: String {
+        var ret = addr ?? ""
+        if let displayname = displayname {
+            ret = "\(displayname) (\(ret))"
+        }
+        ret += configured ? "" : " (not configured)"
+        return ret.trimmingCharacters(in: .whitespaces)
+    }
+
     public var selfstatus: String? {
         get { return getConfig("selfstatus") }
         set { setConfig("selfstatus", newValue) }

+ 34 - 20
deltachat-ios/Controller/SettingsController.swift

@@ -438,38 +438,52 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
 
     private func showSwitchAccountMenu() {
         let accountIds = dcAccounts.getAll()
-        let selectedAccount = dcAccounts.getSelected()
+        let selectedAccountId = dcAccounts.getSelected().id
+        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
 
         // switch account
-        let alert = UIAlertController(title: String.localized("switch_account"), message: nil, preferredStyle: .safeActionSheet)
+        let menu = UIAlertController(title: String.localized("switch_account"), message: nil, preferredStyle: .safeActionSheet)
         for accountId in accountIds {
             let account = dcAccounts.get(id: accountId)
-            var title = account.addr ?? ""
-            if let displayname = account.displayname {
-                title = "\(displayname) (\(title))"
-            }
-            title += account.configured ? "" : " (not configured)"
-            title = (selectedAccount.id==accountId ? "✔︎ " : "") + title
-            alert.addAction(UIAlertAction(title: title, style: .default, handler: { [weak self] _ in
-                guard let self = self else { return }
-                if self.dcAccounts.select(id: accountId) {
-                    // TODO: go to chatlist with new account
-                }
+            var title = account.displaynameAndAddr
+            title = (selectedAccountId==accountId ? "✔︎ " : "") + title
+            menu.addAction(UIAlertAction(title: title, style: .default, handler: { [weak self] _ in
+                _ = self?.dcAccounts.select(id: accountId)
+                appDelegate.reloadDcContext()
             }))
         }
 
         // add account
-        alert.addAction(UIAlertAction(title: String.localized("add_account"), style: .default, handler: { [weak self] _ in
-            // TODO
+        menu.addAction(UIAlertAction(title: String.localized("add_account"), style: .default, handler: { [weak self] _ in
+            _ = self?.dcAccounts.add()
+            appDelegate.reloadDcContext()
         }))
 
         // delete account
-        if accountIds.count > 1 {
-            alert.addAction(UIAlertAction(title: String.localized("delete_account"), style: .default, handler: nil)) // TODO
-        }
+        menu.addAction(UIAlertAction(title: String.localized("delete_account"), style: .default, handler: { [weak self] _ in
+            let confirm1 = UIAlertController(title: String.localized("delete_account_ask"), message: nil, preferredStyle: .safeActionSheet)
+            confirm1.addAction(UIAlertAction(title: String.localized("delete_account"), style: .destructive, handler: { [weak self] _ in
+                guard let self = self else { return }
+                let account = self.dcAccounts.get(id: selectedAccountId)
+                let confirm2 = UIAlertController(title: account.displaynameAndAddr,
+                    message: String.localized("forget_login_confirmation_desktop"), preferredStyle: .alert)
+                confirm2.addAction(UIAlertAction(title: String.localized("delete"), style: .destructive, handler: { [weak self] _ in
+                    guard let self = self else { return }
+                    _ = self.dcAccounts.remove(id: selectedAccountId)
+                    if self.dcAccounts.getAll().isEmpty {
+                        _ = self.dcAccounts.add()
+                    }
+                    appDelegate.reloadDcContext()
+                }))
+                confirm2.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel))
+                self.present(confirm2, animated: true, completion: nil)
+            }))
+            confirm1.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel))
+            self?.present(confirm1, animated: true, completion: nil)
+        }))
 
-        alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
-        present(alert, animated: true, completion: nil)
+        menu.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
+        present(menu, animated: true, completion: nil)
     }
 
     private func startImex(what: Int32) {