FastDropCondition.swift 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // LoopFollow
  2. // FastDropCondition.swift
  3. // Created by Jonas Björkert on 2025-05-14.
  4. import Foundation
  5. struct FastDropCondition: AlarmCondition {
  6. static let type: AlarmType = .fastDrop
  7. init() {}
  8. func evaluate(alarm: Alarm, data: AlarmData) -> Bool {
  9. // ────────────────────────────────
  10. // 0. sanity checks
  11. // ────────────────────────────────
  12. guard
  13. let dropPerReading = alarm.delta, dropPerReading > 0,
  14. let dropsNeeded = alarm.monitoringWindow, dropsNeeded > 0,
  15. data.bgReadings.count >= dropsNeeded + 1
  16. else { return false }
  17. // ────────────────────────────────
  18. // 1. compute recent deltas
  19. // (BG-limit check is now handled by passesBGLimits)
  20. // ────────────────────────────────
  21. let recent = data.bgReadings.suffix(dropsNeeded + 1)
  22. let readings = Array(recent)
  23. for i in 1 ... dropsNeeded {
  24. let delta = Double(readings[i - 1].sgv - readings[i].sgv)
  25. if delta < dropPerReading { return false }
  26. }
  27. return true
  28. }
  29. }