Преглед изворни кода

Merge pull request #670 from deltachat/fix_offline_error

clearer error message on login error if no internet connection available
bjoern пре 5 година
родитељ
комит
c140856e3a

+ 23 - 34
deltachat-ios/AppDelegate.swift

@@ -49,6 +49,29 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
         startThreads()
         setStockTranslations()
+
+        reachability.whenReachable = { reachability in
+            logger.info("network: reachable", reachability.connection.description)
+
+            // call dc_maybe_network() from a worker thread.
+            // normally, dc_maybe_network() can be called uncoditionally,
+            // however, in fact, it may halt things for some seconds.
+            // this pr is a workaround that make things usable for now.
+            DispatchQueue.global(qos: .background).async {
+               self.dcContext.maybeNetwork()
+           }
+        }
+
+        reachability.whenUnreachable = { _ in
+            logger.info("network: not reachable")
+        }
+
+        do {
+            try reachability.startNotifier()
+        } catch {
+            print("Unable to start notifier")
+        }
+
         return true
     }
 
@@ -84,9 +107,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
     func applicationDidEnterBackground(_: UIApplication) {
         logger.info("---- background ----")
 
-        reachability.stopNotifier()
-        NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
-
         maybeStop()
     }
 
@@ -111,7 +131,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         closeDatabase()
 
         reachability.stopNotifier()
-        NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
     }
 
     func openDatabase() {
@@ -207,36 +226,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
                 self.dcContext.performMoveBox()
             }
         }
-
-        NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)),
-                                               name: .reachabilityChanged, object: reachability)
-        do {
-            try reachability.startNotifier()
-        } catch {
-            logger.info("could not start reachability notifier")
-        }
-    }
-
-    @objc private func reachabilityChanged(note: Notification) {
-        guard let reachability = note.object as? Reachability else {
-            logger.info("reachability object missing")
-            return
-        }
-
-        switch reachability.connection {
-        case .wifi, .cellular:
-            logger.info("network: reachable", reachability.connection.description)
-
-            // call dc_maybe_network() from a worker thread.
-            // normally, dc_maybe_network() can be called uncoditionally,
-            // however, in fact, it may halt things for some seconds.
-            // this pr is a workaround that make things usable for now.
-            DispatchQueue.global(qos: .background).async {
-                self.dcContext.maybeNetwork()
-            }
-        case .none:
-            logger.info("network: not reachable")
-        }
     }
 
     // MARK: - BackgroundTask

+ 5 - 1
deltachat-ios/Controller/AccountSetupController.swift

@@ -658,7 +658,11 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
             notification in
             if let ui = notification.userInfo {
                 if ui["error"] as! Bool {
-                    self.updateProgressAlert(error: ui["errorMessage"] as? String)
+                    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")
+                    }
+                    self.updateProgressAlert(error: errorMessage)
                 } else if ui["done"] as! Bool {
                     self.updateProgressAlertSuccess(completion: self.handleLoginSuccess)
                 } else {

+ 1 - 0
deltachat-ios/en.lproj/Localizable.strings

@@ -650,3 +650,4 @@
 "import_contacts_message" = "To chat with contacts from your device open Settings and enable Contacts.";
 "stop_sharing_location" = "Stop sharing location";
 "a11y_voice_message_hint_ios" = "After recording double-tap to send. To discard the recording scrub left-right with two fingers.";
+"login_error_no_internet_connection" = "No internet connection, can\'t log in to your server.";

+ 1 - 0
tools/untranslated.xml

@@ -6,4 +6,5 @@
     <string name="import_contacts_message">To chat with contacts from your device open Settings and enable Contacts.</string>
     <string name="stop_sharing_location">Stop sharing location</string>
     <string name="a11y_voice_message_hint_ios">After recording double-tap to send. To discard the recording scrub left-right with two fingers.</string>
+    <string name="login_error_no_internet_connection">No internet connection, can\'t log in to your server.</string>
 </resources>