https://kotlinlang.org logo
Title
e

Emmanuel

07/18/2022, 5:04 PM
Code Review:
fun onRefreshTrackCollection() {
    val job = scope.launch {
        refreshTrackCollection()
    }
    when(job.isCompleted) {
        // show snackbar message
    }
}
How do I better execute a UI change when a coroutine job is complete?
l

Landry Norris

07/18/2022, 5:07 PM
I would have a StateFlow for isShowingSnackbar (or add it to a data class that holds UI state that is exposed via StateFlow), and have
if(isShowingSnackbar)
in my compose code.
I would also set the variable after
refreshTaskCollection()
inside the
launch
block.
fun onRefreshTrackCollection() {
    scope.launch {
        refreshTrackCollection()
        isShowingSnackbarFlow.update { true }
    }
}
e

Emmanuel

07/18/2022, 5:11 PM
I'll give that a go