weather: fix confusion on startup #395
- Commit
e68e70fff7be581255c0be0518b17d8542864d25- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-31 21:10 UTC (4 months ago)
- Parents
25297f0
Diff
weather/src/main/java/com/vayunmathur/weather/ui/HomePage.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/HomePage.kt b/weather/src/main/java/com/vayunmathur/weather/ui/HomePage.ktindex f763c3296d..b5912b96e9 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/HomePage.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/HomePage.kt@@ -63,7 +63,15 @@ import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @Composable fun HomePage(backStack: NavBackStack<Route>, viewModel: WeatherViewModel) {- val locations by viewModel.savedLocations.collectAsState()+ val locations = viewModel.savedLocations.collectAsState().value++ if (locations == null) {+ // Room hasn't answered yet. Hold a plain surface rather than falling+ // through to EmptyHome, which would flash the location chooser on+ // every cold start before the saved rows arrive.+ Scaffold(containerColor = MaterialTheme.colorScheme.surfaceContainerHigh) {}+ return+ } if (locations.isEmpty()) { EmptyHome(
weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt b/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.ktindex 97f4ee430f..996be029ff 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt@@ -149,7 +149,7 @@ fun LocationsScreen( onLocationSelect: (SavedLocation) -> Unit, onClose: () -> Unit, ) {- val locations by viewModel.savedLocations.collectAsState()+ val locations = viewModel.savedLocations.collectAsState().value.orEmpty() val forecasts by viewModel.forecasts.collectAsState() // Ticks every 30s so the "Last updated Xm ago" labels advance over time
weather/src/main/java/com/vayunmathur/weather/util/WeatherViewModel.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/util/WeatherViewModel.kt b/weather/src/main/java/com/vayunmathur/weather/util/WeatherViewModel.ktindex 2bac0d65cd..ce10bc62b3 100644--- a/weather/src/main/java/com/vayunmathur/weather/util/WeatherViewModel.kt+++ b/weather/src/main/java/com/vayunmathur/weather/util/WeatherViewModel.kt@@ -58,8 +58,15 @@ class WeatherViewModel( private val dao: WeatherDao, ) : AndroidViewModel(application) { - val savedLocations: StateFlow<List<SavedLocation>> = dao.observeLocations()- .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())+ /**+ * Saved locations, or null until Room's first emission lands. The null+ * state matters: seeding this with `emptyList()` made a cold start render+ * the "no locations yet" chooser for a frame or two before the real rows+ * arrived, so the UI must be able to tell "still loading" apart from+ * "genuinely empty".+ */+ val savedLocations: StateFlow<List<SavedLocation>?> = dao.observeLocations()+ .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), null) /** Per-location forecast state, keyed by [SavedLocation.id]. */ private val _forecasts = MutableStateFlow<Map<Long, ForecastUiState>>(emptyMap())@@ -243,7 +250,7 @@ class WeatherViewModel( * network. */ fun refreshAll(force: Boolean = false) {- for (location in savedLocations.value) {+ for (location in savedLocations.value.orEmpty()) { ensureForecast(location, force) } }