فهرست منبع

Active during fix

Jonas Björkert 1 سال پیش
والد
کامیت
878d568b02

+ 20 - 0
LoopFollow/Alarm/Alarm.swift

@@ -32,6 +32,26 @@ enum ActiveOption: String, CaseIterable, Codable, DayNightDisplayable {
     case always, day, night
 }
 
+extension PlaySoundOption {
+    static func allowed(for active: ActiveOption) -> [PlaySoundOption] {
+        switch active {
+        case .always: return [.always, .day, .night, .never]
+        case .day: return [.day, .never]
+        case .night: return [.night, .never]
+        }
+    }
+}
+
+extension RepeatSoundOption {
+    static func allowed(for active: ActiveOption) -> [RepeatSoundOption] {
+        switch active {
+        case .always: return [.always, .day, .night, .never]
+        case .day: return [.day, .never]
+        case .night: return [.night, .never]
+        }
+    }
+}
+
 struct Alarm: Identifiable, Codable, Equatable {
     var id: UUID = .init()
     var type: AlarmType

+ 40 - 4
LoopFollow/Alarm/AlarmEditing/Components/AlarmAudioSection.swift

@@ -31,8 +31,26 @@ struct AlarmAudioSection: View {
                 TonePickerSheet(selected: $alarm.soundFile)
             }
 
-            AlarmEnumMenuPicker(title: "Play", selection: $alarm.playSoundOption)
-            AlarmEnumMenuPicker(title: "Repeat", selection: $alarm.repeatSoundOption)
+            AlarmEnumMenuPicker(
+                title: "Play",
+                selection: $alarm.playSoundOption,
+                allowed: PlaySoundOption.allowed(for: alarm.activeOption)
+            )
+            AlarmEnumMenuPicker(
+                title: "Repeat",
+                selection: $alarm.repeatSoundOption,
+                allowed: RepeatSoundOption.allowed(for: alarm.activeOption)
+            )
+        }.onChange(of: alarm.activeOption) { newActive in
+            let playAllowed = PlaySoundOption.allowed(for: newActive)
+            if !playAllowed.contains(alarm.playSoundOption) {
+                alarm.playSoundOption = playAllowed.last!
+            }
+
+            let repeatAllowed = RepeatSoundOption.allowed(for: newActive)
+            if !repeatAllowed.contains(alarm.repeatSoundOption) {
+                alarm.repeatSoundOption = repeatAllowed.last!
+            }
         }
     }
 }
@@ -40,18 +58,36 @@ struct AlarmAudioSection: View {
 struct AlarmEnumMenuPicker<E: CaseIterable & Hashable & DayNightDisplayable>: View {
     let title: String
     @Binding var selection: E
+    var allowed: [E]
 
     var body: some View {
         HStack {
             Text(title)
             Spacer()
             Picker("", selection: $selection) {
-                ForEach(Array(E.allCases), id: \.self) { option in
-                    Text(option.displayName).tag(option)
+                ForEach(allowed, id: \.self) { opt in
+                    Text(opt.displayName).tag(opt)
                 }
             }
+            // if the current selection became invalid, snap to the first allowed
+            .onAppear { validate() }
+            .onChange(of: allowed) { _ in validate() }
         }
     }
+
+    private func validate() {
+        if !allowed.contains(selection), let first = allowed.first {
+            selection = first
+        }
+    }
+}
+
+extension AlarmEnumMenuPicker where E: CaseIterable {
+    init(title: String, selection: Binding<E>) {
+        self.title = title
+        _selection = selection
+        allowed = Array(E.allCases)
+    }
 }
 
 private struct TonePickerSheet: View {

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

@@ -31,8 +31,8 @@ struct BuildExpireAlarmEditor: View {
                 )
             )
 
-            AlarmAudioSection(alarm: $alarm)
             AlarmActiveSection(alarm: $alarm)
+            AlarmAudioSection(alarm: $alarm)
             AlarmSnoozeSection(
                 alarm: $alarm,
                 range: 1 ... 14,

+ 1 - 3
LoopFollow/Alarm/AlarmEditing/Editors/FastDropAlarmEditor.swift

@@ -53,10 +53,8 @@ struct FastDropAlarmEditor: View {
                 value: $alarm.threshold
             )
 
-            AlarmAudioSection(alarm: $alarm)
-
             AlarmActiveSection(alarm: $alarm)
-
+            AlarmAudioSection(alarm: $alarm)
             AlarmSnoozeSection(
                 alarm: $alarm,
                 range: 5 ... 60,

+ 1 - 1
LoopFollow/Alarm/AlarmEditing/Editors/HighBgAlarmEditor.swift

@@ -45,8 +45,8 @@ struct HighBgAlarmEditor: View {
                 )
             )
 
-            AlarmAudioSection(alarm: $alarm)
             AlarmActiveSection(alarm: $alarm)
+            AlarmAudioSection(alarm: $alarm)
             AlarmSnoozeSection(
                 alarm: $alarm,
                 range: 10 ... 120,

+ 1 - 3
LoopFollow/Alarm/AlarmEditing/Editors/LowBgAlarmEditor.swift

@@ -58,10 +58,8 @@ struct LowBgAlarmEditor: View {
                 )
             )
 
-            AlarmAudioSection(alarm: $alarm)
-
             AlarmActiveSection(alarm: $alarm)
-
+            AlarmAudioSection(alarm: $alarm)
             AlarmSnoozeSection(
                 alarm: $alarm,
                 range: 5 ... 30,

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

@@ -29,10 +29,8 @@ struct MissedReadingEditor: View {
                 )
             )
 
-            AlarmAudioSection(alarm: $alarm)
-
             AlarmActiveSection(alarm: $alarm)
-
+            AlarmAudioSection(alarm: $alarm)
             AlarmSnoozeSection(
                 alarm: $alarm,
                 range: 10 ... 180,

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

@@ -34,8 +34,8 @@ struct NotLoopingAlarmEditor: View {
                 )
             )
 
-            AlarmAudioSection(alarm: $alarm)
             AlarmActiveSection(alarm: $alarm)
+            AlarmAudioSection(alarm: $alarm)
             AlarmSnoozeSection(
                 alarm: $alarm,
                 range: 10 ... 120,