Skip to content

Commit

Permalink
Fix old event clean up logic
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rken committed Sep 12, 2024
1 parent 95f3ca2 commit 3d632d2
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,22 @@ class PowerAlertManager @Inject constructor(
suspend fun setBatteryLowAlert(deviceId: DeviceId, threshold: Float?): Unit = mutex.withLock {
log(TAG) { "setBatteryLowAlert($deviceId,$threshold)" }

val newRule = powerSettings.alertRules.update { oldRules ->
var newRule: BatteryLowAlertRule? = null

powerSettings.alertRules.update { oldRules ->
val otherRules = oldRules.filterNot { it.deviceId == deviceId && it is BatteryLowAlertRule }

if (threshold == null) {
otherRules
} else {
otherRules + BatteryLowAlertRule(deviceId = deviceId, threshold = threshold)
otherRules + BatteryLowAlertRule(deviceId = deviceId, threshold = threshold).also {
newRule = it
}
}.toSet()
}.new.filterIsInstance<BatteryLowAlertRule>().single()
}

powerSettings.alertEvents.update { old ->
val previousEvent = old.singleOrNull { it.id == newRule.id } ?: return@update old
val previousEvent = old.singleOrNull { it.id == newRule?.id } ?: return@update old
log(TAG) { "setBatteryLowAlert(...): Removing old event" }
(old - previousEvent).toSet()
}
Expand Down

0 comments on commit 3d632d2

Please sign in to comment.