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

Merge pull request #449 from loopandlearn/missed-reading-alert

Fix(alarm): Correct logic for missed reading alert
Marion Barker 11 месяцев назад
Родитель
Сommit
1d0d73a468

+ 2 - 2
LoopFollow/Alarm/AlarmCondition/MissedReadingCondition.swift

@@ -9,7 +9,7 @@ struct MissedReadingCondition: AlarmCondition {
     static let type: AlarmType = .missedReading
     init() {}
 
-    func evaluate(alarm: Alarm, data: AlarmData, now _: Date) -> Bool {
+    func evaluate(alarm: Alarm, data: AlarmData, now : Date) -> Bool {
         // ────────────────────────────────
         // 0. sanity checks
         // ────────────────────────────────
@@ -18,7 +18,7 @@ struct MissedReadingCondition: AlarmCondition {
         // Skip if we have *no* readings
         guard let last = data.bgReadings.last else { return false }
 
-        let secondsSinceLast = Date().timeIntervalSince(last.date)
+        let secondsSinceLast = now.timeIntervalSince(last.date)
         return secondsSinceLast >= thresholdMinutes * 60
     }
 }

+ 4 - 2
LoopFollow/Alarm/AlarmManager.swift

@@ -70,8 +70,10 @@ class AlarmManager {
                 continue
             }
 
-            // If the alarm is based on bg values, and the value isnt recent, skip to next
-            if alarm.type.isBGBased, !isLatestReadingRecent {
+            // If an alarm is BG-based, it usually requires recent data.
+            // We make a specific exception for .missedReading, whose entire
+            // purpose is to fire when recent BG data is NOT recent.
+            if alarm.type.isBGBased, alarm.type != .missedReading, !isLatestReadingRecent {
                 continue
             }