浏览代码

Never call DcContext.startIo() or DcContext.stopIo(). Remove deprecated methods from wrapper and inject DcAccounts where needed

cyberta 4 年之前
父节点
当前提交
70e85e272a

+ 1 - 41
DcCore/DcCore/DC/Wrapper.swift

@@ -41,6 +41,7 @@ public class DcAccounts {
         return DcContext(contextPointer: cPtr)
     }
 
+    // call maybeNetwork() from a worker thread.
     public func maybeNetwork() {
         dc_accounts_maybe_network(accountsPointer)
     }
@@ -315,14 +316,6 @@ public class DcContext {
         return "ErrGetContactEncrInfo"
     }
 
-    public func interruptIdle() {
-    }
-
-    public func getEventEmitter() -> DcEventEmitter {
-        let eventEmitterPointer = dc_get_event_emitter(contextPointer)
-        return DcEventEmitter(eventEmitterPointer: eventEmitterPointer)
-    }
-
     public func openDatabase(dbFile: String) {
         var version = ""
         if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
@@ -337,16 +330,6 @@ public class DcContext {
         contextPointer = nil
     }
 
-    public func maybeStartIo() {
-        if isConfigured() {
-            dc_start_io(contextPointer)
-        }
-    }
-
-    public func stopIo() {
-        dc_stop_io(contextPointer)
-    }
-
     public func setStockTranslation(id: Int32, localizationKey: String) {
         dc_set_stock_translation(contextPointer, UInt32(id), String.localized(localizationKey))
     }
@@ -558,11 +541,6 @@ public class DcContext {
         return messageIds
     }
 
-    // call dc_maybe_network() from a worker thread.
-    public func maybeNetwork() {
-        dc_maybe_network(contextPointer)
-    }
-
     // also, there is no much worth in adding a separate function or so
     // for each config option - esp. if they are just forwarded to the core
     // and set/get only at one line of code each.
@@ -711,24 +689,6 @@ public class DcAccountsEventEmitter {
     }
 }
 
-public class DcEventEmitter {
-    private var eventEmitterPointer: OpaquePointer?
-
-    // takes ownership of specified pointer
-    public init(eventEmitterPointer: OpaquePointer?) {
-        self.eventEmitterPointer = eventEmitterPointer
-    }
-
-    public func getNextEvent() -> DcEvent? {
-        guard let eventPointer = dc_get_next_event(eventEmitterPointer) else { return nil }
-        return DcEvent(eventPointer: eventPointer)
-    }
-
-    deinit {
-        dc_event_emitter_unref(eventEmitterPointer)
-    }
-}
-
 public class DcEvent {
     private var eventPointer: OpaquePointer?
 

+ 1 - 1
deltachat-ios/AppDelegate.swift

@@ -383,7 +383,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
             // usually, this handler is not used as we are taking care of timings below.
             logger.info("⬅️ finishing fetch by system urgency requests")
-            self?.dcAccounts.get().stopIo()
+            self?.dcAccounts.stopIo()
             completionHandler(.newData)
             if backgroundTask != .invalid {
                 UIApplication.shared.endBackgroundTask(backgroundTask)

+ 21 - 19
deltachat-ios/Controller/AccountSetupController.swift

@@ -3,7 +3,8 @@ import UIKit
 import DcCore
 
 class AccountSetupController: UITableViewController, ProgressAlertHandler {
-    private let dcContext: DcContext
+    private var dcContext: DcContext
+    private let dcAccounts: DcAccounts
     private var skipOauth = false
     private var backupProgressObserver: NSObjectProtocol?
     var progressObserver: NSObjectProtocol?
@@ -278,9 +279,9 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
             textLabel: String.localized("pref_watch_inbox_folder"),
             on: dcContext.getConfigBool("inbox_watch"),
             action: { cell in
-                self.dcContext.stopIo()
+                self.dcAccounts.stopIo()
                 self.dcContext.setConfigBool("inbox_watch", cell.isOn)
-                self.dcContext.maybeStartIo()
+                self.dcAccounts.maybeStartIo()
         })
     }()
 
@@ -289,9 +290,9 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
             textLabel: String.localized("pref_watch_sent_folder"),
             on: dcContext.getConfigBool("sentbox_watch"),
             action: { cell in
-                self.dcContext.stopIo()
+                self.dcAccounts.stopIo()
                 self.dcContext.setConfigBool("sentbox_watch", cell.isOn)
-                self.dcContext.maybeStartIo()
+                self.dcAccounts.maybeStartIo()
         })
     }()
 
@@ -300,9 +301,9 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
             textLabel: String.localized("pref_watch_mvbox_folder"),
             on: dcContext.getConfigBool("mvbox_watch"),
             action: { cell in
-                self.dcContext.stopIo()
+                self.dcAccounts.stopIo()
                 self.dcContext.setConfigBool("mvbox_watch", cell.isOn)
-                self.dcContext.maybeStartIo()
+                self.dcAccounts.maybeStartIo()
         })
     }()
 
@@ -335,9 +336,10 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
     }()
 
     // MARK: - constructor
-    init(dcContext: DcContext, editView: Bool) {
+    init(dcAccounts: DcAccounts, editView: Bool) {
         self.editView = editView
-        self.dcContext = dcContext
+        self.dcAccounts = dcAccounts
+        self.dcContext = dcAccounts.get()
 
         self.sections.append(basicSection)
         self.sections.append(advancedSection)
@@ -587,7 +589,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
         }
 
         print("oAuth-Flag when loggin in: \(dcContext.getAuthFlags())")
-        dcContext.stopIo()
+        dcAccounts.stopIo()
         dcContext.configure()
         showProgressAlert(title: String.localized("login_header"), dcContext: dcContext)
     }
@@ -668,7 +670,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
             notification in
             if let ui = notification.userInfo {
                 if ui["error"] as! Bool {
-                    self.dcContext.maybeStartIo()
+                    self.dcAccounts.maybeStartIo()
                     var errorMessage = ui["errorMessage"] as? String
                     if let appDelegate = UIApplication.shared.delegate as? AppDelegate, appDelegate.reachability.connection == .none {
                         errorMessage = String.localized("login_error_no_internet_connection")
@@ -677,7 +679,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
                     }
                     self.updateProgressAlert(error: errorMessage)
                 } else if ui["done"] as! Bool {
-                    self.dcContext.maybeStartIo()
+                    self.dcAccounts.maybeStartIo()
                     self.updateProgressAlertSuccess(completion: self.handleLoginSuccess)
                 } else {
                     self.updateProgressAlertValue(value: ui["progress"] as? Int)
@@ -771,14 +773,14 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
             message: nil,
             preferredStyle: .safeActionSheet)
 
-        alert.addAction(UIAlertAction(title: String.localized("delete_account"), style: .destructive, handler: { _ in
-            self.dcContext.stopIo()
-            appDelegate.deleteCurrentAccount()
-            appDelegate.closeDatabase()
-            appDelegate.openDatabase()
-            // FIXME: reinitialize UIs with new DcContext
+        alert.addAction(UIAlertAction(title: String.localized("delete_account"), style: .destructive, handler: { [weak self] _ in
+            guard let self = self else { return }
+            self.dcAccounts.stopIo()
+            self.dcAccounts.removeAccount(id: self.dcAccounts.get().id)
+            self.dcAccounts.closeDatabase()
+            self.dcAccounts.openDatabase()
             appDelegate.installEventHandler()
-            self.dcContext.maybeStartIo()
+            self.dcAccounts.maybeStartIo()
             appDelegate.appCoordinator.presentWelcomeController()
         }))
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel))

+ 5 - 3
deltachat-ios/Controller/EditSettingsController.swift

@@ -3,6 +3,7 @@ import DcCore
 
 class EditSettingsController: UITableViewController, MediaPickerDelegate {
     private let dcContext: DcContext
+    private let dcAccounts: DcAccounts
 
     private let section1 = 0
     private let section1Name = 0
@@ -51,8 +52,9 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
         return cell
     }()
 
-    init(dcContext: DcContext) {
-        self.dcContext = dcContext
+    init(dcAccounts: DcAccounts) {
+        self.dcAccounts = dcAccounts
+        self.dcContext = dcAccounts.get()
         super.init(style: .grouped)
         hidesBottomBarWhenPushed = true
     }
@@ -118,7 +120,7 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
         if cell.tag == tagAccountSettingsCell {
             tableView.deselectRow(at: indexPath, animated: false)
             guard let nc = navigationController else { return }
-            let accountSetupVC = AccountSetupController(dcContext: dcContext, editView: true)
+            let accountSetupVC = AccountSetupController(dcAccounts: dcAccounts, editView: true)
             nc.pushViewController(accountSetupVC, animated: true)
         }
     }

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

@@ -27,6 +27,7 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
     }
 
     private var dcContext: DcContext
+    private var dcAccounts: DcAccounts
 
     private let externalPathDescr = "File Sharing/Delta Chat"
 
@@ -206,8 +207,9 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
         return [profileSection, preferencesSection, autocryptSection, backupSection, helpSection]
     }()
 
-    init(dcContext: DcContext) {
-        self.dcContext = dcContext
+    init(dcAccounts: DcAccounts) {
+        self.dcContext = dcAccounts.get()
+        self.dcAccounts = dcAccounts
         super.init(style: .grouped)
     }
 
@@ -232,7 +234,7 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
 
     override func viewDidAppear(_ animated: Bool) {
         super.viewDidAppear(animated)
-        addProgressAlertListener(dcContext: dcContext, progressName: dcNotificationImexProgress) { [weak self] in
+        addProgressAlertListener(dcAccounts: dcAccounts, progressName: dcNotificationImexProgress) { [weak self] in
             guard let self = self else { return }
             self.progressAlert?.dismiss(animated: true, completion: nil)
         }
@@ -423,7 +425,7 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
         if !documents.isEmpty {
             showProgressAlert(title: String.localized("imex_progress_title_desktop"), dcContext: dcContext)
             DispatchQueue.main.async {
-                self.dcContext.stopIo()
+                self.dcAccounts.stopIo()
                 self.dcContext.imex(what: what, directory: documents[0])
             }
         } else {
@@ -441,7 +443,7 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
 
     // MARK: - coordinator
     private func showEditSettingsController() {
-        let editController = EditSettingsController(dcContext: dcContext)
+        let editController = EditSettingsController(dcAccounts: dcAccounts)
         navigationController?.pushViewController(editController, animated: true)
     }
 

+ 15 - 8
deltachat-ios/Controller/WelcomeViewController.swift

@@ -2,7 +2,8 @@ import UIKit
 import DcCore
 
 class WelcomeViewController: UIViewController, ProgressAlertHandler {
-    private let dcContext: DcContext
+    private var dcContext: DcContext
+    private let dcAccounts: DcAccounts
     var progressObserver: NSObjectProtocol?
     var onProgressSuccess: VoidFunction?
 
@@ -16,7 +17,7 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
         let view = WelcomeContentView()
         view.onLogin = { [weak self] in
             guard let self = self else { return }
-            let accountSetupController = AccountSetupController(dcContext: self.dcContext, editView: false)
+            let accountSetupController = AccountSetupController(dcAccounts: self.dcAccounts, editView: false)
             accountSetupController.onLoginSuccess = {
                 [weak self] in
                 if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
@@ -40,18 +41,20 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
     private var qrCodeReader: QrCodeReaderController?
     weak var progressAlert: UIAlertController?
 
-    init(dcContext: DcContext) {
-        self.dcContext = dcContext
+    init(dcAccounts: DcAccounts) {
+        self.dcAccounts = dcAccounts
+        self.dcContext = dcAccounts.get()
         super.init(nibName: nil, bundle: nil)
         self.navigationItem.title = String.localized("welcome_desktop")
         onProgressSuccess = { [weak self] in
-            let profileInfoController = ProfileInfoViewController(context: dcContext)
+            guard let self = self else { return }
+            let profileInfoController = ProfileInfoViewController(context: self.dcContext)
             profileInfoController.onClose = {
                 if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
                     appDelegate.appCoordinator.presentTabBarController()
                 }
             }
-            self?.navigationController?.setViewControllers([profileInfoController], animated: true)
+            self.navigationController?.setViewControllers([profileInfoController], animated: true)
         }
     }
 
@@ -113,9 +116,13 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
     private func createAccountFromQRCode(qrCode: String) {
         let success = dcContext.setConfigFromQR(qrCode: qrCode)
         if success {
-            addProgressAlertListener(dcContext: dcContext, progressName: dcNotificationConfigureProgress, onSuccess: handleLoginSuccess)
+            addProgressAlertListener(dcAccounts: dcAccounts, progressName: dcNotificationConfigureProgress, onSuccess: handleLoginSuccess)
             showProgressAlert(title: String.localized("login_header"), dcContext: dcContext)
-            dcContext.stopIo()
+            dcAccounts.stopIo()
+            let accountId = dcAccounts.addAccount()
+            if accountId != 0 {
+                self.dcContext = dcAccounts.getAccount(id: accountId)
+            }
             dcContext.configure()
         } else {
             accountCreationErrorAlert()

+ 2 - 2
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -47,7 +47,7 @@ class AppCoordinator {
     }()
 
     private lazy var settingsNavController: UINavigationController = {
-        let root = SettingsViewController(dcContext: dcAccounts.get())
+        let root = SettingsViewController(dcAccounts: dcAccounts)
         let nav = UINavigationController(rootViewController: root)
         let settingsImage = UIImage(named: "settings")
         nav.tabBarItem = UITabBarItem(title: String.localized("menu_settings"), image: settingsImage, tag: settingsTab)
@@ -108,7 +108,7 @@ class AppCoordinator {
         if dcAccounts.get().isConfigured() {
             let _ = dcAccounts.addAccount()
         }
-        loginNavController.setViewControllers([WelcomeViewController(dcContext: dcAccounts.get())], animated: true)
+        loginNavController.setViewControllers([WelcomeViewController(dcAccounts: dcAccounts)], animated: true)
         window.rootViewController = loginNavController
         window.makeKeyAndVisible()
 

+ 4 - 4
deltachat-ios/Handler/ProgressAlertHandler.swift

@@ -8,7 +8,7 @@ protocol ProgressAlertHandler: UIViewController {
     func updateProgressAlertValue(value: Int?)
     func updateProgressAlert(error: String?)
     func updateProgressAlertSuccess(completion: VoidFunction?)
-    func addProgressAlertListener(dcContext: DcContext, progressName: Notification.Name, onSuccess: @escaping VoidFunction)
+    func addProgressAlertListener(dcAccounts: DcAccounts, progressName: Notification.Name, onSuccess: @escaping VoidFunction)
 }
 
 extension ProgressAlertHandler {
@@ -63,7 +63,7 @@ extension ProgressAlertHandler {
         })
     }
 
-    func addProgressAlertListener(dcContext: DcContext, progressName: Notification.Name, onSuccess: @escaping VoidFunction) {
+    func addProgressAlertListener(dcAccounts: DcAccounts, progressName: Notification.Name, onSuccess: @escaping VoidFunction) {
         let nc = NotificationCenter.default
         progressObserver = nc.addObserver(
             forName: progressName,
@@ -73,10 +73,10 @@ extension ProgressAlertHandler {
             guard let self = self else { return }
             if let ui = notification.userInfo {
                 if ui["error"] as? Bool ?? false {
-                    dcContext.maybeStartIo()
+                    dcAccounts.maybeStartIo()
                     self.updateProgressAlert(error: ui["errorMessage"] as? String)
                 } else if ui["done"] as? Bool ?? false {
-                    dcContext.maybeStartIo()
+                    dcAccounts.maybeStartIo()
                     self.updateProgressAlertSuccess(completion: onSuccess)
                 } else {
                     self.updateProgressAlertValue(value: ui["progress"] as? Int)