فهرست منبع

Fix stale snooze state in alarm list (#678)

The alarm list categorized snoozed vs. active alarms against Date()
sampled at render time, but nothing re-rendered the view when a snooze
expired. A lapsed snooze could stay in the Snoozed section for hours
while the editor correctly showed it as not snoozed.

Refresh a now snapshot on appear and on returning to the foreground,
and drive all snooze comparisons through it.
Jonas Björkert 3 هفته پیش
والد
کامیت
4422a1e07e
1فایلهای تغییر یافته به همراه13 افزوده شده و 3 حذف شده
  1. 13 3
      LoopFollow/Alarm/AlarmListView.swift

+ 13 - 3
LoopFollow/Alarm/AlarmListView.swift

@@ -37,15 +37,21 @@ struct AlarmListView: View {
         !snoozedAlarms.isEmpty || !activeAlarms.isEmpty || !inactiveAlarms.isEmpty
         !snoozedAlarms.isEmpty || !activeAlarms.isEmpty || !inactiveAlarms.isEmpty
     }
     }
 
 
+    // Snapshot of "now" used to categorize snoozed vs. active alarms. SwiftUI does
+    // not re-render when the wall clock passes a snooze's expiry, so we refresh this
+    // whenever the screen appears or the app returns to the foreground.
+    @State private var now = Date()
+    @Environment(\.scenePhase) private var scenePhase
+
     // MARK: - Categorized Alarms
     // MARK: - Categorized Alarms
 
 
     private var snoozedAlarms: [Alarm] {
     private var snoozedAlarms: [Alarm] {
-        store.value.filter { $0.snoozedUntil ?? .distantPast > Date() && $0.isEnabled && matches($0) }
+        store.value.filter { $0.snoozedUntil ?? .distantPast > now && $0.isEnabled && matches($0) }
             .sorted(by: Alarm.byPriorityThenSpec)
             .sorted(by: Alarm.byPriorityThenSpec)
     }
     }
 
 
     private var activeAlarms: [Alarm] {
     private var activeAlarms: [Alarm] {
-        store.value.filter { $0.isEnabled && ($0.snoozedUntil ?? .distantPast <= Date()) && matches($0) }
+        store.value.filter { $0.isEnabled && ($0.snoozedUntil ?? .distantPast <= now) && matches($0) }
             .sorted(by: Alarm.byPriorityThenSpec)
             .sorted(by: Alarm.byPriorityThenSpec)
     }
     }
 
 
@@ -121,6 +127,10 @@ struct AlarmListView: View {
                 Button { sheetInfo = .picker } label: { Image(systemName: "plus") }
                 Button { sheetInfo = .picker } label: { Image(systemName: "plus") }
             }
             }
         }
         }
+        .onAppear { now = Date() }
+        .onChange(of: scenePhase) { phase in
+            if phase == .active { now = Date() }
+        }
         .preferredColorScheme(Storage.shared.appearanceMode.value.colorScheme)
         .preferredColorScheme(Storage.shared.appearanceMode.value.colorScheme)
     }
     }
 
 
@@ -142,7 +152,7 @@ struct AlarmListView: View {
                     Text(alarm.name)
                     Text(alarm.name)
                         .foregroundColor(.primary)
                         .foregroundColor(.primary)
 
 
-                    if let until = alarm.snoozedUntil, until > Date() {
+                    if let until = alarm.snoozedUntil, until > now {
                         HStack(spacing: 4) {
                         HStack(spacing: 4) {
                             Image(systemName: "zzz")
                             Image(systemName: "zzz")
                                 .font(.caption2)
                                 .font(.caption2)