Add option to only show bars while riding

This commit is contained in:
Tim Kluge 2024-12-08 18:08:23 +01:00
parent dbaac0cec0
commit 792577ba69
5 changed files with 42 additions and 11 deletions

View File

@ -28,6 +28,7 @@ val settingsKey = stringPreferencesKey("settings")
data class PowerbarSettings( data class PowerbarSettings(
val source: SelectedSource = SelectedSource.POWER, val source: SelectedSource = SelectedSource.POWER,
val topBarSource: SelectedSource = SelectedSource.NONE, val topBarSource: SelectedSource = SelectedSource.NONE,
val onlyShowWhileRiding: Boolean = true
){ ){
companion object { companion object {
val defaultSettings = Json.encodeToString(PowerbarSettings()) val defaultSettings = Json.encodeToString(PowerbarSettings())

View File

@ -7,13 +7,20 @@ import android.app.Service
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.IBinder import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import de.timklge.karoopowerbar.KarooPowerbarExtension.Companion.TAG
import de.timklge.karoopowerbar.screens.SelectedSource import de.timklge.karoopowerbar.screens.SelectedSource
import io.hammerhead.karooext.KarooSystemService
import io.hammerhead.karooext.models.RideState
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
data class StreamState(val settings: PowerbarSettings, val showBars: Boolean)
class ForegroundService : Service() { class ForegroundService : Service() {
override fun onBind(intent: Intent?): IBinder { override fun onBind(intent: Intent?): IBinder {
throw UnsupportedOperationException("Not yet implemented") throw UnsupportedOperationException("Not yet implemented")
@ -26,12 +33,26 @@ class ForegroundService : Service() {
setupForeground() setupForeground()
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
applicationContext.streamSettings() val karooSystemService = KarooSystemService(applicationContext)
.collectLatest { settings -> karooSystemService.connect { connected ->
Log.i(TAG, "Karoo system service connected: $connected")
}
val rideStateFlow = karooSystemService.streamRideState()
applicationContext
.streamSettings()
.combine(rideStateFlow) { settings, rideState ->
val showBars = if (settings.onlyShowWhileRiding){
rideState is RideState.Recording
} else {
true
}
StreamState(settings, showBars)
}.collectLatest { (settings, showBars) ->
windows.forEach { it.close() } windows.forEach { it.close() }
windows.clear() windows.clear()
if (settings.source != SelectedSource.NONE) { if (settings.source != SelectedSource.NONE && showBars) {
Window(this@ForegroundService, PowerbarLocation.BOTTOM).apply { Window(this@ForegroundService, PowerbarLocation.BOTTOM).apply {
selectedSource = settings.source selectedSource = settings.source
windows.add(this) windows.add(this)
@ -39,7 +60,7 @@ class ForegroundService : Service() {
} }
} }
if (settings.topBarSource != SelectedSource.NONE){ if (settings.topBarSource != SelectedSource.NONE && showBars){
Window(this@ForegroundService, PowerbarLocation.TOP).apply { Window(this@ForegroundService, PowerbarLocation.TOP).apply {
selectedSource = settings.topBarSource selectedSource = settings.topBarSource
open() open()

View File

@ -31,9 +31,7 @@ class KarooPowerbarExtension : KarooExtension("karoo-powerbar", "1.0.1") {
serviceJob = CoroutineScope(Dispatchers.IO).launch { serviceJob = CoroutineScope(Dispatchers.IO).launch {
karooSystem.connect { connected -> karooSystem.connect { connected ->
if (connected) { Log.i(TAG, "Karoo system service connected: $connected")
Log.i(TAG, "Connected")
}
} }
} }
} }

View File

@ -12,6 +12,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.WindowInsets import android.view.WindowInsets
import android.view.WindowManager import android.view.WindowManager
import de.timklge.karoopowerbar.KarooPowerbarExtension.Companion.TAG
import de.timklge.karoopowerbar.screens.SelectedSource import de.timklge.karoopowerbar.screens.SelectedSource
import io.hammerhead.karooext.KarooSystemService import io.hammerhead.karooext.KarooSystemService
import io.hammerhead.karooext.models.DataType import io.hammerhead.karooext.models.DataType
@ -98,9 +99,7 @@ class Window(
suspend fun open() { suspend fun open() {
serviceJob = CoroutineScope(Dispatchers.Default).launch { serviceJob = CoroutineScope(Dispatchers.Default).launch {
karooSystem.connect { connected -> karooSystem.connect { connected ->
if (connected) { Log.i(TAG, "Karoo system service connected: $connected")
Log.i(KarooPowerbarExtension.TAG, "Connected")
}
} }
powerbar.progressColor = context.resources.getColor(R.color.zoneAerobic) powerbar.progressColor = context.resources.getColor(R.color.zoneAerobic)

View File

@ -5,6 +5,7 @@ import android.provider.Settings
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
@ -22,6 +23,7 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilledTonalButton import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Icon import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Switch
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
@ -31,6 +33,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -66,12 +69,15 @@ fun MainScreen() {
var showAlerts by remember { mutableStateOf(false) } var showAlerts by remember { mutableStateOf(false) }
var givenPermissions by remember { mutableStateOf(false) } var givenPermissions by remember { mutableStateOf(false) }
var onlyShowWhileRiding by remember { mutableStateOf(false) }
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
givenPermissions = Settings.canDrawOverlays(ctx) givenPermissions = Settings.canDrawOverlays(ctx)
ctx.streamSettings().collect { settings -> ctx.streamSettings().collect { settings ->
bottomSelectedSource = settings.source bottomSelectedSource = settings.source
topSelectedSource = settings.topBarSource topSelectedSource = settings.topBarSource
onlyShowWhileRiding = settings.onlyShowWhileRiding
} }
} }
@ -122,10 +128,16 @@ fun MainScreen() {
} }
} }
Row(verticalAlignment = Alignment.CenterVertically) {
Switch(checked = onlyShowWhileRiding, onCheckedChange = { onlyShowWhileRiding = it})
Spacer(modifier = Modifier.width(10.dp))
Text("Only show while riding")
}
FilledTonalButton(modifier = Modifier FilledTonalButton(modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp), onClick = { .height(50.dp), onClick = {
val newSettings = PowerbarSettings(source = bottomSelectedSource, topBarSource = topSelectedSource) val newSettings = PowerbarSettings(source = bottomSelectedSource, topBarSource = topSelectedSource, onlyShowWhileRiding = onlyShowWhileRiding)
coroutineScope.launch { coroutineScope.launch {
saveSettings(ctx, newSettings) saveSettings(ctx, newSettings)