ListLab.kt
- 1class LessonViewModel(
- 2 private val repo: LessonRepository,
- 3) : ViewModel() {
- 4 private val _load = MutableStateFlow<Load<List<Lesson>>>(Load.Loading)
- 5 val load: StateFlow<Load<List<Lesson>>> = _load
- 6
- 7 init {
- 8 viewModelScope.launch {
- 9 _load.value = Load.Ready(repo.all())
- 10 }
- 11 }
- 12}
A list that does not freeze the first frame
The first composition paints a loading state. The read happens in a coroutine.
- The screen collects a StateFlow of Load<List<Lesson>>.
- The repository reads on Dispatchers.IO.
- An empty list is Ready(emptyList()), which is different from Loading.