Add status label to main activity that indicates background service status for debugging (#66)

This commit is contained in:
timklge 2025-03-08 13:33:21 +01:00 committed by GitHub
parent 107c7a575d
commit ab8d7bec3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 2 deletions

View File

@ -94,6 +94,8 @@ class KarooHeadwindExtension : KarooExtension("karoo-headwind", BuildConfig.VERS
super.onCreate()
karooSystem = KarooSystemService(applicationContext)
ServiceStatusSingleton.getInstance().setServiceStatus(true)
updateLastKnownGpsJob = CoroutineScope(Dispatchers.IO).launch {
karooSystem.updateLastKnownGps(this@KarooHeadwindExtension)

View File

@ -0,0 +1,28 @@
package de.timklge.karooheadwind
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
class ServiceStatusSingleton private constructor() {
companion object {
private var instance: ServiceStatusSingleton? = null
@Synchronized
fun getInstance(): ServiceStatusSingleton {
if (instance == null) {
instance = ServiceStatusSingleton()
}
return instance as ServiceStatusSingleton
}
}
private val serviceStatus: MutableStateFlow<Boolean> = MutableStateFlow(false)
fun getServiceStatus(): StateFlow<Boolean> {
return serviceStatus
}
fun setServiceStatus(status: Boolean) {
serviceStatus.value = status
}
}

View File

@ -27,11 +27,11 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import de.timklge.karooheadwind.HeadwindStats
import de.timklge.karooheadwind.KarooHeadwindExtension
import de.timklge.karooheadwind.R
import de.timklge.karooheadwind.ServiceStatusSingleton
import de.timklge.karooheadwind.TemperatureUnit
import de.timklge.karooheadwind.WeatherInterpretation
import de.timklge.karooheadwind.datatypes.ForecastDataType
import de.timklge.karooheadwind.datatypes.WeatherDataType.Companion.timeFormatter
import de.timklge.karooheadwind.datatypes.WeatherForecastDataType
import de.timklge.karooheadwind.datatypes.getShortDateFormatter
import de.timklge.karooheadwind.getGpsCoordinateFlow
import de.timklge.karooheadwind.streamCurrentWeatherData
@ -113,7 +113,14 @@ fun WeatherScreen(onFinish: () -> Unit) {
val lastPositionDistanceStr =
lastPosition?.let { dist -> " (${dist.roundToInt()} km away)" } ?: ""
if (stats.failedWeatherRequest != null && (stats.lastSuccessfulWeatherRequest == null || stats.failedWeatherRequest!! > stats.lastSuccessfulWeatherRequest!!)) {
val serviceStatus by ServiceStatusSingleton.getInstance().getServiceStatus().collectAsStateWithLifecycle(false)
if (!serviceStatus){
Text(
modifier = Modifier.padding(5.dp),
text = "Attempting to connect to weather background service..."
)
} else if (stats.failedWeatherRequest != null && (stats.lastSuccessfulWeatherRequest == null || stats.failedWeatherRequest!! > stats.lastSuccessfulWeatherRequest!!)) {
val successfulTime = LocalDateTime.ofInstant(
Instant.ofEpochMilli(
stats.lastSuccessfulWeatherRequest ?: 0