-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add charging constraint to LorraineConstraints #37
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
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
74caa5c
feat: Add charging constraint to LorraineConstraints
dombroks 3063444
Update ConstraintEntity.kt and increase database version
dombroks c7cefbf
Update README.md to include requireCharging in constraints
dombroks ee8362d
Integrate migration and set exportSchema to true
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
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
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
97 changes: 97 additions & 0 deletions
97
lorraine/src/iosMain/kotlin/io/dot/lorraine/constraint/ChargingCheck.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,97 @@ | ||
| package io.dot.lorraine.constraint | ||
|
|
||
| import io.dot.lorraine.dsl.LorraineConstraints | ||
| import io.dot.lorraine.logger.LorraineLogger | ||
| 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.UIDeviceBatteryState | ||
| import platform.UIKit.UIDeviceBatteryStateDidChangeNotification | ||
|
|
||
| internal class ChargingCheck( | ||
| scope: CoroutineScope, | ||
| onChange: () -> Unit, | ||
| logger: LorraineLogger | ||
| ) : ConstraintCheck { | ||
|
|
||
| private val observer = AppleChargingObserver() | ||
|
|
||
| private val _value = MutableStateFlow(false) | ||
|
|
||
| init { | ||
| observer.setListener( | ||
| object : ChargingObserver.Listener { | ||
| override fun chargingChanged(isCharging: Boolean) { | ||
| _value.update { isCharging } | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| scope.launch { | ||
| _value.onEach { logger.info("ChargingCheck: $it") }.collect { onChange() } | ||
| } | ||
| } | ||
|
|
||
| override suspend fun match(constraints: LorraineConstraints): Boolean { | ||
| if (!constraints.requireCharging) | ||
| return true | ||
|
|
||
| return _value.value | ||
| } | ||
|
|
||
| } | ||
|
|
||
| internal class AppleChargingObserver : ChargingObserver, Closeable { | ||
| private var listener: ChargingObserver.Listener? = null | ||
| private var stateObserver: Any? = null | ||
|
|
||
| override fun setListener(listener: ChargingObserver.Listener) { | ||
| this.listener = listener | ||
| UIDevice.currentDevice.batteryMonitoringEnabled = true | ||
|
|
||
| val center = NSNotificationCenter.defaultCenter | ||
|
|
||
| stateObserver = center.addObserverForName( | ||
| UIDeviceBatteryStateDidChangeNotification, | ||
| null, | ||
| NSOperationQueue.mainQueue | ||
| ) { _ -> notifyListener() } | ||
|
|
||
| notifyListener() | ||
| } | ||
|
|
||
| private fun notifyListener() { | ||
| listener?.chargingChanged(isCharging()) | ||
| } | ||
|
|
||
| private fun isCharging(): Boolean { | ||
| val device = UIDevice.currentDevice | ||
| val batteryState = device.batteryState | ||
|
|
||
| return batteryState == UIDeviceBatteryState.UIDeviceBatteryStateCharging | ||
| } | ||
|
|
||
| override fun close() { | ||
| stateObserver?.let { NSNotificationCenter.defaultCenter.removeObserver(it) } | ||
| UIDevice.currentDevice.batteryMonitoringEnabled = false | ||
| } | ||
| } | ||
|
|
||
| internal interface ChargingObserver : 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 chargingChanged(isCharging: 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.