Created
August 18, 2025 00:28
-
-
Save ioannisa/e90d53c2abf53edbe1dae6631d838f83 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun <T>ObserveEffects( | |
| flow: Flow<T>, | |
| key1: Any? = null, | |
| key2: Any? = null, | |
| onEvent: suspend (T) -> Unit | |
| ) { | |
| val lifecycleOwner = LocalLifecycleOwner.current | |
| LaunchedEffect(flow, lifecycleOwner, key1, key2) { | |
| lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { | |
| withContext(Dispatchers.Main.immediate){ | |
| flow.collect { | |
| onEvent(it) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| example usage: | |
| @Composable | |
| fun WelcomeScreenRoot( | |
| viewModel: WelcomeViewModel = koinViewModel(), | |
| onNavigate: (LoginGroupNavType) -> Unit | |
| ) { | |
| ObserveEffects(viewModel.events) { effect -> | |
| when (effect) { | |
| is WelcomeEffect.Navigate -> { | |
| onNavigate(effect.route) | |
| } | |
| } | |
| } | |
| WelcomeScreen( | |
| state = viewModel.state, | |
| onIntent = viewModel::onAction | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment