FastDropCondition.swift 1.4 KB

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