fix #5: Show empty bar if sensor becomes unavailable

This commit is contained in:
Tim Kluge 2024-12-10 20:18:39 +01:00
parent b3547f6051
commit 2cf5ce70d6

View File

@ -94,7 +94,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?)
private var serviceJob: Job? = null
@ -134,7 +134,7 @@ class Window(
private suspend fun streamHeartrate() {
val powerFlow = karooSystem.streamDataFlow(DataType.Type.HEART_RATE)
.mapNotNull { (it as? StreamState.Streaming)?.dataPoint?.singleValue }
.map { (it as? StreamState.Streaming)?.dataPoint?.singleValue }
.distinctUntilChanged()
karooSystem.streamUserProfile()
@ -143,7 +143,9 @@ class Window(
.map { (userProfile, hr) -> StreamData(userProfile, hr) }
.distinctUntilChanged()
.collect { streamData ->
val value = streamData.value.roundToInt()
val value = streamData.value?.roundToInt()
if (value != null) {
val color = context.getColor(
getZone(streamData.userProfile.heartRateZones, value)?.colorResource
?: R.color.zone7
@ -156,9 +158,16 @@ class Window(
powerbar.progressColor = color
powerbar.progress = progress
powerbar.label = "$value"
powerbar.invalidate()
Log.d(TAG, "Hr: $value min: $minHr max: $maxHr")
} else {
powerbar.progressColor = context.getColor(R.color.zone0)
powerbar.progress = 0.0
powerbar.label = "?"
Log.d(TAG, "Hr: Unavailable")
}
powerbar.invalidate()
}
}
@ -170,7 +179,7 @@ class Window(
private suspend fun streamPower(smoothed: PowerStreamSmoothing) {
val powerFlow = karooSystem.streamDataFlow(smoothed.dataTypeId)
.mapNotNull { (it as? StreamState.Streaming)?.dataPoint?.singleValue }
.map { (it as? StreamState.Streaming)?.dataPoint?.singleValue }
.distinctUntilChanged()
karooSystem.streamUserProfile()
@ -179,7 +188,9 @@ class Window(
.map { (userProfile, power) -> StreamData(userProfile, power) }
.distinctUntilChanged()
.collect { streamData ->
val value = streamData.value.roundToInt()
val value = streamData.value?.roundToInt()
if (value != null) {
val color = context.getColor(
getZone(streamData.userProfile.powerZones, value)?.colorResource
?: R.color.zone7
@ -192,9 +203,16 @@ class Window(
powerbar.progressColor = color
powerbar.progress = progress
powerbar.label = "${value}W"
powerbar.invalidate()
Log.d(TAG, "Power: $value min: $minPower max: $maxPower")
} else {
powerbar.progressColor = context.getColor(R.color.zone0)
powerbar.progress = 0.0
powerbar.label = "?"
Log.d(TAG, "Power: Unavailable")
}
powerbar.invalidate()
}
}