From a5156a6f03c547d001fbd57bb26cf38b9d4fb7ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20M=C3=A5rtensson?= Date: Sun, 29 Oct 2023 21:20:04 +0100 Subject: [PATCH] Bug fix for saved profiles regarding SMB and UAM basal minutes --- FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift | 1 - .../OverrideProfilesStateModel.swift | 14 +++++++--- .../View/OverrideProfilesRootView.swift | 28 ++++++++++--------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift b/FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift index ca0a7df66..f264e2353 100644 --- a/FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift +++ b/FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift @@ -120,7 +120,6 @@ final class OpenAPS { func oref2() -> RawJSON { coredataContext.performAndWait { - let now = Date() let preferences = storage.retrieve(OpenAPS.Settings.preferences, as: Preferences.self) var hbt_ = preferences?.halfBasalExerciseTarget ?? 160 let wp = preferences?.weightPercentage ?? 1 diff --git a/FreeAPS/Sources/Modules/OverrideProfilesConfig/OverrideProfilesStateModel.swift b/FreeAPS/Sources/Modules/OverrideProfilesConfig/OverrideProfilesStateModel.swift index dd7e55921..953bdfcdd 100644 --- a/FreeAPS/Sources/Modules/OverrideProfilesConfig/OverrideProfilesStateModel.swift +++ b/FreeAPS/Sources/Modules/OverrideProfilesConfig/OverrideProfilesStateModel.swift @@ -24,13 +24,15 @@ extension OverrideProfilesConfig { @Published var end: Decimal = 23 @Published var smbMinutes: Decimal = 0 @Published var uamMinutes: Decimal = 0 + @Published var defaultSmbMinutes: Decimal = 0 + @Published var defaultUamMinutes: Decimal = 0 var units: GlucoseUnits = .mmolL override func subscribe() { units = settingsManager.settings.units - smbMinutes = settingsManager.preferences.maxSMBBasalMinutes - uamMinutes = settingsManager.preferences.maxUAMSMBBasalMinutes + defaultSmbMinutes = settingsManager.preferences.maxSMBBasalMinutes + defaultUamMinutes = settingsManager.preferences.maxUAMSMBBasalMinutes presets = [OverridePresets(context: coredataContext)] } @@ -151,8 +153,8 @@ extension OverrideProfilesConfig { saveOverride.end = profile.end } else { saveOverride.smbIsAlwaysOff = false } - saveOverride.smbMinutes = smbMinutes as NSDecimalNumber - saveOverride.uamMinutes = uamMinutes as NSDecimalNumber + saveOverride.smbMinutes = (profile.smbMinutes ?? 0) as NSDecimalNumber + saveOverride.uamMinutes = (profile.uamMinutes ?? 0) as NSDecimalNumber } try? self.coredataContext.save() } @@ -221,6 +223,8 @@ extension OverrideProfilesConfig { override_target = false smbIsOff = false advancedSettings = false + smbMinutes = defaultSmbMinutes + uamMinutes = defaultUamMinutes } } } @@ -240,6 +244,8 @@ extension OverrideProfilesConfig { profiles.date = Date() try? self.coredataContext.save() } + smbMinutes = defaultSmbMinutes + uamMinutes = defaultUamMinutes } } } diff --git a/FreeAPS/Sources/Modules/OverrideProfilesConfig/View/OverrideProfilesRootView.swift b/FreeAPS/Sources/Modules/OverrideProfilesConfig/View/OverrideProfilesRootView.swift index d00107fb4..de691d474 100644 --- a/FreeAPS/Sources/Modules/OverrideProfilesConfig/View/OverrideProfilesRootView.swift +++ b/FreeAPS/Sources/Modules/OverrideProfilesConfig/View/OverrideProfilesRootView.swift @@ -159,9 +159,8 @@ extension OverrideProfilesConfig { } HStack { Text("SMB Minutes") - let minutes = state.settingsManager.preferences.maxSMBBasalMinutes DecimalTextField( - minutes.formatted(), + "0", value: $state.smbMinutes, formatter: formatter, cleanInput: false @@ -170,9 +169,8 @@ extension OverrideProfilesConfig { } HStack { Text("UAM SMB Minutes") - let uam_minutes = state.settingsManager.preferences.maxUAMSMBBasalMinutes DecimalTextField( - uam_minutes.formatted(), + "0", value: $state.uamMinutes, formatter: formatter, cleanInput: false @@ -217,10 +215,7 @@ extension OverrideProfilesConfig { comment: "" ) } - .disabled( - (state.percentage == 100 && !state.override_target && !state.smbIsOff) || - (!state._indefinite && state.duration == 0) || (state.override_target && state.target == 0) - ) + .disabled(unChanged()) .buttonStyle(BorderlessButtonStyle()) .font(.callout) .controlSize(.mini) @@ -248,12 +243,8 @@ extension OverrideProfilesConfig { .frame(maxWidth: .infinity, alignment: .trailing) .buttonStyle(BorderlessButtonStyle()) .controlSize(.mini) - .disabled( - (state.percentage == 100 && !state.override_target && !state.smbIsOff) || - (!state._indefinite && state.duration == 0) || (state.override_target && state.target == 0) - ) + .disabled(unChanged()) } - .sheet(isPresented: $isSheetPresented) { presetPopover } @@ -336,6 +327,17 @@ extension OverrideProfilesConfig { } } + private func unChanged() -> Bool { + let isChanged = (state.percentage == 100 && !state.override_target && !state.smbIsOff && !state.advancedSettings) || + (!state._indefinite && state.duration == 0) || (state.override_target && state.target == 0) || + ( + state.percentage == 100 && !state.override_target && !state.smbIsOff && state.isf && state.cr && state + .smbMinutes == state.defaultSmbMinutes && state.uamMinutes == state.defaultUamMinutes + ) + + return isChanged + } + private func removeProfile(at offsets: IndexSet) { for index in offsets { let language = fetchedProfiles[index]