AlarmManager.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // AlarmManager.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2025-03-15.
  6. // Copyright © 2025 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import UserNotifications
  10. class AlarmManager {
  11. static let shared = AlarmManager()
  12. private let evaluators: [AlarmType: AlarmCondition]
  13. private init(
  14. conditionTypes: [AlarmCondition.Type] = [
  15. BuildExpireCondition.self
  16. // TODO: add other condition types here
  17. ]
  18. ) {
  19. var dict = [AlarmType: AlarmCondition]()
  20. conditionTypes.forEach { dict[$0.type] = $0.init() }
  21. evaluators = dict
  22. }
  23. func checkAlarms(data: AlarmData) {
  24. let now = Date()
  25. let alarms = Storage.shared.alarms.value
  26. let sorted = alarms.sorted { lhs, rhs in
  27. // Primary: type priority
  28. if lhs.type.priority != rhs.type.priority {
  29. return lhs.type.priority < rhs.type.priority
  30. }
  31. // Secondary: threshold ordering if applicable
  32. if let asc = lhs.type.thresholdSortAscending {
  33. let leftVal = lhs.threshold ?? (asc ? Double.infinity : -Double.infinity)
  34. let rightVal = rhs.threshold ?? (asc ? Double.infinity : -Double.infinity)
  35. return asc ? leftVal < rightVal : leftVal > rightVal
  36. }
  37. // Tertiary: fallback to insertion order
  38. return false
  39. }
  40. var skipType: AlarmType? = nil
  41. for alarm in sorted {
  42. // If there is already an active (snoozed) alarm of this type, skip to next [type]
  43. if alarm.type == skipType {
  44. continue
  45. }
  46. // If the alarm itself is snoozed skip it, and skip lower‑priority alarms of the same type.
  47. // We still want other types af alarm to go off, so we continue here without breaking
  48. if let until = alarm.snoozedUntil, until > now {
  49. skipType = alarm.type
  50. continue
  51. }
  52. // Evaluate the alarm condition.
  53. guard let checker = evaluators[alarm.type],
  54. checker
  55. .shouldFire(
  56. alarm: alarm,
  57. data: data,
  58. now: now,
  59. config: Storage.shared.alarmConfiguration.value
  60. )
  61. else {
  62. // If this alarm is active, but no longer fulfill the requirements, stop it.
  63. // Continue evaluating other alarams
  64. if Observable.shared.currentAlarm.value == alarm.id {
  65. stopAlarm()
  66. }
  67. continue
  68. }
  69. // If this alarm is active, and still fulfill the requirements, let it be active
  70. // Break the loop, nothing else to do
  71. if Observable.shared.currentAlarm.value == alarm.id {
  72. break
  73. }
  74. // Fire the alarm and break the loop; we only allow one alarm per evaluation tick.
  75. Observable.shared.currentAlarm.value = alarm.id
  76. alarm.trigger(config: Storage.shared.alarmConfiguration.value, now: now)
  77. break
  78. }
  79. }
  80. func performSnooze(_ snoozeUnits: Int? = nil) {
  81. guard let alarmID = Observable.shared.currentAlarm.value else { return }
  82. var alarms = Storage.shared.alarms.value
  83. if let idx = alarms.firstIndex(where: { $0.id == alarmID }) {
  84. let alarm = alarms[idx]
  85. let units = snoozeUnits ?? alarm.snoozeDuration
  86. let snoozeSeconds = Double(units) * alarm.type.timeUnit.seconds
  87. alarms[idx].snoozedUntil = Date().addingTimeInterval(snoozeSeconds)
  88. Storage.shared.alarms.value = alarms
  89. stopAlarm()
  90. }
  91. }
  92. func stopAlarm() {
  93. AlarmSound.stop()
  94. Observable.shared.currentAlarm.value = nil
  95. UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
  96. }
  97. }