@@ -7,6 +7,7 @@ extension MainViewController {
77 func processNSOverrides( entries: [ [ String : AnyObject ] ] ) {
88 overrideGraphData. removeAll ( )
99 var activeOverrideNote : String ?
10+ var activeOverrideEndAt : TimeInterval ?
1011
1112 let sorted = entries. sorted { lhs, rhs in
1213 guard
@@ -35,26 +36,29 @@ extension MainViewController {
3536
3637 let start = max ( startDate. timeIntervalSince1970, graphHorizon)
3738
38- let isIndefinite = ( e [ " durationType " ] as? String ) == " indefinite " // Only for Loop overrides
39- var end : TimeInterval
40- if isIndefinite {
41- end = maxEndDate
42- } else {
43- end = start + ( e [ " duration " ] as? Double ?? 5 ) * 60
44- }
39+ let nextStart : TimeInterval ? = {
40+ guard i + 1 < sorted. count,
41+ let nextDateStr = ( sorted [ i + 1 ] [ " timestamp " ] as? String ) ?? ( sorted [ i + 1 ] [ " created_at " ] as? String )
42+ else { return nil }
43+ return NightscoutUtils . parseDate ( nextDateStr) ? . timeIntervalSince1970
44+ } ( )
45+
46+ let durationSeconds = ( e [ " duration " ] as? Double ?? 5 ) * 60
47+ // Loop marks indefinite overrides explicitly; Trio represents them
48+ // as a ~30-day duration. Treat a week or longer as indefinite.
49+ let isIndefinite = ( e [ " durationType " ] as? String ) == " indefinite "
50+ || durationSeconds >= 7 * 24 * 3600
4551
46- // The scheduled end is kept unclamped so the end alarm's early
47- // warning can see ends beyond the graph horizon; nil when the
48- // override runs indefinitely.
49- var scheduledEnd : TimeInterval ? = isIndefinite ? nil : end
52+ var end : TimeInterval = isIndefinite ? maxEndDate : start + durationSeconds
5053
51- if i + 1 < sorted. count,
52- let nextDateStr = ( sorted [ i + 1 ] [ " timestamp " ] as? String ) ?? ( sorted [ i + 1 ] [ " created_at " ] as? String ) ,
53- let nextStart = NightscoutUtils . parseDate ( nextDateStr) ?
54- . timeIntervalSince1970
55- {
54+ // True end for countdown display and the end alarm's early
55+ // warning: based on the raw start and never clamped to the graph
56+ // edge; nil while indefinite.
57+ var trueEnd : TimeInterval ? = isIndefinite ? nil : startDate. timeIntervalSince1970 + durationSeconds
58+
59+ if let nextStart = nextStart {
5660 end = min ( end, nextStart - 60 ) // avoid overlapping overrides
57- scheduledEnd = scheduledEnd . map { min ( $0, nextStart - 60 ) }
61+ trueEnd = trueEnd . map { min ( $0, nextStart - 60 ) }
5862 }
5963
6064 end = min ( end, maxEndDate)
@@ -82,16 +86,18 @@ extension MainViewController {
8286 ?? ( e [ " reason " ] as? String ) ? . trimmingCharacters ( in: . whitespacesAndNewlines)
8387 ?? " " ,
8488 sgv: - 20 ,
85- scheduledEndDate: scheduledEnd
89+ scheduledEndDate: trueEnd
8690 )
8791 overrideGraphData. append ( dot)
8892
8993 if now >= start, now < end {
9094 activeOverrideNote = e [ " notes " ] as? String ?? e [ " reason " ] as? String
95+ activeOverrideEndAt = trueEnd
9196 }
9297 }
9398
9499 Observable . shared. override. value = activeOverrideNote
100+ Observable . shared. overrideEndAt. value = activeOverrideEndAt
95101 if Storage . shared. device. value != " Loop " {
96102 if let note = activeOverrideNote {
97103 infoManager. updateInfoData ( type: . override, value: note)
@@ -102,5 +108,9 @@ extension MainViewController {
102108 if Storage . shared. graphOtherTreatments. value {
103109 updateOverrideGraph ( )
104110 }
111+
112+ #if !targetEnvironment(macCatalyst)
113+ LiveActivityManager . shared. refreshFromCurrentState ( reason: " overrideChanged " )
114+ #endif
105115 }
106116}
0 commit comments