浏览代码

Preferences reordered

Ivan Valkou 4 年之前
父节点
当前提交
42890cd895

+ 1 - 1
FreeAPS/Sources/Models/Preferences.swift

@@ -17,7 +17,7 @@ struct Preferences: JSON {
     var maxCOB: Decimal = 120
     var wideBGTargetRange: Bool = false
     var skipNeutralTemps: Bool = false
-    var unsuspendIfNoTemp: Bool = true
+    var unsuspendIfNoTemp: Bool = false
     var bolusSnoozeDIADivisor: Decimal = 2
     var min5mCarbimpact: Decimal = 8
     var autotuneISFAdjustmentFraction: Decimal = 1.0

+ 67 - 10
FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorDataFlow.swift

@@ -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
 }

文件差异内容过多而无法显示
+ 198 - 208
FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorStateModel.swift


+ 44 - 23
FreeAPS/Sources/Modules/PreferencesEditor/View/PreferencesEditorRootView.swift

@@ -38,32 +38,53 @@ extension PreferencesEditor {
                     Toggle("Skip Bolus screen after carbs", isOn: $state.skipBolusScreenAfterCarbs)
                 }
 
-                Section(header: Text("OpenAPS")) {
-                    Picker(selection: $state.insulinCurveField.value, label: Text(state.insulinCurveField.displayName)) {
-                        ForEach(InsulinCurve.allCases) { v in
-                            Text(v.rawValue).tag(v)
-                        }
-                    }
-
-                    ForEach(state.boolFields.indexed(), id: \.1.id) { index, field in
-                        HStack {
-                            Button("", action: {
-                                infoButtonPressed = InfoText(description: field.infoText, oref0Variable: field.displayName)
-                            })
-                            Toggle(field.displayName, isOn: self.$state.boolFields[index].value)
-                        }
-                    }
-
-                    ForEach(state.decimalFields.indexed(), id: \.1.id) { index, field in
-                        HStack {
-                            Button("", action: {
-                                infoButtonPressed = InfoText(description: field.infoText, oref0Variable: field.displayName)
-                            })
-                            Text(field.displayName)
-                            DecimalTextField("0", value: self.$state.decimalFields[index].value, formatter: formatter)
+                ForEach(state.sections.indexed(), id: \.1.id) { sectionIndex, section in
+                    Section(header: Text(section.displayName)) {
+                        ForEach(section.fields.indexed(), id: \.1.id) { fieldIndex, field in
+                            HStack {
+                                switch field.type {
+                                case .boolean:
+                                    ZStack {
+                                        Button("", action: {
+                                            infoButtonPressed = InfoText(
+                                                description: field.infoText,
+                                                oref0Variable: field.displayName
+                                            )
+                                        })
+                                        Toggle(isOn: self.$state.sections[sectionIndex].fields[fieldIndex].boolValue) {
+                                            Text(field.displayName)
+                                        }
+                                    }
+                                case .decimal:
+                                    ZStack {
+                                        Button("", action: {
+                                            infoButtonPressed = InfoText(
+                                                description: field.infoText,
+                                                oref0Variable: field.displayName
+                                            )
+                                        })
+                                        Text(field.displayName)
+                                    }
+                                    DecimalTextField(
+                                        "0",
+                                        value: self.$state.sections[sectionIndex].fields[fieldIndex].decimalValue,
+                                        formatter: formatter
+                                    )
+                                case .insulinCurve:
+                                    Picker(
+                                        selection: $state.sections[sectionIndex].fields[fieldIndex].insulinCurveValue,
+                                        label: Text(field.displayName)
+                                    ) {
+                                        ForEach(InsulinCurve.allCases) { v in
+                                            Text(v.rawValue).tag(v)
+                                        }
+                                    }
+                                }
+                            }
                         }
                     }
                 }
+
                 Section {
                     Text("Edit settings json")
                         .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.settings), from: self)