How to update a value of a UiState on a StateFlow?

Having this on my viewmodel:

val uiState: StateFlow<BusStopsDBScreenUiState> = busDataRepository.getBusStops()
        .map<List<BusStop>, BusStopsDBScreenUiState> { busStops ->
            BusStopsDBScreenUiState.Success(busStops, Res.string.bus_stops_db_filled)
        }
        .catch { throwable ->
            emit(BusStopsDBScreenUiState.Error(throwable))
        }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), BusStopsDBScreenUiState.Loading)

    fun updateBusStops() {
        viewModelScope.launch(Dispatchers.Main) {
            launch(Dispatchers.IO) {
                busDataRepository.updateBusStops()
            }
        }
    }

With this UiState:

sealed interface BusStopsDBScreenUiState {
    object Loading : BusStopsDBScreenUiState
    data class Error(val throwable: Throwable) : BusStopsDBScreenUiState
    data class Success(
        val data: List<BusStop>,
        val dialogText: StringResource?
    ) : BusStopsDBScreenUiState
}

I need to implement a method on my viewmodel that modifies the dialogText variable from
my Sucess uistate to null, for hidding the dialog when the user press the dialog close button:

fun closeDialog() {
    //TODO
}

How can I modify my val uiState: StateFlow<BusStopsDBScreenUiState> in that new function to set the success dialogText variable to null without lossing the data variable content?

1

That can be done by introducing an additional MutableStateFlow:

private val showDialog = MutableStateFlow(true)

Now your uiState needs to be derived from two flows. You can combine them like this:

val uiState: StateFlow<BusStopsDBScreenUiState> = combine(
    showDialog,
    busDataRepository.getBusStops(),
) { showDialog, busStops ->
    BusStopsDBScreenUiState.Success(
        data = busStops,
        dialogText = if (showDialog) Res.string.bus_stops_db_filled else null,
    ) as BusStopsDBScreenUiState
}
    .catch { throwable ->
        emit(BusStopsDBScreenUiState.Error(throwable))
    }.stateIn(
        scope = viewModelScope,
        started = SharingStarted.WhileSubscribed(5000),
        initialValue = BusStopsDBScreenUiState.Loading,
    )

Instead of map this uses the combine’s transform block where you now have the values of both flows available. This allows you to set dialogText accordingly.

You can now simply toggle the MutableStateFlow’s value to hide the dialog:

fun closeDialog() {
    showDialog.value = false
}

When this is called, only the combine’s transform block will be executed again (and everything after that), the busDataRepository.getBusStops() flow didn’t change so its old value is used. You now have a new BusStopsDBScreenUiState.Success value in your StateFlow with the same data but dialogText set to null.


You might want to consider extracting the mapping logic into a separate function to keep the uiState property clean of business logic:

private fun BusStopsDBScreenUiState(
    showDialog: Boolean,
    busStops: List<BusStop>,
): BusStopsDBScreenUiState = BusStopsDBScreenUiState.Success(
    data = busStops,
    dialogText = if (showDialog) Res.string.bus_stops_db_filled else null,
)

Then you can even convert the function call in the lambda to a reference, which makes it even more concise:

val uiState: StateFlow<BusStopsDBScreenUiState> =
    combine(
        showDialog,
        busDataRepository.getBusStops(),
        ::BusStopsDBScreenUiState,
    ).catch { throwable ->
        emit(BusStopsDBScreenUiState.Error(throwable))
    }.stateIn(
        scope = viewModelScope,
        started = SharingStarted.WhileSubscribed(5000),
        initialValue = BusStopsDBScreenUiState.Loading,
    )

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How to update a value of a UiState on a StateFlow?

Having this on my viewmodel:

val uiState: StateFlow<BusStopsDBScreenUiState> = busDataRepository.getBusStops()
        .map<List<BusStop>, BusStopsDBScreenUiState> { busStops ->
            BusStopsDBScreenUiState.Success(busStops, Res.string.bus_stops_db_filled)
        }
        .catch { throwable ->
            emit(BusStopsDBScreenUiState.Error(throwable))
        }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), BusStopsDBScreenUiState.Loading)

    fun updateBusStops() {
        viewModelScope.launch(Dispatchers.Main) {
            launch(Dispatchers.IO) {
                busDataRepository.updateBusStops()
            }
        }
    }

With this UiState:

sealed interface BusStopsDBScreenUiState {
    object Loading : BusStopsDBScreenUiState
    data class Error(val throwable: Throwable) : BusStopsDBScreenUiState
    data class Success(
        val data: List<BusStop>,
        val dialogText: StringResource?
    ) : BusStopsDBScreenUiState
}

I need to implement a method on my viewmodel that modifies the dialogText variable from
my Sucess uistate to null, for hidding the dialog when the user press the dialog close button:

fun closeDialog() {
    //TODO
}

How can I modify my val uiState: StateFlow<BusStopsDBScreenUiState> in that new function to set the success dialogText variable to null without lossing the data variable content?

1

That can be done by introducing an additional MutableStateFlow:

private val showDialog = MutableStateFlow(true)

Now your uiState needs to be derived from two flows. You can combine them like this:

val uiState: StateFlow<BusStopsDBScreenUiState> = combine(
    showDialog,
    busDataRepository.getBusStops(),
) { showDialog, busStops ->
    BusStopsDBScreenUiState.Success(
        data = busStops,
        dialogText = if (showDialog) Res.string.bus_stops_db_filled else null,
    ) as BusStopsDBScreenUiState
}
    .catch { throwable ->
        emit(BusStopsDBScreenUiState.Error(throwable))
    }.stateIn(
        scope = viewModelScope,
        started = SharingStarted.WhileSubscribed(5000),
        initialValue = BusStopsDBScreenUiState.Loading,
    )

Instead of map this uses the combine’s transform block where you now have the values of both flows available. This allows you to set dialogText accordingly.

You can now simply toggle the MutableStateFlow’s value to hide the dialog:

fun closeDialog() {
    showDialog.value = false
}

When this is called, only the combine’s transform block will be executed again (and everything after that), the busDataRepository.getBusStops() flow didn’t change so its old value is used. You now have a new BusStopsDBScreenUiState.Success value in your StateFlow with the same data but dialogText set to null.


You might want to consider extracting the mapping logic into a separate function to keep the uiState property clean of business logic:

private fun BusStopsDBScreenUiState(
    showDialog: Boolean,
    busStops: List<BusStop>,
): BusStopsDBScreenUiState = BusStopsDBScreenUiState.Success(
    data = busStops,
    dialogText = if (showDialog) Res.string.bus_stops_db_filled else null,
)

Then you can even convert the function call in the lambda to a reference, which makes it even more concise:

val uiState: StateFlow<BusStopsDBScreenUiState> =
    combine(
        showDialog,
        busDataRepository.getBusStops(),
        ::BusStopsDBScreenUiState,
    ).catch { throwable ->
        emit(BusStopsDBScreenUiState.Error(throwable))
    }.stateIn(
        scope = viewModelScope,
        started = SharingStarted.WhileSubscribed(5000),
        initialValue = BusStopsDBScreenUiState.Loading,
    )

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật