@@ -44,6 +44,7 @@ import androidx.compose.material3.windowsizeclass.WindowSizeClass
4444import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
4545import androidx.compose.runtime.Composable
4646import androidx.compose.runtime.LaunchedEffect
47+ import androidx.compose.runtime.State as ComposeState
4748import androidx.compose.runtime.derivedStateOf
4849import androidx.compose.runtime.getValue
4950import androidx.compose.runtime.mutableLongStateOf
@@ -58,7 +59,7 @@ import androidx.compose.ui.res.stringResource
5859import androidx.compose.ui.unit.dp
5960import androidx.lifecycle.compose.collectAsStateWithLifecycle
6061import ca.amandeep.path.DeveloperStatus
61- import ca.amandeep.path.data.model.State
62+ import ca.amandeep.path.data.model.State as PathState
6263import ca.amandeep.path.main.core.ArrivalsUiModel
6364import ca.amandeep.path.main.core.MainUiModel
6465import ca.amandeep.path.main.core.Result
@@ -120,7 +121,7 @@ fun MainScreen(
120121
121122 val showDebugOptions by DeveloperStatus .developerModeFlow.collectAsStateWithLifecycle()
122123
123- val store = UserPreferencesRepo (context)
124+ val store = remember(context) { UserPreferencesRepo (context) }
124125
125126 val showModelNamePref by store.showModelName.collectAsStateWithLifecycle(initialValue = false )
126127 val aiSummarizeAlertsPref by store.aiSummarizeAlerts.collectAsStateWithLifecycle(initialValue = false )
@@ -260,6 +261,7 @@ fun MainScreen(
260261 setShowHelpGuide = setShowHelpGuideWithUndo,
261262 anyLocationPermissionsGranted = anyLocationPermissionsGranted,
262263 windowSizeClass = windowSizeClass,
264+ store = store,
263265 )
264266 PullRefreshIndicator (
265267 refreshing = refreshing,
@@ -290,14 +292,14 @@ private fun setAndComputeLastGoodState(
290292): MainUiModel {
291293 val uiModel by uiStateFlow.collectAsStateWithLifecycle(initialValue = MainUiModel ())
292294
293- val ( lastGoodState, setLastGoodState) = remember { mutableStateOf(MainUiModel ()) }
295+ var lastGoodState by remember { mutableStateOf(MainUiModel ()) }
294296
295- setLastGoodState(
296- MainUiModel (
297+ LaunchedEffect (uiModel) {
298+ lastGoodState = MainUiModel (
297299 arrivals = foldWithLastGoodState(uiModel, lastGoodState) { it.arrivals },
298300 alerts = foldWithLastGoodState(uiModel, lastGoodState) { it.alerts },
299- ),
300- )
301+ )
302+ }
301303
302304 // If all trains are empty, force a refresh, and show a loading screen
303305 val allTrainsEmpty = lastGoodState.arrivals.let {
@@ -307,14 +309,10 @@ private fun setAndComputeLastGoodState(
307309 LaunchedEffect (allTrainsEmpty, forceUpdate) {
308310 if (allTrainsEmpty) {
309311 forceUpdate()
310- setLastGoodState(
311- MainUiModel (alerts = lastGoodState.alerts),
312- )
312+ lastGoodState = MainUiModel (alerts = lastGoodState.alerts)
313313 }
314314 }
315315
316- d { " lastGoodState: $lastGoodState " }
317-
318316 return lastGoodState
319317}
320318
@@ -348,8 +346,10 @@ private fun MainScreenContent(
348346 setShowElevatorAlerts : suspend (Boolean ) -> Unit ,
349347 setShowHelpGuide : suspend (Boolean ) -> Unit ,
350348 windowSizeClass : WindowSizeClass ,
349+ store : UserPreferencesRepo ,
351350) {
352- val connectivityState by LocalContext .current.observeConnectivity()
351+ val connectivityState by LocalContext .current
352+ .observeConnectivity()
353353 .collectAsStateWithLifecycle(initialValue = ConnectionState .Available )
354354
355355 if (uiModel.arrivals is Result .Error ) {
@@ -364,7 +364,10 @@ private fun MainScreenContent(
364364 ) { isLoading ->
365365 val uiModelArrivals = uiModel.arrivals
366366 when (isLoading || uiModelArrivals !is Result .Valid ) {
367- true -> LoadingScreen ()
367+ true -> {
368+ LoadingScreen ()
369+ }
370+
368371 false -> {
369372 val lastUpdatedState = rememberLastUpdatedState(uiModelArrivals.lastUpdated)
370373 lastUpdatedState.KeepUpdatedEffect (uiModelArrivals.lastUpdated, 1 .seconds)
@@ -381,8 +384,6 @@ private fun MainScreenContent(
381384 },
382385 )
383386
384- val context = LocalContext .current
385- val store = UserPreferencesRepo (context)
386387 val showDirectionWarning by store.showDirectionWarning.collectAsStateWithLifecycle(initialValue = true )
387388 val setShowDirectionWarningPref = store::updateShowDirectionWarning
388389
@@ -396,7 +397,7 @@ private fun MainScreenContent(
396397 anyLocationPermissionsGranted = anyLocationPermissionsGranted,
397398 setShowingOppositeDirection = setShowingOppositeDirection,
398399 snackbarState = snackbarState,
399- lastUpdatedState = lastUpdatedState.value ,
400+ lastUpdatedState = lastUpdatedState,
400401 now = now,
401402 setShowHelpGuide = setShowHelpGuide,
402403 alertsExpanded = alertsExpanded,
@@ -422,7 +423,7 @@ fun LoadedScreen(
422423 setShowingOppositeDirection : suspend (Boolean ) -> Unit ,
423424 setShowElevatorAlerts : suspend (Boolean ) -> Unit ,
424425 snackbarState : SnackbarHostState ,
425- lastUpdatedState : LastUpdatedUiModel ,
426+ lastUpdatedState : ComposeState < LastUpdatedUiModel > ,
426427 now : Long ,
427428 setShowHelpGuide : suspend (Boolean ) -> Unit ,
428429 alertsExpanded : Boolean ,
@@ -469,13 +470,14 @@ fun LoadedScreen(
469470 val alertsModel = when (val uiModelAlerts = uiModel.alerts) {
470471 is Result .Valid -> uiModelAlerts.copy(
471472 data = uiModelAlerts.data.copy(
472- alerts = uiModelAlerts.data.alerts.filter {
473- if (userState.showElevatorAlerts) {
474- true
475- } else {
476- ! it.isElevator
477- }
478- }.toImmutableList(),
473+ alerts = uiModelAlerts.data.alerts
474+ .filter {
475+ if (userState.showElevatorAlerts) {
476+ true
477+ } else {
478+ ! it.isElevator
479+ }
480+ }.toImmutableList(),
479481 ),
480482 )
481483
@@ -517,13 +519,13 @@ fun LoadedScreen(
517519 else -> Unit
518520 }
519521 AnimatedVisibility (
520- visible = ! autoRefreshingNow && lastUpdatedState.secondsAgo > TOP_LAST_UPDATED_THRESHOLD_SECS ,
522+ visible = ! autoRefreshingNow && lastUpdatedState.value. secondsAgo > TOP_LAST_UPDATED_THRESHOLD_SECS ,
521523 enter = expandVertically(),
522524 exit = shrinkVertically(),
523525 ) {
524526 LastUpdatedInfoRow (
525527 modifier = spacingModifier,
526- lastUpdatedState = lastUpdatedState,
528+ lastUpdatedState = lastUpdatedState.value ,
527529 )
528530 }
529531 }
@@ -532,7 +534,7 @@ fun LoadedScreen(
532534 arrivals.data
533535 } else {
534536 arrivals.data.filter {
535- it.first.stationName.state == (if (userState.isInNJ) State .NJ else State .NY )
537+ it.first.stationName.state == (if (userState.isInNJ) PathState .NJ else PathState .NY )
536538 }
537539 }
538540 items(
@@ -554,7 +556,7 @@ fun LoadedScreen(
554556 ) {
555557 LastUpdatedInfoRow (
556558 modifier = spacingModifier,
557- lastUpdatedState = lastUpdatedState,
559+ lastUpdatedState = lastUpdatedState.value ,
558560 )
559561 }
560562 }
0 commit comments