Tim Malseed
03/22/2023, 10:37 PMTim Malseed
03/22/2023, 10:38 PMstateIn(viewModelScope, whileSubscribed(5000), ViewState.Loading)Pablichjenkov
03/22/2023, 10:54 PMTim Malseed
03/22/2023, 10:55 PMTim Malseed
03/22/2023, 10:56 PMPablichjenkov
03/22/2023, 11:00 PMPablichjenkov
03/22/2023, 11:03 PMPablichjenkov
03/22/2023, 11:06 PMTim Malseed
03/22/2023, 11:17 PM10:11:56.288 onDestinationChanged: home_route // start destination
10:11:56.450 AuthorizationState: Unauthorized // User is unauthorized
10:11:56.524 ViewState: Loading // Initial view state
10:11:56.803 ViewState: Login // Because user is unauthorized
10:11:56.803 navController.navigate(login_route) // Navigate to login screen
10:11:56.806 onDestinationChanged: login_route // User arrives. Presses 'login'
10:12:05.158 AuthorizationState: Authorized // Auth state changes to 'authorized'
10:12:05.726 navController.popBackStack() // go back to home screen
10:12:05.727 onDestinationChanged: home_route // we're back
10:12:05.745 ViewState: Login // ViewState is Login? Should be 'Ready'
10:12:05.745 navController.navigate(login_route) // Navigate back to the login screen
10:12:05.750 onDestinationChanged: login_route // Go to login screen
10:12:05.786 navController.popBackStack() // Oh, user is authorized, pop back again
10:12:05.788 onDestinationChanged: home_route // Back to home
10:12:05.837 ViewState: Ready // Now the home screen has the up-to-date view stateTim Malseed
03/22/2023, 11:18 PMTim Malseed
03/22/2023, 11:21 PMpopBackStack()Pablichjenkov
03/22/2023, 11:30 PMTim Malseed
03/22/2023, 11:32 PMPablichjenkov
03/22/2023, 11:36 PMPablichjenkov
03/22/2023, 11:39 PMTim Malseed
03/22/2023, 11:42 PMTim Malseed
03/22/2023, 11:44 PMPablichjenkov
03/22/2023, 11:47 PMPablichjenkov
03/22/2023, 11:50 PMPablichjenkov
03/22/2023, 11:51 PMPablichjenkov
03/22/2023, 11:51 PMTim Malseed
03/22/2023, 11:55 PMcollectAsStateWithLifecycle(), which resets its state when the lifecycle changesTim Malseed
03/22/2023, 11:58 PMAlex Vanyo
03/23/2023, 12:54 AMWhileSubscribed(5000), the previous value of the StateFlow will be cached and it won’t expire due to the default value for replayExpiration, the second parameter of WhileSubscribed
So even if Loading is the default state, it’ll be caching the last one seen upon returning.
Maybe for authentication state in particular, it’d be better to not cache that last value? Maybe set replayExpiration to 0, and maybe also stopTimeoutMillis to 0 too specifically for the authentication:
WhileSubscribed(0, 0)
That would ensure that you’re refreshing the flow upon going back to the home screen, which will cause the initial Loading state againTim Malseed
03/23/2023, 12:58 AMstopTimeoutMillis to 0, but I didn’t even consider this replayExpiration parameter! That seems to solve the problemTim Malseed
03/23/2023, 12:59 AMWhileSubscribed docs and think about this a bit moreTim Malseed
03/23/2023, 1:04 AMColton Idle
03/26/2023, 2:26 PMTim Malseed
03/28/2023, 3:14 AM