-
Notifications
You must be signed in to change notification settings - Fork 2
Battery not low constraint #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rteyssandier
merged 9 commits into
rteyssandier:main
from
dombroks:battery-not-low-constraint
Jan 13, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3ef92a7
feat: add BatteryNotLow constraint
dombroks 8551a0b
named argument for improved readability
dombroks cc7767e
Merge branch 'main' of https://github.com/dombroks/lorraine into batt…
dombroks d622091
Add requireBatteryNotLow to ConstraintEntity.kt and increase Room's v…
dombroks a593314
Upload schema json file after building
dombroks feca857
Use Logger instead of println instruction
dombroks d2ca844
Delete NoOpBatteryObserver
dombroks 84024e7
Merge branch 'main' into battery-not-low-constraint
dombroks 626c365
Update README.md with requireBatteryNotLow constraint
dombroks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| { | ||
| "formatVersion": 1, | ||
| "database": { | ||
| "version": 3, | ||
| "identityHash": "3aa46bbfd2b3c83f0efd26e20dc287f2", | ||
| "entities": [ | ||
| { | ||
| "tableName": "worker", | ||
| "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uuid` TEXT NOT NULL, `queue_id` TEXT NOT NULL, `identifier` TEXT NOT NULL, `state` TEXT NOT NULL, `tags` TEXT NOT NULL, `worker_dependencies` TEXT NOT NULL, `input_data` TEXT, `output_data` TEXT, `constraints_require_network` INTEGER NOT NULL, `constraints_require_battery_not_low` INTEGER NOT NULL, PRIMARY KEY(`uuid`))", | ||
| "fields": [ | ||
| { | ||
| "fieldPath": "uuid", | ||
| "columnName": "uuid", | ||
| "affinity": "TEXT", | ||
| "notNull": true | ||
| }, | ||
| { | ||
| "fieldPath": "queueId", | ||
| "columnName": "queue_id", | ||
| "affinity": "TEXT", | ||
| "notNull": true | ||
| }, | ||
| { | ||
| "fieldPath": "identifier", | ||
| "columnName": "identifier", | ||
| "affinity": "TEXT", | ||
| "notNull": true | ||
| }, | ||
| { | ||
| "fieldPath": "state", | ||
| "columnName": "state", | ||
| "affinity": "TEXT", | ||
| "notNull": true | ||
| }, | ||
| { | ||
| "fieldPath": "tags", | ||
| "columnName": "tags", | ||
| "affinity": "TEXT", | ||
| "notNull": true | ||
| }, | ||
| { | ||
| "fieldPath": "workerDependencies", | ||
| "columnName": "worker_dependencies", | ||
| "affinity": "TEXT", | ||
| "notNull": true | ||
| }, | ||
| { | ||
| "fieldPath": "inputData", | ||
| "columnName": "input_data", | ||
| "affinity": "TEXT" | ||
| }, | ||
| { | ||
| "fieldPath": "outputData", | ||
| "columnName": "output_data", | ||
| "affinity": "TEXT" | ||
| }, | ||
| { | ||
| "fieldPath": "constraints.requireNetwork", | ||
| "columnName": "constraints_require_network", | ||
| "affinity": "INTEGER", | ||
| "notNull": true | ||
| }, | ||
| { | ||
| "fieldPath": "constraints.requireBatteryNotLow", | ||
| "columnName": "constraints_require_battery_not_low", | ||
| "affinity": "INTEGER", | ||
| "notNull": true | ||
| } | ||
| ], | ||
| "primaryKey": { | ||
| "autoGenerate": false, | ||
| "columnNames": [ | ||
| "uuid" | ||
| ] | ||
| } | ||
| } | ||
| ], | ||
| "setupQueries": [ | ||
| "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", | ||
| "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '3aa46bbfd2b3c83f0efd26e20dc287f2')" | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 8 additions & 3 deletions
11
lorraine/src/commonMain/kotlin/io/dot/lorraine/dsl/LorraineConstraints.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,26 @@ | ||
| package io.dot.lorraine.dsl | ||
|
|
||
| data class LorraineConstraints internal constructor( | ||
| val requireNetwork: Boolean | ||
| val requireNetwork: Boolean, | ||
| val requireBatteryNotLow: Boolean, | ||
| ) { | ||
|
|
||
| companion object { | ||
| val NONE = LorraineConstraints( | ||
| requireNetwork = false | ||
| requireNetwork = false, | ||
| requireBatteryNotLow = false | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| class LorraineConstraintsDefinition internal constructor() { | ||
| var requiredNetwork: Boolean = false | ||
| var requiredBatteryNotLow: Boolean = false | ||
|
|
||
|
|
||
| fun build() = LorraineConstraints( | ||
| requireNetwork = requiredNetwork | ||
| requireNetwork = requiredNetwork, | ||
| requireBatteryNotLow = requiredBatteryNotLow | ||
| ) | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
lorraine/src/iosMain/kotlin/io/dot/lorraine/constraint/BatteryNotLowCheck.ios.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package io.dot.lorraine.constraint | ||
|
|
||
| import io.dot.lorraine.dsl.LorraineConstraints | ||
| import io.dot.lorraine.logger.Logger | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.onEach | ||
| import kotlinx.coroutines.flow.update | ||
| import kotlinx.coroutines.launch | ||
| import okio.Closeable | ||
| import platform.Foundation.NSNotificationCenter | ||
| import platform.Foundation.NSOperationQueue | ||
| import platform.UIKit.UIDevice | ||
| import platform.UIKit.UIDeviceBatteryLevelDidChangeNotification | ||
| import platform.UIKit.UIDeviceBatteryState | ||
| import platform.UIKit.UIDeviceBatteryStateDidChangeNotification | ||
|
|
||
| internal class BatteryNotLowCheck( | ||
| scope: CoroutineScope, | ||
| onChange: () -> Unit, | ||
| logger: Logger | ||
| ) : ConstraintCheck { | ||
|
|
||
| private val observer = AppleBatteryObserver() | ||
|
|
||
| private val _value = MutableStateFlow(false) | ||
|
|
||
| init { | ||
| observer.setListener( | ||
| object : BatteryObserver.Listener { | ||
| override fun batteryChanged(isNotLow: Boolean) { | ||
| _value.update { isNotLow } | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| scope.launch { | ||
| _value.onEach { logger.info("BatteryNotLowCheck: $it") }.collect { onChange() } | ||
| } | ||
| } | ||
|
|
||
| override suspend fun match(constraints: LorraineConstraints): Boolean { | ||
| if (!constraints.requireBatteryNotLow) | ||
| return true | ||
|
|
||
| return _value.value | ||
| } | ||
|
|
||
| } | ||
|
|
||
| internal class AppleBatteryObserver : BatteryObserver, Closeable { | ||
| private var listener: BatteryObserver.Listener? = null | ||
| private var levelObserver: Any? = null | ||
| private var stateObserver: Any? = null | ||
|
|
||
| override fun setListener(listener: BatteryObserver.Listener) { | ||
| this.listener = listener | ||
| UIDevice.currentDevice.batteryMonitoringEnabled = true | ||
|
|
||
| val center = NSNotificationCenter.defaultCenter | ||
|
|
||
| levelObserver = center.addObserverForName( | ||
| UIDeviceBatteryLevelDidChangeNotification, | ||
| null, | ||
| NSOperationQueue.mainQueue | ||
| ) { _ -> notifyListener() } | ||
|
|
||
| stateObserver = center.addObserverForName( | ||
| UIDeviceBatteryStateDidChangeNotification, | ||
| null, | ||
| NSOperationQueue.mainQueue | ||
| ) { _ -> notifyListener() } | ||
|
|
||
| // Notify initial state | ||
| notifyListener() | ||
| } | ||
|
|
||
| private fun notifyListener() { | ||
| listener?.batteryChanged(isBatteryNotLow()) | ||
| } | ||
|
|
||
| private fun isBatteryNotLow(): Boolean { | ||
| val device = UIDevice.currentDevice | ||
| val batteryLevel = device.batteryLevel | ||
| val batteryState = device.batteryState | ||
|
|
||
| // -1 means unknown (simulator), treat as not low | ||
| if (batteryLevel < 0) return true | ||
|
|
||
| // Battery is NOT low if: | ||
| // - level is above 15%, OR | ||
| // - device is charging, OR | ||
| // - device is full | ||
| return batteryLevel > 0.15f || | ||
| batteryState == UIDeviceBatteryState.UIDeviceBatteryStateCharging || | ||
| batteryState == UIDeviceBatteryState.UIDeviceBatteryStateFull | ||
| } | ||
|
|
||
| override fun close() { | ||
| levelObserver?.let { NSNotificationCenter.defaultCenter.removeObserver(it) } | ||
| stateObserver?.let { NSNotificationCenter.defaultCenter.removeObserver(it) } | ||
| UIDevice.currentDevice.batteryMonitoringEnabled = false | ||
| } | ||
| } | ||
|
|
||
| internal interface BatteryObserver : Closeable { | ||
| /** | ||
| * Sets the listener | ||
| * | ||
| * Implementation must call [listener] shortly after [setListener] returns to let the callers know about the initial state. | ||
| */ | ||
| fun setListener(listener: Listener) | ||
|
|
||
| interface Listener { | ||
| fun batteryChanged(isNotLow: Boolean) | ||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.