From 4363ad1057a7d99489352debeb46c17ff748c599 Mon Sep 17 00:00:00 2001 From: James Rich Date: Thu, 21 May 2026 17:51:44 -0500 Subject: [PATCH] feat(car): add lifecycle-aware screen invalidation on state changes HomeScreen now observes CarStateCoordinator flows and calls invalidate() when messaging or node state updates, ensuring the template refreshes with live data. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../feature/car/screens/HomeScreen.kt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/feature/car/src/main/kotlin/org/meshtastic/feature/car/screens/HomeScreen.kt b/feature/car/src/main/kotlin/org/meshtastic/feature/car/screens/HomeScreen.kt index 6d1b76fd79..8a4c7c6184 100644 --- a/feature/car/src/main/kotlin/org/meshtastic/feature/car/screens/HomeScreen.kt +++ b/feature/car/src/main/kotlin/org/meshtastic/feature/car/screens/HomeScreen.kt @@ -26,6 +26,13 @@ import androidx.car.app.model.Tab import androidx.car.app.model.TabContents import androidx.car.app.model.TabTemplate import androidx.car.app.model.Template +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleOwner +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel +import kotlinx.coroutines.launch import org.meshtastic.feature.car.R import org.meshtastic.feature.car.model.NodeUi import org.meshtastic.feature.car.model.SignalQuality @@ -34,6 +41,26 @@ import org.meshtastic.feature.car.service.CarStateCoordinator class HomeScreen(carContext: CarContext, private val stateCoordinator: CarStateCoordinator) : Screen(carContext) { private var selectedTabId: String = TAB_ID_MESSAGES + private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) + + init { + lifecycle.addObserver( + object : DefaultLifecycleObserver { + override fun onStart(owner: LifecycleOwner) { + observeState() + } + + override fun onDestroy(owner: LifecycleOwner) { + scope.cancel() + } + }, + ) + } + + private fun observeState() { + scope.launch { stateCoordinator.messagingState.collect { invalidate() } } + scope.launch { stateCoordinator.nodeDashboardState.collect { invalidate() } } + } override fun onGetTemplate(): Template { val messagingTab =