|
@@ -11,7 +11,11 @@ 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: WritableKeyPath<Preferences, Decimal>? = nil,
|
|
|
|
|
+ maxVal: WritableKeyPath<Preferences, Decimal>? = nil
|
|
|
|
|
+ )
|
|
|
case insulinCurve(keypath: WritableKeyPath<Preferences, InsulinCurve>)
|
|
case insulinCurve(keypath: WritableKeyPath<Preferences, InsulinCurve>)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -34,7 +38,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 +61,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 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
|
|
|
|
|
+ }
|
|
|
|
|
+ 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
|