Step #2: Reporting our Loaded Status

Right now, RosterListFragment cannot detect when we are loading data. Because of the rules of StateFlow, we have to provide an initial value, which has an empty list of to-do items. RosterListFragment has no good way to distinguish that from the case where we have loaded the data and the database is empty. So, we need to do something to clarify “empty but not yet loaded” from “empty after loading”.

To that end, add an isLoaded property to RosterViewState:

data class RosterViewState(
  val items: List<ToDoModel> = listOf(),
  val isLoaded: Boolean = false
)

We will use false to indicate that we are loading the data and true to indicate that the data is loaded. Since we default it to false, we need to update our map() call in the states definition of RosterMotor to pass in true:

  val states = repo.items()
    .map { RosterViewState(it, true) }
    .stateIn(viewModelScope, SharingStarted.Eagerly, RosterViewState())

Now, isLoaded will be false for the initial state and true once our data is loaded.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.