I have the following case, where I listen to a flo...
# flow
a
I have the following case, where I listen to a flow for changes and trigger an API update whenever a new change is detected. Like this:
Copy code
private val _filtersState: MutableStateFlow<FiltersUiState> = MutableStateFlow(FiltersUiState.Loading)
    val uiState: StateFlow<FiltersUiState> = _filtersState.asStateFlow()

init {
    viewModelScope.launch {
            transactionPreferences.transactionInfoStream
                .onEach { _filtersState.update { FiltersUiState.Loading } }
                .map {
                    filtersRepository.loadFilters(it)
                        .map { /* TODO map to UI filters */ }
                }
                .catch { ex -> _filtersState.update { FiltersUiState.Error(ex) } }
                .collect()
    }
}
Could I simplify this? It seems wrong having to update the UI manually, on
onEach
,
map
and
catch