From ecd912fe859ccbb041d8396045627606ca040722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Mon, 11 Aug 2025 10:42:40 +0200 Subject: [PATCH] Fix(alarm): Correct logic for missed reading alert --- .../Alarm/AlarmCondition/MissedReadingCondition.swift | 4 ++-- LoopFollow/Alarm/AlarmManager.swift | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/LoopFollow/Alarm/AlarmCondition/MissedReadingCondition.swift b/LoopFollow/Alarm/AlarmCondition/MissedReadingCondition.swift index d75ecca33..7f978eb53 100644 --- a/LoopFollow/Alarm/AlarmCondition/MissedReadingCondition.swift +++ b/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 } } diff --git a/LoopFollow/Alarm/AlarmManager.swift b/LoopFollow/Alarm/AlarmManager.swift index 2edc10331..f786529fa 100644 --- a/LoopFollow/Alarm/AlarmManager.swift +++ b/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 }