Tom Colvin
Jul 19, 2023

--

The loading state is handled in the view model code which converts info received from the repository into a PlanetsListUiState instance.
When we're told it's in the WorkResult.Loading state our view will get a PlanetsListUiState with isLoading = true, and so the UI is able to show a loading spinner or similar.
Here's the code:

val uiState = planets.map { planets ->
when (planets) {
is WorkResult.Error -> PlanetsListUiState(isError = true)
is WorkResult.Loading -> PlanetsListUiState(isLoading = true)
is WorkResult.Success -> PlanetsListUiState(planets = planets.data)
}
}

...which is a snip from Step 5 in the article, you can see more context by looking at the repo.

--

--

Tom Colvin
Tom Colvin

Written by Tom Colvin

Android developer / consultant; freelancer or through my agency Apptaura. Google Developer Expert in Android. tomcolvin.co.uk Articles 100% me, no AI.

No responses yet