From 1912ca1ec258996cab5fbc1e5729b4d09711152a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Fri, 20 Nov 2026 09:34:22 +0100 Subject: [PATCH] Treat night as ending at day start when night starts after midnight Alarm activation used a day/night check that assumed night starts in the evening, so a night start at 00:00 made the whole day count as night and day-only alarms never fired. Both the activation and sound checks now use one shared isNight helper on AlarmConfiguration that handles a night start earlier than the day start. --- LoopFollow/Alarm/Alarm.swift | 18 +--- .../Alarm/AlarmCondition/AlarmCondition.swift | 6 +- LoopFollow/Alarm/AlarmConfiguration.swift | 13 +++ .../DayNightScheduleTests.swift | 97 +++++++++++++++++++ 4 files changed, 112 insertions(+), 22 deletions(-) create mode 100644 Tests/AlarmConditions/DayNightScheduleTests.swift diff --git a/LoopFollow/Alarm/Alarm.swift b/LoopFollow/Alarm/Alarm.swift index 7bf83811a..bc2ab7582 100644 --- a/LoopFollow/Alarm/Alarm.swift +++ b/LoopFollow/Alarm/Alarm.swift @@ -244,23 +244,7 @@ struct Alarm: Identifiable, Codable, Equatable { } // Mute this alarm day or night or always - let cal = Calendar.current - let today = cal.startOfDay(for: now) - let dayStart = cal.date(bySettingHour: config.dayStart.hour, - minute: config.dayStart.minute, - second: 0, - of: today)! - let nightStart = cal.date(bySettingHour: config.nightStart.hour, - minute: config.nightStart.minute, - second: 0, - of: today)! - - let isNight: Bool - if nightStart >= dayStart { - isNight = (now >= nightStart) || (now < dayStart) - } else { - isNight = (now >= nightStart) && (now < dayStart) - } + let isNight = config.isNight(at: now) let isDay = !isNight switch playSoundOption { diff --git a/LoopFollow/Alarm/AlarmCondition/AlarmCondition.swift b/LoopFollow/Alarm/AlarmCondition/AlarmCondition.swift index 242d84069..2b082dc5f 100644 --- a/LoopFollow/Alarm/AlarmCondition/AlarmCondition.swift +++ b/LoopFollow/Alarm/AlarmCondition/AlarmCondition.swift @@ -77,11 +77,7 @@ extension AlarmCondition { if !passesBGLimits(alarm: alarm, data: data) { return false } // time-of-day guard - let comps = Calendar.current.dateComponents([.hour, .minute], from: now) - let nowMin = (comps.hour! * 60) + comps.minute! - let dStart = config.dayStart.minutesSinceMidnight - let nStart = config.nightStart.minutesSinceMidnight - let isNight = (nowMin < dStart) || (nowMin >= nStart) + let isNight = config.isNight(at: now) switch alarm.activeOption { case .always: diff --git a/LoopFollow/Alarm/AlarmConfiguration.swift b/LoopFollow/Alarm/AlarmConfiguration.swift index 5c3e9b290..ee06a00b9 100644 --- a/LoopFollow/Alarm/AlarmConfiguration.swift +++ b/LoopFollow/Alarm/AlarmConfiguration.swift @@ -31,4 +31,17 @@ struct AlarmConfiguration: Codable, Equatable { autoSnoozeCGMStart: false, enableVolumeButtonSnooze: false ) + + /// Night runs from `nightStart` up to the next `dayStart`, wrapping past midnight + /// when `nightStart` is later in the day than `dayStart`. + func isNight(at date: Date, calendar: Calendar = .current) -> Bool { + let comps = calendar.dateComponents([.hour, .minute], from: date) + let now = comps.hour! * 60 + comps.minute! + let day = dayStart.minutesSinceMidnight + let night = nightStart.minutesSinceMidnight + if night >= day { + return now >= night || now < day + } + return now >= night && now < day + } } diff --git a/Tests/AlarmConditions/DayNightScheduleTests.swift b/Tests/AlarmConditions/DayNightScheduleTests.swift new file mode 100644 index 000000000..3a94fc08b --- /dev/null +++ b/Tests/AlarmConditions/DayNightScheduleTests.swift @@ -0,0 +1,97 @@ +// LoopFollow +// DayNightScheduleTests.swift + +import Foundation +@testable import LoopFollow +import Testing + +@Suite(.serialized) +struct DayNightScheduleTests { + private func config(day: (Int, Int), night: (Int, Int)) -> AlarmConfiguration { + var c = AlarmConfiguration.default + c.dayStart = TimeOfDay(hour: day.0, minute: day.1) + c.nightStart = TimeOfDay(hour: night.0, minute: night.1) + return c + } + + private func at(_ hour: Int, _ minute: Int) -> Date { + Calendar.current.date(bySettingHour: hour, minute: minute, second: 0, of: Date())! + } + + private func lowAlarm(active: ActiveOption) -> Alarm { + var alarm = Alarm.low(belowBG: 80, persistentMinutes: 0) + alarm.activeOption = active + return alarm + } + + private func lowData(endingAt now: Date) -> AlarmData { + let readings = [60, 60, 60].enumerated().map { i, v in + GlucoseValue(sgv: v, date: now.addingTimeInterval(-Double(2 - i) * 300)) + } + return AlarmData.withGlucose(readings: readings) + } + + // MARK: - isNight + + @Test("evening night start: night wraps past midnight") + func eveningNightStart() { + let c = config(day: (7, 0), night: (22, 0)) + + #expect(!c.isNight(at: at(7, 0))) + #expect(!c.isNight(at: at(12, 0))) + #expect(!c.isNight(at: at(21, 59))) + #expect(c.isNight(at: at(22, 0))) + #expect(c.isNight(at: at(0, 0))) + #expect(c.isNight(at: at(6, 59))) + } + + @Test("night start at midnight: night is only 00:00 until day start") + func midnightNightStart() { + let c = config(day: (7, 0), night: (0, 0)) + + #expect(c.isNight(at: at(0, 0))) + #expect(c.isNight(at: at(3, 30))) + #expect(c.isNight(at: at(6, 59))) + #expect(!c.isNight(at: at(7, 0))) + #expect(!c.isNight(at: at(12, 0))) + #expect(!c.isNight(at: at(23, 59))) + } + + @Test("night start after midnight but before day start") + func earlyMorningNightStart() { + let c = config(day: (7, 0), night: (1, 30)) + + #expect(!c.isNight(at: at(0, 45))) + #expect(c.isNight(at: at(1, 30))) + #expect(c.isNight(at: at(6, 59))) + #expect(!c.isNight(at: at(7, 0))) + } + + // MARK: - shouldFire + + @Test("day-only alarm fires during the day when night starts at midnight") + func dayAlarmFiresWithMidnightNightStart() { + let c = config(day: (7, 0), night: (0, 0)) + let now = at(12, 0) + + #expect(LowBGCondition().shouldFire(alarm: lowAlarm(active: .day), data: lowData(endingAt: now), now: now, config: c)) + } + + @Test("day-only alarm is held back at night when night starts at midnight") + func dayAlarmHeldAtNightWithMidnightNightStart() { + let c = config(day: (7, 0), night: (0, 0)) + let now = at(3, 0) + + #expect(!LowBGCondition().shouldFire(alarm: lowAlarm(active: .day), data: lowData(endingAt: now), now: now, config: c)) + } + + @Test("night-only alarm follows the same schedule") + func nightAlarmWithMidnightNightStart() { + let c = config(day: (7, 0), night: (0, 0)) + let night = at(3, 0) + let day = at(12, 0) + + #expect(LowBGCondition().shouldFire(alarm: lowAlarm(active: .night), data: lowData(endingAt: night), now: night, config: c)) + #expect(!LowBGCondition().shouldFire(alarm: lowAlarm(active: .night), data: lowData(endingAt: day), now: day, config: c)) + } +}