|
|
@@ -1,15 +1,67 @@
|
|
|
import Foundation
|
|
|
+import LoopKit
|
|
|
+
|
|
|
+protocol SettableValue {}
|
|
|
+extension Bool: SettableValue {}
|
|
|
+extension Decimal: SettableValue {}
|
|
|
+extension InsulinCurve: SettableValue {}
|
|
|
|
|
|
enum PreferencesEditor {
|
|
|
enum Config {}
|
|
|
|
|
|
- class Field<T>: Identifiable {
|
|
|
+ enum FieldType {
|
|
|
+ case boolean(keypath: WritableKeyPath<Preferences, Bool>)
|
|
|
+ case decimal(keypath: WritableKeyPath<Preferences, Decimal>)
|
|
|
+ case insulinCurve(keypath: WritableKeyPath<Preferences, InsulinCurve>)
|
|
|
+ }
|
|
|
+
|
|
|
+ class Field: Identifiable {
|
|
|
var displayName: String
|
|
|
- var keypath: WritableKeyPath<Preferences, T>
|
|
|
+ var type: FieldType
|
|
|
var infoText: String
|
|
|
- var value: T {
|
|
|
- didSet {
|
|
|
- settable?.onSet(keypath, value: value)
|
|
|
+
|
|
|
+ var boolValue: Bool {
|
|
|
+ get {
|
|
|
+ switch type {
|
|
|
+ case let .boolean(keypath):
|
|
|
+ return settable?.get(keypath) ?? false
|
|
|
+ default: return false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ set { set(value: newValue) }
|
|
|
+ }
|
|
|
+
|
|
|
+ var decimalValue: Decimal {
|
|
|
+ get {
|
|
|
+ switch type {
|
|
|
+ case let .decimal(keypath):
|
|
|
+ return settable?.get(keypath) ?? 0
|
|
|
+ default: return 0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ set { set(value: newValue) }
|
|
|
+ }
|
|
|
+
|
|
|
+ var insulinCurveValue: InsulinCurve {
|
|
|
+ get {
|
|
|
+ switch type {
|
|
|
+ case let .insulinCurve(keypath):
|
|
|
+ return settable?.get(keypath) ?? .rapidActing
|
|
|
+ default: return .rapidActing
|
|
|
+ }
|
|
|
+ }
|
|
|
+ set { set(value: newValue) }
|
|
|
+ }
|
|
|
+
|
|
|
+ private func set<T: SettableValue>(value: T) {
|
|
|
+ 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 (.insulinCurve(keypath), value as InsulinCurve):
|
|
|
+ settable?.set(keypath, value: value)
|
|
|
+ default: break
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -17,20 +69,24 @@ enum PreferencesEditor {
|
|
|
|
|
|
init(
|
|
|
displayName: String,
|
|
|
- keypath: WritableKeyPath<Preferences, T>,
|
|
|
- value: T,
|
|
|
+ type: FieldType,
|
|
|
infoText: String,
|
|
|
settable: PreferencesSettable? = nil
|
|
|
) {
|
|
|
self.displayName = displayName
|
|
|
- self.keypath = keypath
|
|
|
- self.value = value
|
|
|
+ self.type = type
|
|
|
self.infoText = infoText
|
|
|
self.settable = settable
|
|
|
}
|
|
|
|
|
|
let id = UUID()
|
|
|
}
|
|
|
+
|
|
|
+ struct FieldSection: Identifiable {
|
|
|
+ let displayName: String
|
|
|
+ var fields: [Field]
|
|
|
+ let id = UUID()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
protocol PreferencesEditorProvider: Provider {
|
|
|
@@ -40,5 +96,6 @@ protocol PreferencesEditorProvider: Provider {
|
|
|
}
|
|
|
|
|
|
protocol PreferencesSettable: AnyObject {
|
|
|
- func onSet<T>(_ keypath: WritableKeyPath<Preferences, T>, value: T)
|
|
|
+ func set<T>(_ keypath: WritableKeyPath<Preferences, T>, value: T)
|
|
|
+ func get<T>(_ keypath: WritableKeyPath<Preferences, T>) -> T
|
|
|
}
|