From d26192b585f39b2a5f972babc45294f7ccf05b95 Mon Sep 17 00:00:00 2001 From: Bill Gestrich <3207996+gestrich@users.noreply.github.com> Date: Tue, 30 Jan 2024 19:15:39 -0500 Subject: [PATCH] Bring back override end date on home view --- LoopCaregiver/LoopCaregiver/Views/Home/HUDView.swift | 8 +++++++- .../Models/RemoteDataServiceManager.swift | 9 ++++++--- .../Extensions/TemporaryScheduleOverride.swift | 11 ++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/LoopCaregiver/LoopCaregiver/Views/Home/HUDView.swift b/LoopCaregiver/LoopCaregiver/Views/Home/HUDView.swift index bd3ec552..96ad6b71 100644 --- a/LoopCaregiver/LoopCaregiver/Views/Home/HUDView.swift +++ b/LoopCaregiver/LoopCaregiver/Views/Home/HUDView.swift @@ -61,12 +61,18 @@ struct HUDView: View { }.onChange(of: hudViewModel.selectedLooper) { newValue in looperPopoverShowing = false } - if let activeOverride = nightscoutDataSource.activeOverride() { + if let (activeOverride, status) = nightscoutDataSource.activeOverrideAndStatus() { HStack { Text(activeOverride.presentableDescription()) .bold() .font(.subheadline) Spacer() + if let endTimeDescription = status.endTimeDescription() { + Text(endTimeDescription) + .foregroundColor(.gray) + .bold() + .font(.subheadline) + } } } diff --git a/LoopCaregiverKit/Sources/LoopCaregiverKit/Models/RemoteDataServiceManager.swift b/LoopCaregiverKit/Sources/LoopCaregiverKit/Models/RemoteDataServiceManager.swift index 95008fb1..ffa6db01 100644 --- a/LoopCaregiverKit/Sources/LoopCaregiverKit/Models/RemoteDataServiceManager.swift +++ b/LoopCaregiverKit/Sources/LoopCaregiverKit/Models/RemoteDataServiceManager.swift @@ -280,8 +280,7 @@ public class RemoteDataServiceManager: ObservableObject, RemoteDataServiceProvid return try await remoteDataProvider.fetchCurrentProfile() } - public func activeOverride() -> NightscoutKit.TemporaryScheduleOverride? { - + public func activeOverrideAndStatus() -> (override: NightscoutKit.TemporaryScheduleOverride, status: NightscoutKit.OverrideStatus)? { /* There are 3 sources of the current override from Nightscout 1. Devicestatus.overrideStatus: Used by NS Plugin (bubble view) @@ -320,7 +319,11 @@ public class RemoteDataServiceManager: ObservableObject, RemoteDataServiceProvid // } // } - return override + return (override, overrideStatus) + } + + public func activeOverride() -> NightscoutKit.TemporaryScheduleOverride? { + return activeOverrideAndStatus()?.override } public func fetchRecentCommands() async throws -> [RemoteCommand] { diff --git a/LoopCaregiverKit/Sources/LoopCaregiverKit/Nightscout/Extensions/TemporaryScheduleOverride.swift b/LoopCaregiverKit/Sources/LoopCaregiverKit/Nightscout/Extensions/TemporaryScheduleOverride.swift index e0735866..23c31571 100644 --- a/LoopCaregiverKit/Sources/LoopCaregiverKit/Nightscout/Extensions/TemporaryScheduleOverride.swift +++ b/LoopCaregiverKit/Sources/LoopCaregiverKit/Nightscout/Extensions/TemporaryScheduleOverride.swift @@ -17,13 +17,22 @@ public extension TemporaryScheduleOverride { func presentableDescription() -> String { return "\(symbol ?? "") \(name ?? "")" } - + func targetRangePresentableDescription() -> String? { guard let targetRange else { return nil } return "\(Int(targetRange.lowerBound)) - \(Int(targetRange.upperBound))" } } +public extension OverrideStatus { + func endTimeDescription() -> String? { + guard let duration, duration < 60 * 60 * 24 else { return nil } + let endDate = timestamp.addingTimeInterval(duration) + let endTimeText = DateFormatter.localizedString(from: endDate, dateStyle: .none, timeStyle: .short) + return String(format: NSLocalizedString("until %@", comment: "The format for the description of a custom preset end date"), endTimeText) + } +} + extension TemporaryScheduleOverride: Hashable { public func hash(into hasher: inout Hasher) {