Pablo
12/05/2024, 10:44 PMsealed interface MainScreenUiState {
object Loading : MainScreenUiState
data class Error(val throwable: Throwable) : MainScreenUiState
data class Success(val data: List<BusStop>) : MainScreenUiState
}
and this method of my `viewmodel`:
fun getBusStops() {
viewModelScope.launch(Dispatchers.Main) {
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
busDataRepository.updateBusStops() //updates local database with retrofit new data if available
}
val busStops = busDataRepository.getBusStops() //returns Flow<List<BusStop>>
//TODO update uiState with new busStops
}
}
how can I initialize my uistate variable with local data if exist (empty if not) and update it with new data when getBusStops()
of the viewmodel gets called by ui?
val uiState: StateFlow<MainScreenUiState>
Timo Drick
12/05/2024, 10:52 PM