|
@@ -41,10 +41,88 @@ class ConnectivityViewController: WebViewViewController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // this method needs to be run from a background thread
|
|
|
+ private func getNotificationStatus() -> String {
|
|
|
+ let title = " <b>" + String.localized("pref_notifications") + ":</b> "
|
|
|
+ let notificationsEnabledInDC = !UserDefaults.standard.bool(forKey: "notifications_disabled")
|
|
|
+ var notificationsEnabledInSystem = false
|
|
|
+ let semaphore = DispatchSemaphore(value: 0)
|
|
|
+ DispatchQueue.global(qos: .userInitiated).async {
|
|
|
+ NotificationManager.notificationEnabledInSystem { enabled in
|
|
|
+ notificationsEnabledInSystem = enabled
|
|
|
+ semaphore.signal()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if semaphore.wait(timeout: .now() + 1) == .timedOut {
|
|
|
+ return "<span class=\"red dot\"></span>"
|
|
|
+ .appending(title)
|
|
|
+ .appending("Timeout Error")
|
|
|
+ }
|
|
|
+
|
|
|
+ if !notificationsEnabledInDC {
|
|
|
+ return "<span class=\"disabled dot\"></span>"
|
|
|
+ .appending(title)
|
|
|
+ .appending(String.localized("notifications_disabled_dc"))
|
|
|
+ }
|
|
|
+
|
|
|
+ if !notificationsEnabledInSystem {
|
|
|
+ return "<span class=\"disabled dot\"></span>"
|
|
|
+ .appending(title)
|
|
|
+ .appending(String.localized("notifications_disabled"))
|
|
|
+ }
|
|
|
+
|
|
|
+ let timestamps = UserDefaults.standard.array(forKey: Constants.Keys.notificationTimestamps) as? [Double]
|
|
|
+ guard let timestamps = timestamps else {
|
|
|
+ return "<span class=\"yellow dot\"></span>"
|
|
|
+ .appending(title)
|
|
|
+ .appending(String.localized("no_data"))
|
|
|
+ }
|
|
|
+
|
|
|
+ if timestamps.isEmpty || timestamps.count == 1 {
|
|
|
+ // FIXME: for timestamp == 1, that is just okay if the timestamp is not too old
|
|
|
+ return "<span class=\"red dot\"></span>"
|
|
|
+ .appending(title)
|
|
|
+ .appending(String.localized("notifications_not_working"))
|
|
|
+ }
|
|
|
+
|
|
|
+ var timestampDeltas: Double = 0
|
|
|
+ for (index, element) in timestamps.enumerated() where index > 0 {
|
|
|
+ let diff = element - timestamps[index - 1]
|
|
|
+ timestampDeltas += diff
|
|
|
+ }
|
|
|
+
|
|
|
+ let averageDelta = timestampDeltas / Double(timestamps.count - 1)
|
|
|
+ let lastWakeup = DateUtils.getExtendedRelativeTimeSpanString(timeStamp: timestamps.last!)
|
|
|
+
|
|
|
+ if Int(averageDelta / Double(60 * 60)) > 1 {
|
|
|
+ // more than 1 hour in average
|
|
|
+ return "<span class=\"red dot\"></span>"
|
|
|
+ .appending(title)
|
|
|
+ .appending(String.localized(stringID: "notifications_stats_hours_delayed", count: Int(averageDelta / Double(60 * 60))))
|
|
|
+ .appending(" ")
|
|
|
+ .appending(String.localizedStringWithFormat(String.localized("notifications_stats_last_wakeup"), lastWakeup))
|
|
|
+ }
|
|
|
+
|
|
|
+ if averageDelta / Double(60 * 20) > 1 {
|
|
|
+ // more than 20 minutes in average
|
|
|
+ return "<span class=\"yellow dot\"></span>"
|
|
|
+ .appending(title)
|
|
|
+ .appending(String.localized(stringID: "notifications_stats_minutes_delayed", count: Int(averageDelta / 60)))
|
|
|
+ .appending(" ")
|
|
|
+ .appending(String.localizedStringWithFormat(String.localized("notifications_stats_last_wakeup"), lastWakeup))
|
|
|
+ }
|
|
|
+
|
|
|
+ return "<span class=\"green dot\"></span>"
|
|
|
+ .appending(title)
|
|
|
+ .appending(String.localized(stringID: "notifications_stats_minutes", count: Int(averageDelta / 60)))
|
|
|
+ .appending(" ")
|
|
|
+ .appending(String.localizedStringWithFormat(String.localized("notifications_stats_last_wakeup"), lastWakeup))
|
|
|
+ }
|
|
|
+
|
|
|
private func loadHtml() {
|
|
|
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
|
|
|
guard let self = self else { return }
|
|
|
- let html = self.dcContext.getConnectivityHtml()
|
|
|
+ var html = self.dcContext.getConnectivityHtml()
|
|
|
.replacingOccurrences(of: "</style>", with:
|
|
|
"""
|
|
|
body {
|
|
@@ -54,6 +132,10 @@ class ConnectivityViewController: WebViewViewController {
|
|
|
-webkit-text-size-adjust: none;
|
|
|
}
|
|
|
|
|
|
+ .disabled {
|
|
|
+ background-color: #aaaaaa;
|
|
|
+ }
|
|
|
+
|
|
|
@media (prefers-color-scheme: dark) {
|
|
|
body {
|
|
|
background-color: black !important;
|
|
@@ -62,6 +144,12 @@ class ConnectivityViewController: WebViewViewController {
|
|
|
}
|
|
|
</style>
|
|
|
""")
|
|
|
+
|
|
|
+ let notificationStatus = self.getNotificationStatus()
|
|
|
+ if let range = html.range(of: "</ul>") {
|
|
|
+ html = html.replacingCharacters(in: range, with: "<li>" + notificationStatus + "</li></ul>")
|
|
|
+ }
|
|
|
+
|
|
|
DispatchQueue.main.async {
|
|
|
self.webView.loadHTMLString(html, baseURL: nil)
|
|
|
}
|