BatteryCondition.swift 594 B

12345678910111213141516171819202122
  1. // LoopFollow
  2. // BatteryCondition.swift
  3. import Foundation
  4. struct BatteryCondition: AlarmCondition {
  5. static let type: AlarmType = .battery
  6. init() {}
  7. func evaluate(alarm: Alarm, data: AlarmData, now _: Date) -> Bool {
  8. guard let limit = alarm.threshold, limit > 0 else { return false }
  9. guard let level = data.latestBattery else { return false }
  10. // Optional: stay silent while the phone is charging back up.
  11. if alarm.suppressIfCharging, data.latestBatteryIsCharging == true {
  12. return false
  13. }
  14. return level <= limit
  15. }
  16. }