From d68b887790529a2818f5b83ae1d6d4b235d76ad6 Mon Sep 17 00:00:00 2001 From: Jeremy Storring Date: Sat, 1 Jun 2024 17:02:42 -0600 Subject: [PATCH] Addressed review comments and added the ability to add min and max values to preferences --- .../PreferencesEditorDataFlow.swift | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorDataFlow.swift b/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorDataFlow.swift index afdf35ea7..54a74c5ef 100644 --- a/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorDataFlow.swift +++ b/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorDataFlow.swift @@ -11,7 +11,11 @@ enum PreferencesEditor { enum FieldType { case boolean(keypath: WritableKeyPath) - case decimal(keypath: WritableKeyPath, minVal: Decimal? = nil, maxVal: Decimal? = nil) + case decimal( + keypath: WritableKeyPath, + minVal: WritableKeyPath? = nil, + maxVal: WritableKeyPath? = nil + ) case insulinCurve(keypath: WritableKeyPath) } @@ -59,12 +63,12 @@ enum PreferencesEditor { settable?.set(keypath, value: value) case let (.decimal(keypath, minVal, maxVal), value as Decimal): let constrainedValue: Decimal - if let minValue = minVal, let maxValue = maxVal { - constrainedValue = min(max(value, minValue), maxValue) - } else if let minValue = minVal { - constrainedValue = max(value, minValue) - } else if let maxValue = maxVal { - constrainedValue = min(value, maxValue) + if let minValue = minVal, let minValueDecimal: Decimal = settable?.get(minValue), let maxValue = maxVal, let maxValueDecimal: Decimal = settable?.get(maxValue) { + constrainedValue = min(max(value, minValueDecimal), maxValueDecimal) + } else if let minValue = minVal, let minValueDecimal: Decimal = settable?.get(minValue) { + constrainedValue = max(value, minValueDecimal) + } else if let maxValue = maxVal, let maxValueDecimal: Decimal = settable?.get(maxValue) { + constrainedValue = min(value, maxValueDecimal) } else { constrainedValue = value }