Kaynağa Gözat

add hints for low power and low data mode

cyberta 2 yıl önce
ebeveyn
işleme
c930f9be33

+ 33 - 0
deltachat-ios/Controller/ConnectivityViewController.swift

@@ -1,9 +1,12 @@
 import UIKit
 import DcCore
+import Network
 
 class ConnectivityViewController: WebViewViewController {
     private let dcContext: DcContext
     private var connectivityChangedObserver: NSObjectProtocol?
+    private var connectivityMonitor: AnyObject?
+    private var isLowDataMode: Bool = false
 
     init(dcContext: DcContext) {
         self.dcContext = dcContext
@@ -22,6 +25,20 @@ class ConnectivityViewController: WebViewViewController {
         self.webView.isOpaque = false
         self.webView.backgroundColor = .clear
         view.backgroundColor = DcColors.defaultBackgroundColor
+
+        if #available(iOS 13.0, *) {
+            let monitor = NWPathMonitor()
+            monitor.pathUpdateHandler = { [weak self] path in
+                guard let self = self else { return }
+                self.isLowDataMode = path.isConstrained
+                DispatchQueue.main.async {
+                    self.loadHtml()
+                }
+            }
+            isLowDataMode = monitor.currentPath.isConstrained
+            monitor.start(queue: DispatchQueue.global(qos: .background))
+            self.connectivityMonitor = monitor
+        }
     }
 
     // called everytime the view will appear
@@ -40,6 +57,10 @@ class ConnectivityViewController: WebViewViewController {
         if let connectivityChangedObserver = self.connectivityChangedObserver {
             NotificationCenter.default.removeObserver(connectivityChangedObserver)
         }
+
+        if #available(iOS 13.0, *) {
+            (connectivityMonitor as? NWPathMonitor)?.cancel()
+        }
     }
 
     // this method needs to be run from a background thread
@@ -85,6 +106,18 @@ class ConnectivityViewController: WebViewViewController {
                 .appending(String.localized("connectivity_not_connected"))
         }
 
+        if isLowDataMode {
+            return "<span class=\"red dot\"></span>"
+                .appending(title)
+                .appending(String.localized("connectivity_low_data_mode"))
+        }
+
+        if ProcessInfo.processInfo.isLowPowerModeEnabled {
+            return "<span class=\"red dot\"></span>"
+                .appending(title)
+                .appending(String.localized("connectivity_low_battery_mode"))
+        }
+
         let timestamps = UserDefaults.standard.array(forKey: Constants.Keys.notificationTimestamps) as? [Double]
         guard let timestamps = timestamps else {
             // in most cases, here the app was just installed and we do not have any data.

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

@@ -896,3 +896,5 @@
 "webxdc_empty_hint" = "Received or sent apps will appear here. Tap \"Files\" to select downloaded apps.";
 "shortcut_share_btn" = "Click the share button";
 "shortcut_add_to_home_description" = "Select - Add to Home Screen - to add the app to your home screen.";
+"connectivity_low_data_mode" = "Low Data Mode: background message fetching is disabled.";
+"connectivity_low_battery_mode" = "Low Power Mode: background message fetching is disabled.";

+ 2 - 0
scripts/untranslated.xml

@@ -6,4 +6,6 @@
     <string name ="webxdc_empty_hint">Received or sent apps will appear here. Tap \"Files\" to select downloaded apps.</string>
     <string name ="shortcut_share_btn">Click the share button</string>
     <string name ="shortcut_add_to_home_description">Select - Add to Home Screen - to add the app to your home screen.</string>
+    <string name="connectivity_low_data_mode">Low Data Mode: background message fetching is disabled.</string>
+    <string name="connectivity_low_battery_mode">Low Power Mode: background message fetching is disabled.</string>
 </resources>