-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add battery level alerts logic and UI to config a threshold
(worker and notification still missing)
- Loading branch information
Showing
33 changed files
with
596 additions
and
32 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
app-common/src/main/java/eu/darken/octi/common/TextViewExtensions.kt
This file contains 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,10 @@ | ||
package eu.darken.octi.common | ||
|
||
import android.graphics.Typeface | ||
import android.widget.TextView | ||
|
||
var TextView.isBold: Boolean | ||
get() = typeface.isBold | ||
set(value) { | ||
setTypeface(null, if (value) Typeface.BOLD else Typeface.NORMAL) | ||
} |
14 changes: 14 additions & 0 deletions
14
app-common/src/main/java/eu/darken/octi/common/flow/throttleLatest.kt
This file contains 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,14 @@ | ||
package eu.darken.octi.common.flow | ||
|
||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.conflate | ||
import kotlinx.coroutines.flow.transform | ||
|
||
|
||
fun <T> Flow<T>.throttleLatest(delayMillis: Long): Flow<T> = this | ||
.conflate() | ||
.transform { | ||
emit(it) | ||
delay(delayMillis) | ||
} |
This file contains 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
2 changes: 1 addition & 1 deletion
2
app/src/main/java/eu/darken/octi/modules/power/ui/PowerInfoExtensions.kt
This file contains 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
3 changes: 3 additions & 0 deletions
3
app/src/main/java/eu/darken/octi/modules/power/ui/alerts/PowerAlertsAction.kt
This file contains 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,3 @@ | ||
package eu.darken.octi.modules.power.ui.alerts | ||
|
||
sealed interface PowerAlertsAction |
68 changes: 68 additions & 0 deletions
68
app/src/main/java/eu/darken/octi/modules/power/ui/alerts/PowerAlertsFragment.kt
This file contains 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,68 @@ | ||
package eu.darken.octi.modules.power.ui.alerts | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.fragment.app.viewModels | ||
import androidx.navigation.fragment.findNavController | ||
import androidx.navigation.ui.setupWithNavController | ||
import com.google.android.material.slider.Slider | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import eu.darken.octi.R | ||
import eu.darken.octi.common.isBold | ||
import eu.darken.octi.common.observe2 | ||
import eu.darken.octi.common.uix.Fragment3 | ||
import eu.darken.octi.common.viewbinding.viewBinding | ||
import eu.darken.octi.databinding.ModulePowerAlertsFragmentBinding | ||
|
||
|
||
@AndroidEntryPoint | ||
class PowerAlertsFragment : Fragment3(R.layout.module_power_alerts_fragment) { | ||
|
||
override val vm: PowerAlertsVM by viewModels() | ||
override val ui: ModulePowerAlertsFragmentBinding by viewBinding() | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
ui.toolbar.apply { | ||
setupWithNavController(findNavController()) | ||
setOnMenuItemClickListener { | ||
when (it.itemId) { | ||
else -> super.onOptionsItemSelected(it) | ||
} | ||
} | ||
} | ||
|
||
ui.lowbatteryThresholdSlider.apply { | ||
addOnChangeListener { _, value, _ -> | ||
ui.lowbatteryThresholdSliderCaption.text = when (value) { | ||
0f -> getString(R.string.module_power_alerts_lowbattery_disabled_caption) | ||
else -> getString( | ||
R.string.module_power_alerts_lowbattery_slider_value_caption, | ||
"${(value * 100).toInt()}%" | ||
) | ||
} | ||
} | ||
addOnSliderTouchListener(object : Slider.OnSliderTouchListener { | ||
override fun onStartTrackingTouch(slider: Slider) {} | ||
|
||
override fun onStopTrackingTouch(slider: Slider) { | ||
vm.setBatteryLowAlert(slider.value) | ||
} | ||
}) | ||
stepSize = 0.05f | ||
valueFrom = 0f | ||
valueTo = 0.95f | ||
} | ||
|
||
vm.state.observe2(this@PowerAlertsFragment, ui) { state -> | ||
ui.toolbar.subtitle = getString(R.string.device_x_label, state.deviceLabel) | ||
lowbatteryThresholdSlider.value = state.batteryLowAlert?.threshold ?: 0f | ||
lowbatteryTitle.isBold = state.batteryLowAlert != null | ||
} | ||
|
||
vm.events.observe2 { event -> | ||
|
||
} | ||
|
||
super.onViewCreated(view, savedInstanceState) | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
app/src/main/java/eu/darken/octi/modules/power/ui/alerts/PowerAlertsVM.kt
This file contains 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,70 @@ | ||
package eu.darken.octi.modules.power.ui.alerts | ||
|
||
import android.annotation.SuppressLint | ||
import androidx.lifecycle.SavedStateHandle | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import eu.darken.octi.common.coroutine.DispatcherProvider | ||
import eu.darken.octi.common.debug.logging.Logging.Priority.ERROR | ||
import eu.darken.octi.common.debug.logging.log | ||
import eu.darken.octi.common.debug.logging.logTag | ||
import eu.darken.octi.common.livedata.SingleLiveEvent | ||
import eu.darken.octi.common.navigation.navArgs | ||
import eu.darken.octi.common.uix.ViewModel3 | ||
import eu.darken.octi.modules.meta.core.MetaRepo | ||
import eu.darken.octi.modules.power.core.alerts.BatteryLowAlert | ||
import eu.darken.octi.modules.power.core.alerts.PowerAlertManager | ||
import kotlinx.coroutines.flow.combine | ||
import kotlinx.coroutines.flow.map | ||
import java.util.Locale | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
@SuppressLint("StaticFieldLeak") | ||
class PowerAlertsVM @Inject constructor( | ||
handle: SavedStateHandle, | ||
dispatcherProvider: DispatcherProvider, | ||
metaRepo: MetaRepo, | ||
private val alertsManager: PowerAlertManager, | ||
) : ViewModel3(dispatcherProvider = dispatcherProvider) { | ||
|
||
private val navArgs: PowerAlertsFragmentArgs by handle.navArgs() | ||
|
||
val events = SingleLiveEvent<PowerAlertsAction>() | ||
|
||
init { | ||
launch { alertsManager.dismissBatteryLowAlert(navArgs.deviceId) } | ||
} | ||
|
||
data class State( | ||
val deviceLabel: String = "", | ||
val batteryLowAlert: BatteryLowAlert? = null, | ||
) | ||
|
||
val state = combine( | ||
alertsManager.alerts.map { alerts -> alerts.filter { it.deviceId == navArgs.deviceId } }, | ||
metaRepo.state, | ||
) { alerts, metaState -> | ||
val metaData = metaState.all.firstOrNull { it.deviceId == navArgs.deviceId } | ||
|
||
if (metaData == null) { | ||
log(TAG, ERROR) { "No meta data found for ${navArgs.deviceId}" } | ||
popNavStack() | ||
return@combine State() | ||
} | ||
|
||
State( | ||
deviceLabel = metaData.data.deviceLabel ?: metaData.data.deviceName, | ||
batteryLowAlert = alerts.find { it is BatteryLowAlert } as BatteryLowAlert? | ||
) | ||
}.asLiveData2() | ||
|
||
fun setBatteryLowAlert(threshold: Float) = launch { | ||
log(TAG) { "setBatteryLowAlert($threshold)" } | ||
val cleanThreshold = String.format(Locale.ROOT, "%.2f", threshold.coerceIn(0f, 95f)).toFloat() | ||
alertsManager.setBatteryLowAlert(navArgs.deviceId, cleanThreshold.takeIf { it > 0f }) | ||
} | ||
|
||
companion object { | ||
private val TAG = logTag("Module", "Power", "Alerts", "Fragment", "VM") | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.