浏览代码

Issue stale-glucose calibration warning via TrioAlertManager

trioneer 2 周之前
父节点
当前提交
365aa321f4
共有 1 个文件被更改,包括 27 次插入1 次删除
  1. 27 1
      Trio/Sources/Modules/Calibrations/CalibrationsStateModel.swift

+ 27 - 1
Trio/Sources/Modules/Calibrations/CalibrationsStateModel.swift

@@ -1,4 +1,5 @@
 import CoreData
 import CoreData
+import LoopKit
 import Observation
 import Observation
 import SwiftDate
 import SwiftDate
 import SwiftUI
 import SwiftUI
@@ -7,6 +8,7 @@ extension Calibrations {
     @Observable final class StateModel: BaseStateModel<Provider> {
     @Observable final class StateModel: BaseStateModel<Provider> {
         @ObservationIgnored @Injected() var glucoseStorage: GlucoseStorage!
         @ObservationIgnored @Injected() var glucoseStorage: GlucoseStorage!
         @ObservationIgnored @Injected() var calibrationService: CalibrationService!
         @ObservationIgnored @Injected() var calibrationService: CalibrationService!
+        @ObservationIgnored @Injected() var trioAlertManager: TrioAlertManager!
 
 
         var slope: Double = 1
         var slope: Double = 1
         var intercept: Double = 1
         var intercept: Double = 1
@@ -77,7 +79,8 @@ extension Calibrations {
 
 
                     calibrationService.addCalibration(calibration)
                     calibrationService.addCalibration(calibration)
                 } else {
                 } else {
-                    info(.service, "Glucose is stale for calibration")
+                    debug(.service, "Glucose is stale for calibration")
+                    issueStaleGlucoseAlert()
                     return
                     return
                 }
                 }
             } catch {
             } catch {
@@ -85,6 +88,29 @@ extension Calibrations {
             }
             }
         }
         }
 
 
+        /// Surfaces the "glucose too stale to calibrate against" condition as
+        /// a one-shot info alert through `TrioAlertManager`. Mirrors the old
+        /// `info(.service, …)` banner path that ran via `router.alertMessage`.
+        private func issueStaleGlucoseAlert() {
+            let content = Alert.Content(
+                title: String(localized: "Calibration unavailable"),
+                body: String(localized: "Glucose is stale for calibration"),
+                acknowledgeActionButtonLabel: String(localized: "OK")
+            )
+            let alert = Alert(
+                identifier: Alert.Identifier(
+                    managerIdentifier: "trio.calibration",
+                    alertIdentifier: "glucose.stale"
+                ),
+                foregroundContent: content,
+                backgroundContent: content,
+                trigger: .immediate,
+                interruptionLevel: .active,
+                sound: nil
+            )
+            trioAlertManager?.issueAlert(alert)
+        }
+
         func removeLast() {
         func removeLast() {
             calibrationService.removeLast()
             calibrationService.removeLast()
             setupCalibrations()
             setupCalibrations()