Skip to content

Instantly share code, notes, and snippets.

@CostaFot
Last active July 25, 2024 14:07
Show Gist options
  • Save CostaFot/c663f4f1593562e6d72d45aae8de95d0 to your computer and use it in GitHub Desktop.
Save CostaFot/c663f4f1593562e6d72d45aae8de95d0 to your computer and use it in GitHub Desktop.
// viewModel layer
class MyViewModel: ViewModel() {
private val _eventWithChannel = Channel<Event>()
val eventFlowFromChannel = _eventWithChannel.receiveAsFlow()
}
sealed class Event {
object NavigateToAnotherScreen: Event()
}
// compose layer
LaunchedEffect(lifecycleOwner) {
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
// switch to Dispatchers.Main.immediate maaaaaaaybe a bit overkill
withContext(Dispatchers.Main.immediate) {
viewModel.eventFlowFromChannel.collect {
// handle one-time event
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment