|
| 1 | +package io.dot.lorraine.constraint |
| 2 | + |
| 3 | +import io.dot.lorraine.dsl.LorraineConstraints |
| 4 | +import io.dot.lorraine.logger.Logger |
| 5 | +import kotlinx.coroutines.CoroutineScope |
| 6 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 7 | +import kotlinx.coroutines.flow.onEach |
| 8 | +import kotlinx.coroutines.flow.update |
| 9 | +import kotlinx.coroutines.launch |
| 10 | +import okio.Closeable |
| 11 | +import platform.Foundation.NSNotificationCenter |
| 12 | +import platform.Foundation.NSOperationQueue |
| 13 | +import platform.UIKit.UIDevice |
| 14 | +import platform.UIKit.UIDeviceBatteryLevelDidChangeNotification |
| 15 | +import platform.UIKit.UIDeviceBatteryState |
| 16 | +import platform.UIKit.UIDeviceBatteryStateDidChangeNotification |
| 17 | + |
| 18 | +internal class BatteryNotLowCheck( |
| 19 | + scope: CoroutineScope, |
| 20 | + onChange: () -> Unit, |
| 21 | + logger: Logger |
| 22 | +) : ConstraintCheck { |
| 23 | + |
| 24 | + private val observer = AppleBatteryObserver() |
| 25 | + |
| 26 | + private val _value = MutableStateFlow(false) |
| 27 | + |
| 28 | + init { |
| 29 | + observer.setListener( |
| 30 | + object : BatteryObserver.Listener { |
| 31 | + override fun batteryChanged(isNotLow: Boolean) { |
| 32 | + _value.update { isNotLow } |
| 33 | + } |
| 34 | + } |
| 35 | + ) |
| 36 | + |
| 37 | + scope.launch { |
| 38 | + _value.onEach { logger.info("BatteryNotLowCheck: $it") }.collect { onChange() } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + override suspend fun match(constraints: LorraineConstraints): Boolean { |
| 43 | + if (!constraints.requireBatteryNotLow) |
| 44 | + return true |
| 45 | + |
| 46 | + return _value.value |
| 47 | + } |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +internal class AppleBatteryObserver : BatteryObserver, Closeable { |
| 52 | + private var listener: BatteryObserver.Listener? = null |
| 53 | + private var levelObserver: Any? = null |
| 54 | + private var stateObserver: Any? = null |
| 55 | + |
| 56 | + override fun setListener(listener: BatteryObserver.Listener) { |
| 57 | + this.listener = listener |
| 58 | + UIDevice.currentDevice.batteryMonitoringEnabled = true |
| 59 | + |
| 60 | + val center = NSNotificationCenter.defaultCenter |
| 61 | + |
| 62 | + levelObserver = center.addObserverForName( |
| 63 | + UIDeviceBatteryLevelDidChangeNotification, |
| 64 | + null, |
| 65 | + NSOperationQueue.mainQueue |
| 66 | + ) { _ -> notifyListener() } |
| 67 | + |
| 68 | + stateObserver = center.addObserverForName( |
| 69 | + UIDeviceBatteryStateDidChangeNotification, |
| 70 | + null, |
| 71 | + NSOperationQueue.mainQueue |
| 72 | + ) { _ -> notifyListener() } |
| 73 | + |
| 74 | + // Notify initial state |
| 75 | + notifyListener() |
| 76 | + } |
| 77 | + |
| 78 | + private fun notifyListener() { |
| 79 | + listener?.batteryChanged(isBatteryNotLow()) |
| 80 | + } |
| 81 | + |
| 82 | + private fun isBatteryNotLow(): Boolean { |
| 83 | + val device = UIDevice.currentDevice |
| 84 | + val batteryLevel = device.batteryLevel |
| 85 | + val batteryState = device.batteryState |
| 86 | + |
| 87 | + // -1 means unknown (simulator), treat as not low |
| 88 | + if (batteryLevel < 0) return true |
| 89 | + |
| 90 | + // Battery is NOT low if: |
| 91 | + // - level is above 15%, OR |
| 92 | + // - device is charging, OR |
| 93 | + // - device is full |
| 94 | + return batteryLevel > 0.15f || |
| 95 | + batteryState == UIDeviceBatteryState.UIDeviceBatteryStateCharging || |
| 96 | + batteryState == UIDeviceBatteryState.UIDeviceBatteryStateFull |
| 97 | + } |
| 98 | + |
| 99 | + override fun close() { |
| 100 | + levelObserver?.let { NSNotificationCenter.defaultCenter.removeObserver(it) } |
| 101 | + stateObserver?.let { NSNotificationCenter.defaultCenter.removeObserver(it) } |
| 102 | + UIDevice.currentDevice.batteryMonitoringEnabled = false |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +internal interface BatteryObserver : Closeable { |
| 107 | + /** |
| 108 | + * Sets the listener |
| 109 | + * |
| 110 | + * Implementation must call [listener] shortly after [setListener] returns to let the callers know about the initial state. |
| 111 | + */ |
| 112 | + fun setListener(listener: Listener) |
| 113 | + |
| 114 | + interface Listener { |
| 115 | + fun batteryChanged(isNotLow: Boolean) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | + |
0 commit comments