Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions LoopFollow/Alarm/Alarm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions LoopFollow/Alarm/AlarmCondition/AlarmCondition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions LoopFollow/Alarm/AlarmConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
97 changes: 97 additions & 0 deletions Tests/AlarmConditions/DayNightScheduleTests.swift
Original file line number Diff line number Diff line change
@@ -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))
}
}