AlarmType.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /// Should alarms of this type sort their thresholds ascending (true) or descending (false)
  41. var thresholdSortAscending: Bool? {
  42. switch self {
  43. case .low, .fastDrop, .fastRise, .missedReading, .notLooping, .missedBolus, .buildExpire:
  44. return true
  45. case .high, .iob, .cob:
  46. return false
  47. default:
  48. return nil
  49. }
  50. }
  51. }
  52. extension AlarmType {
  53. /// What “unit” we use for snoozeDuration for this alarmType.
  54. var timeUnit: TimeUnit {
  55. switch self {
  56. case .buildExpire:
  57. return .day
  58. case .low, .high, .fastDrop, .fastRise,
  59. .missedReading, .notLooping, .missedBolus,
  60. .bolus, .recBolus,
  61. .overrideStart, .overrideEnd, .tempTargetStart,
  62. .tempTargetEnd:
  63. return .minute
  64. case .battery, .batteryDrop, .sensorChange, .pumpChange, .cob, .iob,
  65. .pump:
  66. return .hour
  67. }
  68. }
  69. }
  70. enum TimeUnit {
  71. case minute, hour, day
  72. /// How many seconds in one “unit”
  73. var seconds: TimeInterval {
  74. switch self {
  75. case .minute: return 60
  76. case .hour: return 60 * 60
  77. case .day: return 60 * 60 * 24
  78. }
  79. }
  80. /// A user-facing label
  81. var label: String {
  82. switch self {
  83. case .minute: return "min" // Changed from minutes to save ui space
  84. case .hour: return "hours"
  85. case .day: return "days"
  86. }
  87. }
  88. }
  89. extension AlarmType {
  90. /// `true` for alarms whose primary trigger is a blood-glucose value
  91. /// or its rate of change.
  92. var isBGBased: Bool {
  93. switch self {
  94. case .low, .high, .fastDrop, .fastRise, .missedReading:
  95. return true
  96. default:
  97. return false
  98. }
  99. }
  100. }