Jelajahi Sumber

updated main app snooze function to use the same functions as the notification actions

Charlie Chrisman 4 bulan lalu
induk
melakukan
716c52692f

+ 23 - 0
Trio/Sources/Modules/Snooze/SnoozeStateModel.swift

@@ -5,11 +5,18 @@ extension Snooze {
     @Observable final class StateModel: BaseStateModel<Provider> {
         @ObservationIgnored @Persisted(key: "UserNotificationsManager.snoozeUntilDate") var snoozeUntilDate: Date = .distantPast
         @ObservationIgnored @Injected() var glucoseStogare: GlucoseStorage!
+        @ObservationIgnored @Injected() var notificationsManager: UserNotificationsManager!
+        @ObservationIgnored @Injected() var broadcaster: Broadcaster!
 
         var alarm: GlucoseAlarm?
 
         override func subscribe() {
             alarm = glucoseStogare.alarm
+            broadcaster.register(SnoozeObserver.self, observer: self)
+        }
+
+        deinit {
+            broadcaster.unregister(SnoozeObserver.self, observer: self)
         }
 
         // Add validation helper inside the class
@@ -29,5 +36,21 @@ extension Snooze {
                 alarm = glucoseStogare.alarm
             }
         }
+
+        @MainActor func applySnooze(_ duration: TimeInterval) async {
+            // Allow any duration chosen in the Snooze UI, while keeping validation for quick actions elsewhere.
+            snoozeUntilDate = duration > 0 ? Date().addingTimeInterval(duration) : .distantPast
+            alarm = glucoseStogare.alarm
+            await notificationsManager.applySnooze(for: duration)
+        }
+    }
+}
+
+extension Snooze.StateModel: SnoozeObserver {
+    func snoozeDidChange(_ untilDate: Date) {
+        Task { @MainActor in
+            snoozeUntilDate = untilDate
+            alarm = glucoseStogare.alarm
+        }
     }
 }

+ 7 - 4
Trio/Sources/Modules/Snooze/View/SnoozeRootView.swift

@@ -87,10 +87,13 @@ extension Snooze {
                     let interval = pickerTimes[selectedInterval]
                     let snoozeFor = formatter.string(from: interval)!
                     let untilDate = Date() + interval
-                    state.snoozeUntilDate = untilDate < Date() ? .distantPast : untilDate
-                    debug(.default, "will snooze for \(snoozeFor) until \(dateFormatter.string(from: untilDate))")
-                    snoozeDescription = getSnoozeDescription()
-                    state.hideModal()
+
+                    Task { @MainActor in
+                        await state.applySnooze(interval)
+                        debug(.default, "will snooze for \(snoozeFor) until \(dateFormatter.string(from: untilDate))")
+                        snoozeDescription = getSnoozeDescription()
+                        state.hideModal()
+                    }
                 } label: {
                     Text("Click to Snooze Alerts")
                         .padding()