SnoozeStateModel.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import Observation
  2. import SwiftUI
  3. extension Snooze {
  4. @Observable final class StateModel: BaseStateModel<Provider> {
  5. @ObservationIgnored @Persisted(key: "UserNotificationsManager.snoozeUntilDate") var snoozeUntilDate: Date = .distantPast
  6. @ObservationIgnored @Injected() var glucoseStorage: GlucoseStorage!
  7. @ObservationIgnored @Injected() var notificationsManager: UserNotificationsManager!
  8. @ObservationIgnored @Injected() var broadcaster: Broadcaster!
  9. var alarm: GlucoseAlarm?
  10. override func subscribe() {
  11. alarm = glucoseStorage.alarm
  12. broadcaster.register(SnoozeObserver.self, observer: self)
  13. }
  14. func unsubscribe() {
  15. broadcaster.unregister(SnoozeObserver.self, observer: self)
  16. }
  17. // Add validation helper inside the class
  18. private func validateSnoozeDuration(_ duration: TimeInterval) -> Bool {
  19. // Only allow durations matching our defined actions
  20. NotificationResponseAction.allCases
  21. .map(\.duration)
  22. .contains(duration)
  23. }
  24. @MainActor func applySnooze(_ duration: TimeInterval) async {
  25. // Canonical path: notificationsManager.applySnooze → BaseTrioAlertManager
  26. // writes the persisted date, mutes AlertMuter, clears pending UNs, and
  27. // broadcasts SnoozeObserver. `snoozeDidChange` below then updates the
  28. // @Persisted value here for SwiftUI observation.
  29. alarm = glucoseStorage.alarm
  30. await notificationsManager.applySnooze(for: duration)
  31. }
  32. }
  33. }
  34. extension Snooze.StateModel: SnoozeObserver {
  35. func snoozeDidChange(_ untilDate: Date) {
  36. snoozeUntilDate = untilDate
  37. alarm = glucoseStorage.alarm
  38. }
  39. }