Explorar o código

Added ability to add min and max values to decimals in preferences page

Jeremy Storring %!s(int64=2) %!d(string=hai) anos
pai
achega
f97d4fd909

+ 14 - 4
FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorDataFlow.swift

@@ -11,7 +11,7 @@ enum PreferencesEditor {
 
 
     enum FieldType {
     enum FieldType {
         case boolean(keypath: WritableKeyPath<Preferences, Bool>)
         case boolean(keypath: WritableKeyPath<Preferences, Bool>)
-        case decimal(keypath: WritableKeyPath<Preferences, Decimal>)
+        case decimal(keypath: WritableKeyPath<Preferences, Decimal>, minVal: Decimal? = nil, maxVal: Decimal? = nil)
         case insulinCurve(keypath: WritableKeyPath<Preferences, InsulinCurve>)
         case insulinCurve(keypath: WritableKeyPath<Preferences, InsulinCurve>)
     }
     }
 
 
@@ -34,7 +34,7 @@ enum PreferencesEditor {
         var decimalValue: Decimal {
         var decimalValue: Decimal {
             get {
             get {
                 switch type {
                 switch type {
-                case let .decimal(keypath):
+                case let .decimal(keypath, _, _):
                     return settable?.get(keypath) ?? 0
                     return settable?.get(keypath) ?? 0
                 default: return 0
                 default: return 0
                 }
                 }
@@ -57,8 +57,18 @@ enum PreferencesEditor {
             switch (type, value) {
             switch (type, value) {
             case let (.boolean(keypath), value as Bool):
             case let (.boolean(keypath), value as Bool):
                 settable?.set(keypath, value: value)
                 settable?.set(keypath, value: value)
-            case let (.decimal(keypath), value as Decimal):
-                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)
+                } else {
+                    constrainedValue = value
+                }
+                settable?.set(keypath, value: constrainedValue)
             case let (.insulinCurve(keypath), value as InsulinCurve):
             case let (.insulinCurve(keypath), value as InsulinCurve):
                 settable?.set(keypath, value: value)
                 settable?.set(keypath, value: value)
             default: break
             default: break

+ 1 - 1
FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorStateModel.swift

@@ -443,7 +443,7 @@ extension PreferencesEditor {
                 ),
                 ),
                 Field(
                 Field(
                     displayName: NSLocalizedString("Remaining Carbs Cap", comment: "Remaining Carbs Cap"),
                     displayName: NSLocalizedString("Remaining Carbs Cap", comment: "Remaining Carbs Cap"),
-                    type: .decimal(keypath: \.remainingCarbsCap),
+                    type: .decimal(keypath: \.remainingCarbsCap, minVal: 90),
                     infoText: NSLocalizedString(
                     infoText: NSLocalizedString(
                         "This is the amount of the maximum number of carbs we’ll assume will absorb over 4h if we don’t yet see carb absorption.",
                         "This is the amount of the maximum number of carbs we’ll assume will absorb over 4h if we don’t yet see carb absorption.",
                         comment: "Remaining Carbs Cap"
                         comment: "Remaining Carbs Cap"