fix #9: Add option to set bar to fixed color

This commit is contained in:
Tim Kluge 2024-12-19 17:47:59 +01:00
parent 2477748ac4
commit f5e0491df8
6 changed files with 38 additions and 24 deletions

View File

@ -15,8 +15,8 @@ android {
applicationId = "de.timklge.karoopowerbar"
minSdk = 26
targetSdk = 33
versionCode = 7
versionName = "1.2.2"
versionCode = 8
versionName = "1.2.3"
}
signingConfigs {

View File

@ -3,9 +3,9 @@
"packageName": "de.timklge.karoopowerbar",
"iconUrl": "https://github.com/timklge/karoo-powerbar/releases/latest/download/karoo-powerbar.png",
"latestApkUrl": "https://github.com/timklge/karoo-powerbar/releases/latest/download/app-release.apk",
"latestVersion": "1.2.2",
"latestVersionCode": 7,
"latestVersion": "1.2.3",
"latestVersionCode": 8,
"developer": "timklge",
"description": "Adds a colored power bar to the bottom of the screen",
"releaseNotes": "Slightly adjusts bar appearance. First build with new CI/CD pipeline."
"releaseNotes": "Add option to set bar to single color"
}

View File

@ -29,7 +29,8 @@ data class PowerbarSettings(
val source: SelectedSource = SelectedSource.POWER,
val topBarSource: SelectedSource = SelectedSource.NONE,
val onlyShowWhileRiding: Boolean = true,
val showLabelOnBars: Boolean = true
val showLabelOnBars: Boolean = true,
val useZoneColors: Boolean = true,
){
companion object {
val defaultSettings = Json.encodeToString(PowerbarSettings())

View File

@ -10,7 +10,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
class KarooPowerbarExtension : KarooExtension("karoo-powerbar", "1.2.2") {
class KarooPowerbarExtension : KarooExtension("karoo-powerbar", "1.2.3") {
companion object {
const val TAG = "karoo-powerbar"

View File

@ -98,7 +98,7 @@ class Window(
private val karooSystem: KarooSystemService = KarooSystemService(context)
data class StreamData(val userProfile: UserProfile, val value: Double?)
data class StreamData(val userProfile: UserProfile, val value: Double?, val settings: PowerbarSettings? = null)
private var serviceJob: Job? = null
@ -145,29 +145,31 @@ class Window(
}
private suspend fun streamHeartrate() {
val powerFlow = karooSystem.streamDataFlow(DataType.Type.HEART_RATE)
val hrFlow = karooSystem.streamDataFlow(DataType.Type.HEART_RATE)
.map { (it as? StreamState.Streaming)?.dataPoint?.singleValue }
.distinctUntilChanged()
val settingsFlow = context.streamSettings()
karooSystem.streamUserProfile()
.distinctUntilChanged()
.combine(powerFlow) { userProfile, hr -> userProfile to hr }
.map { (userProfile, hr) -> StreamData(userProfile, hr) }
.combine(hrFlow) { userProfile, hr -> StreamData(userProfile, hr) }
.combine(settingsFlow) { streamData, settings -> streamData.copy(settings = settings) }
.distinctUntilChanged()
.collect { streamData ->
val value = streamData.value?.roundToInt()
if (value != null) {
val color = context.getColor(
getZone(streamData.userProfile.heartRateZones, value)?.colorResource
?: R.color.zone7
)
val minHr = streamData.userProfile.restingHr
val maxHr = streamData.userProfile.maxHr
val progress =
remap(value.toDouble(), minHr.toDouble(), maxHr.toDouble(), 0.0, 1.0)
powerbar.progressColor = color
powerbar.progressColor = if (streamData.settings?.useZoneColors == true) {
context.getColor(getZone(streamData.userProfile.heartRateZones, value)?.colorResource ?: R.color.zone7)
} else {
context.getColor(R.color.zone0)
}
powerbar.progress = progress
powerbar.label = "$value"
@ -194,25 +196,27 @@ class Window(
.map { (it as? StreamState.Streaming)?.dataPoint?.singleValue }
.distinctUntilChanged()
val settingsFlow = context.streamSettings()
karooSystem.streamUserProfile()
.distinctUntilChanged()
.combine(powerFlow) { userProfile, power -> userProfile to power }
.map { (userProfile, power) -> StreamData(userProfile, power) }
.combine(powerFlow) { userProfile, hr -> StreamData(userProfile, hr) }
.combine(settingsFlow) { streamData, settings -> streamData.copy(settings = settings) }
.distinctUntilChanged()
.collect { streamData ->
val value = streamData.value?.roundToInt()
if (value != null) {
val color = context.getColor(
getZone(streamData.userProfile.powerZones, value)?.colorResource
?: R.color.zone7
)
val minPower = streamData.userProfile.powerZones.first().min
val maxPower = streamData.userProfile.powerZones.last().min + 50
val progress =
remap(value.toDouble(), minPower.toDouble(), maxPower.toDouble(), 0.0, 1.0)
powerbar.progressColor = color
powerbar.progressColor = if (streamData.settings?.useZoneColors == true) {
context.getColor(getZone(streamData.userProfile.powerZones, value)?.colorResource ?: R.color.zone7)
} else {
context.getColor(R.color.zone0)
}
powerbar.progress = progress
powerbar.label = "${value}W"

View File

@ -70,6 +70,7 @@ fun MainScreen() {
var givenPermissions by remember { mutableStateOf(false) }
var onlyShowWhileRiding by remember { mutableStateOf(false) }
var colorBasedOnZones by remember { mutableStateOf(false) }
var showLabelOnBars by remember { mutableStateOf(true) }
LaunchedEffect(Unit) {
@ -80,6 +81,7 @@ fun MainScreen() {
topSelectedSource = settings.topBarSource
onlyShowWhileRiding = settings.onlyShowWhileRiding
showLabelOnBars = settings.showLabelOnBars
colorBasedOnZones = settings.useZoneColors
}
}
@ -130,6 +132,12 @@ fun MainScreen() {
}
}
Row(verticalAlignment = Alignment.CenterVertically) {
Switch(checked = colorBasedOnZones, onCheckedChange = { colorBasedOnZones = it})
Spacer(modifier = Modifier.width(10.dp))
Text("Color based on HR / power zones")
}
Row(verticalAlignment = Alignment.CenterVertically) {
Switch(checked = showLabelOnBars, onCheckedChange = { showLabelOnBars = it})
Spacer(modifier = Modifier.width(10.dp))
@ -147,7 +155,8 @@ fun MainScreen() {
.height(50.dp), onClick = {
val newSettings = PowerbarSettings(
source = bottomSelectedSource, topBarSource = topSelectedSource,
onlyShowWhileRiding = onlyShowWhileRiding, showLabelOnBars = showLabelOnBars
onlyShowWhileRiding = onlyShowWhileRiding, showLabelOnBars = showLabelOnBars,
useZoneColors = colorBasedOnZones
)
coroutineScope.launch {