AlarmType.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // AlarmType.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. /// Categorizes alarms into distinct types, prioritized in the order they appear here.
  10. /// Multiple user-defined alarms may share the same type but differ in configuration.
  11. enum AlarmType: String, CaseIterable, Codable {
  12. case iob = "IOB Alert"
  13. case bolus = "Bolus Alert"
  14. case cob = "COB Alert"
  15. case low = "Low BG Alert"
  16. case high = "High BG Alert"
  17. case fastDrop = "Fast Drop Alert"
  18. case fastRise = "Fast Rise Alert"
  19. case missedReading = "Missed Reading Alert"
  20. case notLooping = "Not Looping Alert"
  21. case missedBolus = "Missed Bolus Alert"
  22. case sensorChange = "Sensor Change Alert"
  23. case pumpChange = "Pump Change Alert"
  24. case pump = "Pump Insulin Alert"
  25. case battery = "Low Battery"
  26. case batteryDrop = "Battery Drop"
  27. case recBolus = "Rec. Bolus"
  28. case overrideStart = "Override Started"
  29. case overrideEnd = "Override Ended"
  30. case tempTargetStart = "Temp Target Started"
  31. case tempTargetEnd = "Temp Target Ended"
  32. case buildExpire = "Looping app expiration"
  33. }
  34. extension AlarmType {
  35. var priority: Int {
  36. return AlarmType.allCases.firstIndex(of: self) ?? 0
  37. }
  38. }
  39. extension AlarmType {
  40. /// What “unit” we use for snoozeDuration for this alarmType.
  41. var timeUnit: TimeUnit {
  42. switch self {
  43. case .buildExpire:
  44. return .day
  45. case .low, .high, .fastDrop, .fastRise,
  46. .missedReading, .notLooping, .missedBolus,
  47. .bolus, .recBolus,
  48. .overrideStart, .overrideEnd, .tempTargetStart,
  49. .tempTargetEnd:
  50. return .minute
  51. case .battery, .batteryDrop, .sensorChange, .pumpChange, .cob, .iob,
  52. .pump:
  53. return .hour
  54. }
  55. }
  56. }
  57. enum TimeUnit {
  58. case minute, hour, day
  59. /// How many seconds in one “unit”
  60. var seconds: TimeInterval {
  61. switch self {
  62. case .minute: return 60
  63. case .hour: return 60 * 60
  64. case .day: return 60 * 60 * 24
  65. }
  66. }
  67. /// A user-facing label
  68. var label: String {
  69. switch self {
  70. case .minute: return "min" // Changed from minutes to save ui space
  71. case .hour: return "hours"
  72. case .day: return "days"
  73. }
  74. }
  75. }
  76. extension AlarmType {
  77. /// `true` for alarms whose primary trigger is a blood-glucose value
  78. /// or its rate of change.
  79. var isBGBased: Bool {
  80. switch self {
  81. case .low, .high, .fastDrop, .fastRise, .missedReading:
  82. return true
  83. default:
  84. return false
  85. }
  86. }
  87. }