How to convert Flow<List<Custom Type>> to MutableStateFlow<UiState>?

55 Views Asked by At

How to update _uiState's sumList when get Flow<List<Sum>> from room/repo? Thanks a lot!

data class UiState(
    val xxx: Boolean = false,
    val yyy: Boolean = false,
    val sumList: List<Sum> = listOf(),
)

class SumViewModel(repo: Repo) : ViewModel() {
    private val _uiState = MutableStateFlow(UiState())
    val uiState: StateFlow<UiState> = _uiState.asStateFlow()

    //get sumList from room/repo
    val sumList: Flow<List<Sum>> = repo.getSumListFlow()

    fun updateXxxAndYyy() {
        _uiState.update { it.copy(xxx = true, yyy = true) }
    }

    fun updateSumList() {
        //problem: how to update sumList in _uiState from room<->Flow<List<Sum>> ?
        _uiState.update { it.copy(/* how to use repo.getSumListFlow() here?*/) }
    }
}

I've tried to use List<Sum> instead of Flow<List<Sum>>, but data won't auto refresh. I expect to use uiState as the only source in Screen Class .

0

There are 0 best solutions below