diff --git a/LoopFollow/Charts/BGChartModel.swift b/LoopFollow/Charts/BGChartModel.swift index 9b451ac6a..27cb003ee 100644 --- a/LoopFollow/Charts/BGChartModel.swift +++ b/LoopFollow/Charts/BGChartModel.swift @@ -157,6 +157,7 @@ final class BGChartModel: ObservableObject { @Published var now: Date = .init() @Published var diaMarkers: [Date] = [] @Published var midnightMarkers: [Date] = [] + @Published var priorDayTimeMarkers: [Date] = [] @Published var thirtyMinMark: Date? = nil @Published var ninetyMinMark: Date? = nil @@ -596,6 +597,17 @@ final class BGChartModel: ObservableObject { } return cal }() + + // Mark the current local time on each prior day. Calendar arithmetic + // preserves the displayed time of day across daylight-saving changes. + var priorDayTimes: [Date] = [] + var priorDayTime = calendar.date(byAdding: .day, value: -1, to: currentNow) + while let marker = priorDayTime, marker > domainStart { + priorDayTimes.append(marker) + priorDayTime = calendar.date(byAdding: .day, value: -1, to: marker) + } + priorDayTimeMarkers = priorDayTimes + var cursor = calendar.startOfDay(for: domainStart) while cursor <= domainEnd { if cursor >= domainStart { diff --git a/LoopFollow/Charts/BGChartView.swift b/LoopFollow/Charts/BGChartView.swift index 9098bd123..a28aaef65 100644 --- a/LoopFollow/Charts/BGChartView.swift +++ b/LoopFollow/Charts/BGChartView.swift @@ -1095,6 +1095,7 @@ private struct BGChartCanvas: View, Equatable { if showTreatments { treatmentMarks } + priorDayTimeRuleMarks if !isSmall { ruleMarks } else if model.showMidnight { @@ -1462,6 +1463,15 @@ private struct BGChartCanvas: View, Equatable { } } + @ChartContentBuilder + private var priorDayTimeRuleMarks: some ChartContent { + ForEach(model.priorDayTimeMarkers.filter { $0 >= windowStart && $0 <= windowEnd }, id: \.self) { d in + RuleMark(x: .value("same time on prior day", d)) + .lineStyle(StrokeStyle(lineWidth: 1, dash: isSmall ? [2, 2] : [2, 5])) + .foregroundStyle(Color.orange.opacity(isSmall ? 1 : 0.5)) + } + } + @ChartContentBuilder private var ruleMarks: some ChartContent { RuleMark(y: .value("low", model.lowLine))