Просмотр исходного кода

Hide glucose badge when glucose is 20 minutes (or more) old (optional)

(cherry picked from commit 9f1d44ec4357ea5bba265b234fd010b72113503c)
Jon Mårtensson 3 лет назад
Родитель
Сommit
141932ec84

+ 5 - 0
FreeAPS/Sources/Models/FreeAPSSettings.swift

@@ -17,6 +17,7 @@ struct FreeAPSSettings: JSON, Equatable {
     var useCalendar: Bool = false
     var useAppleHealth: Bool = false
     var glucoseBadge: Bool = false
+    var tooOldGlucose: Bool = false
     var glucoseNotificationsAlways: Bool = false
     var useAlarmSound: Bool = false
     var addSourceInfoToGlucoseNotifications: Bool = false
@@ -103,6 +104,10 @@ extension FreeAPSSettings: Decodable {
             settings.glucoseBadge = glucoseBadge
         }
 
+        if let tooOldGlucose = try? container.decode(Bool.self, forKey: .tooOldGlucose) {
+            settings.tooOldGlucose = tooOldGlucose
+        }
+
         if let useFPUconversion = try? container.decode(Bool.self, forKey: .useFPUconversion) {
             settings.useFPUconversion = useFPUconversion
         }

+ 2 - 0
FreeAPS/Sources/Modules/NotificationsConfig/NotificationsConfigStateModel.swift

@@ -3,6 +3,7 @@ import SwiftUI
 extension NotificationsConfig {
     final class StateModel: BaseStateModel<Provider> {
         @Published var glucoseBadge = false
+        @Published var tooOldGlucose = false
         @Published var glucoseNotificationsAlways = false
         @Published var useAlarmSound = false
         @Published var addSourceInfoToGlucoseNotifications = false
@@ -16,6 +17,7 @@ extension NotificationsConfig {
             self.units = units
 
             subscribeSetting(\.glucoseBadge, on: $glucoseBadge) { glucoseBadge = $0 }
+            subscribeSetting(\.tooOldGlucose, on: $tooOldGlucose) { tooOldGlucose = $0 }
             subscribeSetting(\.glucoseNotificationsAlways, on: $glucoseNotificationsAlways) { glucoseNotificationsAlways = $0 }
             subscribeSetting(\.useAlarmSound, on: $useAlarmSound) { useAlarmSound = $0 }
             subscribeSetting(\.addSourceInfoToGlucoseNotifications, on: $addSourceInfoToGlucoseNotifications) {

+ 1 - 0
FreeAPS/Sources/Modules/NotificationsConfig/View/NotificationsConfigRootView.swift

@@ -28,6 +28,7 @@ extension NotificationsConfig {
             Form {
                 Section(header: Text("Glucose")) {
                     Toggle("Show glucose on the app badge", isOn: $state.glucoseBadge)
+                    Toggle("Hide glucose badge when older than 20 minutes", isOn: $state.tooOldGlucose)
                     Toggle("Always Notify Glucose", isOn: $state.glucoseNotificationsAlways)
                     Toggle("Also play alert sound", isOn: $state.useAlarmSound)
                     Toggle("Also add source info", isOn: $state.addSourceInfoToGlucoseNotifications)

+ 7 - 0
FreeAPS/Sources/Services/UserNotifiactions/UserNotificationsManager.swift

@@ -191,6 +191,13 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
 
         addAppBadge(glucose: lastGlucose.glucose)
 
+        if settingsManager.settings.tooOldGlucose {
+            DispatchQueue.main.asyncAfter(deadline: .now() + 1200) {
+                print("CGM too old for glucose badge")
+                self.addAppBadge(glucose: nil)
+            }
+        }
+
         guard glucoseStorage.alarm != nil || settingsManager.settings.glucoseNotificationsAlways else {
             return
         }