RecBolusCondition.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // LoopFollow
  2. // RecBolusCondition.swift
  3. import Foundation
  4. /// Fires once when the recommended bolus (units) is ≥ the user-set threshold.
  5. struct RecBolusCondition: AlarmCondition {
  6. static let type: AlarmType = .recBolus
  7. init() {}
  8. func evaluate(alarm: Alarm, data: AlarmData, now _: Date) -> Bool {
  9. // ────────────────────────────────
  10. // Reset alarm if below threshold
  11. // ────────────────────────────────
  12. guard let threshold = alarm.threshold, threshold > 0 else { return false }
  13. guard let rec = data.recBolus, rec >= threshold else {
  14. Storage.shared.lastRecBolusNotified.value = nil
  15. return false
  16. }
  17. // ────────────────────────────────
  18. // Check if we should alert (increase or first time)
  19. // ────────────────────────────────
  20. let shouldAlert: Bool
  21. if let last = Storage.shared.lastRecBolusNotified.value {
  22. // Only alert if there's been more than 5% increase
  23. shouldAlert = rec > last * 1.05
  24. } else {
  25. // First time above threshold - alert
  26. shouldAlert = true
  27. }
  28. // Always update the stored value when above threshold
  29. Storage.shared.lastRecBolusNotified.value = rec
  30. return shouldAlert
  31. }
  32. }