B. Petersen 5 年之前
父節點
當前提交
b3e2e23cd1

+ 27 - 34
deltachat-ios/AppDelegate.swift

@@ -21,37 +21,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
     var relayHelper: RelayHelper!
     var locationManager: LocationManager!
     private var backgroundTask: UIBackgroundTaskIdentifier = .invalid
-
     var reachability = Reachability()!
     var window: UIWindow?
-
     var state = ApplicationState.stopped
 
-    func application(_: UIApplication, open url: URL, options _: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
-        // gets here when app returns from oAuth2-Setup process - the url contains the provided token
-        if let params = url.queryParameters, let token = params["code"] {
-            NotificationCenter.default.post(name: NSNotification.Name("oauthLoginApproved"), object: nil, userInfo: ["token": token])
-        }
-
-        // Hack to format url properly
-        let urlString = url.absoluteString
-                       .replacingOccurrences(of: "openpgp4fpr", with: "OPENPGP4FPR", options: .literal, range: nil)
-                       .replacingOccurrences(of: "%23", with: "#", options: .literal, range: nil)
-
-        self.appCoordinator.handleQRCode(urlString)
-        return true
-    }
-
     func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
-
+        // main()
         let console = ConsoleDestination()
         logger.addDestination(console)
         dcContext.logger = DcLogger()
-
         logger.info("launching")
 
-        // Override point for customization after application launch.
-
         window = UIWindow(frame: UIScreen.main.bounds)
         guard let window = window else {
             fatalError("window was nil in app delegate")
@@ -61,23 +41,36 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         } else {
             window.backgroundColor = UIColor.white
         }
-        // setup deltachat core context
-        //       - second param remains nil (user data for more than one mailbox)
-        open()
+
+        openDatabase()
         RelayHelper.setup(dcContext)
         appCoordinator = AppCoordinator(window: window, dcContext: dcContext)
-        appCoordinator.start()
         locationManager = LocationManager(context: dcContext)
         UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
-        start()
+        startThreads()
         setStockTranslations()
         return true
     }
 
+    func application(_: UIApplication, open url: URL, options _: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
+        // gets here when app returns from oAuth2-Setup process - the url contains the provided token
+        if let params = url.queryParameters, let token = params["code"] {
+            NotificationCenter.default.post(name: NSNotification.Name("oauthLoginApproved"), object: nil, userInfo: ["token": token])
+        }
+
+        // Hack to format url properly
+        let urlString = url.absoluteString
+                       .replacingOccurrences(of: "openpgp4fpr", with: "OPENPGP4FPR", options: .literal, range: nil)
+                       .replacingOccurrences(of: "%23", with: "#", options: .literal, range: nil)
+
+        self.appCoordinator.handleQRCode(urlString)
+        return true
+    }
+
     func application(_: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
         logger.info("---- background-fetch ----")
 
-        start {
+        startThreads {
             // TODO: actually set the right value depending on if we found sth
             completionHandler(.newData)
         }
@@ -85,7 +78,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
     func applicationWillEnterForeground(_: UIApplication) {
         logger.info("---- foreground ----")
-        start()
+        startThreads()
     }
 
     func applicationDidEnterBackground(_: UIApplication) {
@@ -106,7 +99,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
                 // only need to do sth in the background
                 return
             } else if app.backgroundTimeRemaining < 10 {
-                self.stop()
+                self.stopThreads()
             } else {
                 self.maybeStop()
             }
@@ -115,13 +108,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
     func applicationWillTerminate(_: UIApplication) {
         logger.info("---- terminate ----")
-        close()
+        closeDatabase()
 
         reachability.stopNotifier()
         NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
     }
 
-    func open() {
+    func openDatabase() {
         guard let databaseLocation = DatabaseHelper().updateDatabaseLocation() else {
             fatalError("Database could not be opened")
         }
@@ -164,17 +157,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         dcContext.setStockTranslation(id: DC_STR_DEVICE_MESSAGES, localizationKey: "device_talk")
     }
 
-    func stop() {
+    func stopThreads() {
         state = .background
         dcContext.interruptIdle()
     }
 
-    func close() {
+    func closeDatabase() {
         state = .stopped
         dcContext.closeDatabase()
     }
 
-    func start(_ completion: (() -> Void)? = nil) {
+    func startThreads(_ completion: (() -> Void)? = nil) {
         logger.info("---- start ----")
 
         if state == .running {

+ 4 - 4
deltachat-ios/Controller/AccountSetupController.swift

@@ -778,11 +778,11 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
             preferredStyle: .safeActionSheet)
 
         alert.addAction(UIAlertAction(title: String.localized("delete_account"), style: .destructive, handler: { _ in
-            appDelegate.stop()
-            appDelegate.close()
+            appDelegate.stopThreads()
+            appDelegate.closeDatabase()
             DatabaseHelper().clearAccountData()
-            appDelegate.open()
-            appDelegate.start()
+            appDelegate.openDatabase()
+            appDelegate.startThreads()
             appDelegate.appCoordinator.presentWelcomeController()
         }))
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel))

+ 1 - 1
deltachat-ios/Controller/GroupChatDetailViewController.swift

@@ -224,7 +224,7 @@ class GroupChatDetailViewController: UIViewController {
         presentPreview(for: DC_MSG_IMAGE, messageType2: DC_MSG_GIF, messageType3: DC_MSG_VIDEO)
     }
 
-private func presentPreview(for messageType: Int32, messageType2: Int32, messageType3: Int32) {
+    private func presentPreview(for messageType: Int32, messageType2: Int32, messageType3: Int32) {
         let messageIds = dcContext.getChatMedia(chatId: chatId, messageType: messageType, messageType2: messageType2, messageType3: messageType3)
         var mediaUrls: [URL] = []
         for messageId in messageIds {

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

@@ -5,7 +5,7 @@ import MobileCoreServices
 import DcCore
 
 // MARK: - AppCoordinator
-class AppCoordinator: NSObject, Coordinator {
+class AppCoordinator {
 
     private let window: UIWindow
     private let dcContext: DcContext
@@ -59,23 +59,20 @@ class AppCoordinator: NSObject, Coordinator {
     init(window: UIWindow, dcContext: DcContext) {
         self.window = window
         self.dcContext = dcContext
-        super.init()
 
         if dcContext.isConfigured() {
             presentTabBarController()
         } else {
             presentWelcomeController()
         }
-    }
 
-    public func start() {
         let lastActiveTab = appStateRestorer.restoreLastActiveTab()
         if lastActiveTab == -1 {
             // no stored tab
             showTab(index: chatsTab)
         } else {
             showTab(index: lastActiveTab)
-            if let lastActiveChatId = appStateRestorer.restoreLastActiveChatId(), lastActiveTab == 1 {
+            if let lastActiveChatId = appStateRestorer.restoreLastActiveChatId(), lastActiveTab == chatsTab {
                 // as getChat() returns an empty object for invalid chatId,
                 // check that the returned object is actually set up.
                 if dcContext.getChat(chatId: lastActiveChatId).id == lastActiveChatId {

+ 0 - 3
deltachat-ios/Helper/Protocols.swift

@@ -1,8 +1,5 @@
 import UIKit
 
-protocol Coordinator: class {
-}
-
 protocol QrCodeReaderDelegate: class {
     func handleQrCode(_ code: String)
 }