Skip to content

Commit

Permalink
Added ability to add min and max values to decimals in preferences page
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Storring authored and Jeremy Storring committed May 22, 2024
1 parent 5e51344 commit 1941a02
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ enum PreferencesEditor {

enum FieldType {
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>)
}

Expand All @@ -34,7 +34,7 @@ enum PreferencesEditor {
var decimalValue: Decimal {
get {
switch type {
case let .decimal(keypath):
case let .decimal(keypath, _, _):
return settable?.get(keypath) ?? 0
default: return 0
}
Expand All @@ -57,8 +57,18 @@ enum PreferencesEditor {
switch (type, value) {
case let (.boolean(keypath), value as Bool):
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):
settable?.set(keypath, value: value)
default: break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ extension PreferencesEditor {
),
Field(
displayName: NSLocalizedString("Remaining Carbs Cap", comment: "Remaining Carbs Cap"),
type: .decimal(keypath: \.remainingCarbsCap),
type: .decimal(keypath: \.remainingCarbsCap, minVal: 90),
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.",
comment: "Remaining Carbs Cap"
Expand Down

0 comments on commit 1941a02

Please sign in to comment.