RecBolusCondition.swift 1.6 KB

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