SnoozerViewModel.swift 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // SnoozerViewModel.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2025-05-04.
  6. // Copyright © 2025 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import Combine
  10. final class SnoozerViewModel: ObservableObject {
  11. @Published var activeAlarm: Alarm?
  12. @Published var snoozeMins: Int = 5
  13. @Published var timeUnitLabel: String = "minutes"
  14. private var bag = Set<AnyCancellable>()
  15. init() {
  16. Observable.shared.currentAlarm.$value
  17. .compactMap { $0 } // drop nils
  18. .map { id in
  19. Storage.shared.alarms.value.first { $0.id == id }
  20. }
  21. .receive(on: DispatchQueue.main)
  22. .sink { [weak self] alarm in
  23. self?.activeAlarm = alarm // may be nil
  24. if let a = alarm {
  25. self?.snoozeMins = a.snoozeDuration
  26. self?.timeUnitLabel = a.type.timeUnit.label
  27. }
  28. }
  29. .store(in: &bag)
  30. }
  31. func snoozeTapped() {
  32. guard let alarm = activeAlarm else { return }
  33. AlarmManager.shared.performSnooze(
  34. snoozeMins * Int(alarm.type.timeUnit.seconds) / 60
  35. )
  36. }
  37. }