Parcourir la source

Default values

Jonas Björkert il y a 1 an
Parent
commit
13905210bc

+ 27 - 0
LoopFollow/Alarm/Alarm.swift

@@ -217,34 +217,61 @@ struct Alarm: Identifiable, Codable, Equatable {
             repeatSoundOption = .always
         case .low:
             soundFile = .indeed
+            belowBG = 80
+            persistentMinutes = 0
+            predictiveMinutes = 0
         case .iob:
             soundFile = .alertToneRingtone1
+            delta = 1
+            monitoringWindow = 2
+            predictiveMinutes = 30
+            threshold = 6
         case .cob:
             soundFile = .alertToneRingtone2
+            threshold = 20
         case .high:
             soundFile = .timeHasCome
+            aboveBG = 180
+            persistentMinutes = 0
         case .fastDrop:
             soundFile = .bigClockTicking
+            delta = 18
+            monitoringWindow = 2
         case .fastRise:
             soundFile = .cartoonFailStringsTrumpet
+            delta = 10
+            monitoringWindow = 3
         case .missedReading:
             soundFile = .cartoonTipToeSneakyWalk
+            threshold = 16
         case .notLooping:
             soundFile = .sciFiEngineShutDown
+            threshold = 31
         case .missedBolus:
             soundFile = .dholShuffleloop
+            monitoringWindow = 15
+            predictiveMinutes = 15
+            delta = 0.1
+            threshold = 4
         case .sensorChange:
             soundFile = .wakeUpWillYou
+            threshold = 12
         case .pumpChange:
             soundFile = .wakeUpWillYou
+            threshold = 12
         case .pump:
             soundFile = .marimbaDescend
+            threshold = 20
         case .battery:
             soundFile = .machineCharge
+            threshold = 20
         case .batteryDrop:
             soundFile = .machineCharge
+            delta = 10
+            monitoringWindow = 15
         case .recBolus:
             soundFile = .dholShuffleloop
+            threshold = 1
         case .overrideStart:
             soundFile = .endingReached
             repeatSoundOption = .never

+ 53 - 5
LoopFollow/Alarm/AlarmEditing/Components/AlarmStepperSection.swift

@@ -5,13 +5,21 @@
 import SwiftUI
 
 struct AlarmStepperSection: View {
+    // MARK: – public parameters
+
     let header: String?
     let footer: String?
     let title: String
     let range: ClosedRange<Double>
     let step: Double
     let unitLabel: String?
-    @Binding var value: Double
+
+    // MARK: – private binding (always Double?)
+
+    @Binding
+    private var value: Double?
+
+    // MARK: – designated initialiser  (Double?)
 
     init(
         header: String? = nil,
@@ -20,7 +28,7 @@ struct AlarmStepperSection: View {
         range: ClosedRange<Double>,
         step: Double,
         unitLabel: String? = nil,
-        value: Binding<Double>
+        value: Binding<Double?>
     ) {
         self.header = header
         self.footer = footer
@@ -31,17 +39,57 @@ struct AlarmStepperSection: View {
         _value = value
     }
 
+    // MARK: – convenience initialiser  (Int?)
+
+    /// Same API but for **`Binding<Int?>`** — it bridges to Double internally.
+    init(
+        header: String? = nil,
+        footer: String? = nil,
+        title: String,
+        range: ClosedRange<Double>,
+        step: Double,
+        unitLabel: String? = nil,
+        value intValue: Binding<Int?>
+    ) {
+        self.init(
+            header: header,
+            footer: footer,
+            title: title,
+            range: range,
+            step: step,
+            unitLabel: unitLabel,
+            value: Binding<Double?>(
+                get: { intValue.wrappedValue.map(Double.init) },
+                set: { newVal in intValue.wrappedValue = newVal.map(Int.init) }
+            )
+        )
+    }
+
+    // MARK: – derived non-optional Binding<Double>
+
+    private var nonOptional: Binding<Double> {
+        Binding(
+            get: { value ?? range.lowerBound },
+            set: { newVal in value = newVal }
+        )
+    }
+
+    // MARK: – view
+
     var body: some View {
         Section(
             header: header.map(Text.init),
             footer: footer.map(Text.init)
         ) {
-            Stepper(value: $value, in: range, step: step) {
+            Stepper(value: nonOptional, in: range, step: step) {
                 HStack {
                     Text(title)
                     Spacer()
-                    Text("\(Int(value))\(unitLabel.map { " \($0)" } ?? "")")
-                        .foregroundColor(.secondary)
+                    Text(
+                        "\(Int(nonOptional.wrappedValue))" +
+                            (unitLabel.map { " \($0)" } ?? "")
+                    )
+                    .foregroundColor(.secondary)
                 }
             }
         }

+ 1 - 4
LoopFollow/Alarm/AlarmEditing/Editors/BatteryAlarmEditor.swift

@@ -23,10 +23,7 @@ struct BatteryAlarmEditor: View {
                 range: 0 ... 100,
                 step: 5,
                 unitLabel: "%",
-                value: Binding(
-                    get: { alarm.threshold ?? 20 },
-                    set: { alarm.threshold = $0 }
-                )
+                value: $alarm.threshold
             )
 
             AlarmActiveSection(alarm: $alarm)

+ 2 - 8
LoopFollow/Alarm/AlarmEditing/Editors/BatteryDropAlarmEditor.swift

@@ -23,10 +23,7 @@ struct BatteryDropAlarmEditor: View {
                 range: 5 ... 100,
                 step: 5,
                 unitLabel: "%",
-                value: Binding(
-                    get: { alarm.delta ?? 10 },
-                    set: { alarm.delta = $0 }
-                )
+                value: $alarm.delta
             )
 
             AlarmStepperSection(
@@ -36,10 +33,7 @@ struct BatteryDropAlarmEditor: View {
                 range: 5 ... 30,
                 step: 5,
                 unitLabel: "min",
-                value: Binding(
-                    get: { Double(alarm.monitoringWindow ?? 15) },
-                    set: { alarm.monitoringWindow = Int($0) }
-                )
+                value: $alarm.monitoringWindow
             )
 
             AlarmActiveSection(alarm: $alarm)

+ 1 - 4
LoopFollow/Alarm/AlarmEditing/Editors/BuildExpireAlarmEditor.swift

@@ -22,10 +22,7 @@ struct BuildExpireAlarmEditor: View {
                 range: 1 ... 14,
                 step: 1,
                 unitLabel: alarm.type.snoozeTimeUnit.label,
-                value: Binding(
-                    get: { alarm.threshold ?? 1 },
-                    set: { alarm.threshold = $0 }
-                )
+                value: $alarm.threshold
             )
 
             AlarmActiveSection(alarm: $alarm)

+ 1 - 4
LoopFollow/Alarm/AlarmEditing/Editors/COBAlarmEditor.swift

@@ -23,10 +23,7 @@ struct COBAlarmEditor: View {
                 range: 1 ... 200,
                 step: 1,
                 unitLabel: "g",
-                value: Binding(
-                    get: { alarm.threshold ?? 20 },
-                    set: { alarm.threshold = $0 }
-                )
+                value: $alarm.threshold
             )
 
             AlarmActiveSection(alarm: $alarm)

+ 2 - 6
LoopFollow/Alarm/AlarmEditing/Editors/FastDropAlarmEditor.swift

@@ -22,22 +22,18 @@ struct FastDropAlarmEditor: View {
                 title: "Falls by",
                 range: 3 ... 54,
                 value: Binding(
-                    get: { alarm.delta ?? 18 },
+                    get: { alarm.delta ?? 18 }, // This value is not used, the default value is set on the alarm
                     set: { alarm.delta = $0 }
                 )
             )
 
-            // TODO: In the migration script, use 1 value less than stored since we are switching from readings to drops
             AlarmStepperSection(
                 header: "Consecutive Drops",
                 footer: "Number of drops—each meeting the rate above—required before an alert fires.",
                 title: "Number of Drops",
                 range: 1 ... 3,
                 step: 1,
-                value: Binding(
-                    get: { Double(alarm.monitoringWindow ?? 2) },
-                    set: { alarm.monitoringWindow = Int($0) }
-                )
+                value: $alarm.monitoringWindow
             )
 
             AlarmBGLimitSection(

+ 2 - 5
LoopFollow/Alarm/AlarmEditing/Editors/FastRiseAlarmEditor.swift

@@ -24,7 +24,7 @@ struct FastRiseAlarmEditor: View {
                 title: "Rises by",
                 range: 3 ... 54,
                 value: Binding(
-                    get: { alarm.delta ?? 3 },
+                    get: { alarm.delta ?? 10 }, // This value has not effect since it is set as default on the alarm
                     set: { alarm.delta = $0 }
                 )
             )
@@ -36,10 +36,7 @@ struct FastRiseAlarmEditor: View {
                 title: "Rises in a row",
                 range: 1 ... 3,
                 step: 1,
-                value: Binding(
-                    get: { Double(alarm.monitoringWindow ?? 2) },
-                    set: { alarm.monitoringWindow = Int($0) }
-                )
+                value: $alarm.monitoringWindow
             )
 
             AlarmBGLimitSection(

+ 2 - 5
LoopFollow/Alarm/AlarmEditing/Editors/HighBgAlarmEditor.swift

@@ -22,7 +22,7 @@ struct HighBgAlarmEditor: View {
                 title: "BG",
                 range: 120 ... 350,
                 value: Binding(
-                    get: { alarm.aboveBG ?? 180 },
+                    get: { alarm.aboveBG ?? 180 }, // This value is not used, default is set on the alarm type
                     set: { alarm.aboveBG = $0 }
                 )
             )
@@ -35,10 +35,7 @@ struct HighBgAlarmEditor: View {
                 range: 0 ... 120,
                 step: 5,
                 unitLabel: alarm.type.snoozeTimeUnit.label,
-                value: Binding(
-                    get: { Double(alarm.persistentMinutes ?? 0) },
-                    set: { alarm.persistentMinutes = Int($0) }
-                )
+                value: $alarm.persistentMinutes
             )
 
             AlarmActiveSection(alarm: $alarm)

+ 4 - 16
LoopFollow/Alarm/AlarmEditing/Editors/IOBAlarmEditor.swift

@@ -24,10 +24,7 @@ struct IOBAlarmEditor: View {
                 range: 0.1 ... 20,
                 step: 0.1,
                 unitLabel: "Units",
-                value: Binding(
-                    get: { alarm.delta ?? 1.0 },
-                    set: { alarm.delta = $0 }
-                )
+                value: $alarm.delta
             )
 
             AlarmStepperSection(
@@ -37,10 +34,7 @@ struct IOBAlarmEditor: View {
                 range: 1 ... 10,
                 step: 1,
                 unitLabel: "Boluses",
-                value: Binding(
-                    get: { Double(alarm.monitoringWindow ?? 2) },
-                    set: { alarm.monitoringWindow = Int($0) }
-                )
+                value: $alarm.monitoringWindow
             )
 
             AlarmStepperSection(
@@ -50,10 +44,7 @@ struct IOBAlarmEditor: View {
                 range: 5 ... 120,
                 step: 5,
                 unitLabel: "min",
-                value: Binding(
-                    get: { Double(alarm.predictiveMinutes ?? 30) },
-                    set: { alarm.predictiveMinutes = Int($0) }
-                )
+                value: $alarm.predictiveMinutes
             )
 
             AlarmStepperSection(
@@ -63,10 +54,7 @@ struct IOBAlarmEditor: View {
                 range: 1 ... 20,
                 step: 0.5,
                 unitLabel: "Units",
-                value: Binding(
-                    get: { alarm.threshold ?? 6 },
-                    set: { alarm.threshold = $0 }
-                )
+                value: $alarm.threshold
             )
 
             AlarmActiveSection(alarm: $alarm)

+ 2 - 8
LoopFollow/Alarm/AlarmEditing/Editors/LowBgAlarmEditor.swift

@@ -32,10 +32,7 @@ struct LowBgAlarmEditor: View {
                 range: 0 ... 120,
                 step: 5,
                 unitLabel: alarm.type.snoozeTimeUnit.label,
-                value: Binding(
-                    get: { Double(alarm.persistentMinutes ?? 0) },
-                    set: { alarm.persistentMinutes = Int($0) }
-                )
+                value: $alarm.persistentMinutes
             )
 
             AlarmStepperSection(
@@ -47,10 +44,7 @@ struct LowBgAlarmEditor: View {
                 range: 0 ... 60,
                 step: 5,
                 unitLabel: alarm.type.snoozeTimeUnit.label,
-                value: Binding(
-                    get: { Double(alarm.predictiveMinutes ?? 0) },
-                    set: { alarm.predictiveMinutes = Int($0) }
-                )
+                value: $alarm.predictiveMinutes
             )
 
             AlarmActiveSection(alarm: $alarm)

+ 4 - 16
LoopFollow/Alarm/AlarmEditing/Editors/MissedBolusAlarmEditor.swift

@@ -26,10 +26,7 @@ struct MissedBolusAlarmEditor: View {
                 range: 5 ... 60,
                 step: 5,
                 unitLabel: "min",
-                value: Binding(
-                    get: { Double(alarm.monitoringWindow ?? 15) },
-                    set: { alarm.monitoringWindow = Int($0) }
-                )
+                value: $alarm.monitoringWindow
             )
 
             AlarmStepperSection(
@@ -40,10 +37,7 @@ struct MissedBolusAlarmEditor: View {
                 range: 0 ... 45,
                 step: 5,
                 unitLabel: "min",
-                value: Binding(
-                    get: { Double(alarm.predictiveMinutes ?? 15) },
-                    set: { alarm.predictiveMinutes = Int($0) }
-                )
+                value: $alarm.predictiveMinutes
             )
 
             AlarmStepperSection(
@@ -53,10 +47,7 @@ struct MissedBolusAlarmEditor: View {
                 range: 0.05 ... 2,
                 step: 0.05,
                 unitLabel: "Units",
-                value: Binding(
-                    get: { alarm.delta ?? 0.1 },
-                    set: { alarm.delta = $0 }
-                )
+                value: $alarm.delta
             )
 
             AlarmStepperSection(
@@ -66,10 +57,7 @@ struct MissedBolusAlarmEditor: View {
                 range: 0 ... 15,
                 step: 1,
                 unitLabel: "Grams",
-                value: Binding(
-                    get: { alarm.threshold ?? 4 },
-                    set: { alarm.threshold = $0 }
-                )
+                value: $alarm.threshold
             )
 
             AlarmBGLimitSection(

+ 1 - 4
LoopFollow/Alarm/AlarmEditing/Editors/MissedReadingEditor.swift

@@ -20,10 +20,7 @@ struct MissedReadingEditor: View {
                 range: 11 ... 121,
                 step: 5,
                 unitLabel: alarm.type.snoozeTimeUnit.label,
-                value: Binding(
-                    get: { alarm.threshold ?? 16 },
-                    set: { alarm.threshold = $0 }
-                )
+                value: $alarm.threshold
             )
 
             AlarmActiveSection(alarm: $alarm)

+ 1 - 4
LoopFollow/Alarm/AlarmEditing/Editors/NotLoopingAlarmEditor.swift

@@ -26,10 +26,7 @@ struct NotLoopingAlarmEditor: View {
                 range: 16 ... 61,
                 step: 5,
                 unitLabel: alarm.type.snoozeTimeUnit.label,
-                value: Binding(
-                    get: { alarm.threshold ?? 31 },
-                    set: { alarm.threshold = $0 }
-                )
+                value: $alarm.threshold
             )
 
             AlarmBGLimitSection(

+ 1 - 4
LoopFollow/Alarm/AlarmEditing/Editors/PumpChangeAlarmEditor.swift

@@ -25,10 +25,7 @@ struct PumpChangeAlarmEditor: View {
                 range: 1 ... 24,
                 step: 1,
                 unitLabel: "Hours",
-                value: Binding(
-                    get: { alarm.threshold ?? 12 },
-                    set: { alarm.threshold = $0 }
-                )
+                value: $alarm.threshold
             )
 
             AlarmActiveSection(alarm: $alarm)

+ 1 - 4
LoopFollow/Alarm/AlarmEditing/Editors/PumpVolumeAlarmEditor.swift

@@ -24,10 +24,7 @@ struct PumpVolumeAlarmEditor: View {
                 range: 1 ... 50,
                 step: 1,
                 unitLabel: "Units",
-                value: Binding(
-                    get: { alarm.threshold ?? 20 },
-                    set: { alarm.threshold = $0 }
-                )
+                value: $alarm.threshold
             )
 
             AlarmActiveSection(alarm: $alarm)

+ 1 - 4
LoopFollow/Alarm/AlarmEditing/Editors/RecBolusAlarmEditor.swift

@@ -24,10 +24,7 @@ struct RecBolusAlarmEditor: View {
                 range: 0.1 ... 50,
                 step: 0.1,
                 unitLabel: "Units",
-                value: Binding(
-                    get: { alarm.threshold ?? 1.0 },
-                    set: { alarm.threshold = $0 }
-                )
+                value: $alarm.threshold
             )
 
             AlarmActiveSection(alarm: $alarm)

+ 1 - 4
LoopFollow/Alarm/AlarmEditing/Editors/SensorAgeAlarmEditor.swift

@@ -24,10 +24,7 @@ struct SensorAgeAlarmEditor: View {
                 range: 1 ... 24,
                 step: 1,
                 unitLabel: "hours",
-                value: Binding(
-                    get: { alarm.threshold ?? 12 },
-                    set: { alarm.threshold = $0 }
-                )
+                value: $alarm.threshold
             )
 
             AlarmActiveSection(alarm: $alarm)

+ 2 - 2
LoopFollow/Storage/Storage+Migrate.swift

@@ -614,7 +614,7 @@ extension Storage {
 
         // core trigger parameters
         alarm.delta = Double(take("alertFastDropDelta", default: 10.0))
-        alarm.monitoringWindow = take("alertFastDropReadings", default: 3) // store #readings
+        alarm.monitoringWindow = take("alertFastDropReadings", default: 3) - 1// store #readings
         if take("alertFastDropUseLimit", default: false) {
             alarm.belowBG = Double(take("alertFastDropBelowBG", default: 120.0))
         }
@@ -726,7 +726,7 @@ extension Storage {
         alarm.isEnabled = take("alertMissedReadingActive", default: false)
 
         // “No CGM data for X minutes”
-        alarm.persistentMinutes = take("alertMissedReading", default: 31)
+        alarm.threshold = take("alertMissedReading", default: 31)
 
         // snoozing
         alarm.snoozeDuration = take("alertMissedReadingSnooze", default: 30)

+ 1 - 0
LoopFollow/Task/AlarmTask.swift

@@ -63,6 +63,7 @@ extension MainViewController {
             }
 
             LogManager.shared.log(category: .alarm, message: "Checking alarms based on \(finalAlarmData)", isDebug: true)
+            LogManager.shared.log(category: .alarm, message: "Alarms \(Storage.shared.alarms.value)", isDebug: true)
 
             AlarmManager.shared.checkAlarms(data: finalAlarmData)