https://kotlinlang.org logo
Title
m

Micko Cabacungan

04/06/2022, 6:52 PM
Is there a proper way to observe a SharedFlow in a Composable function, for StateFlow we'd use
collectAsState
but if I do that for a SharedFlow, it will remember the last value, instead of firing and forget
c

Chris Fillmore

04/06/2022, 6:54 PM
Can you describe your use case?
m

Micko Cabacungan

04/06/2022, 6:55 PM
Basically I am trying to send a one time SideEffect that will play an animation
but I want to be able to do this everytime I send that SideEffect to play the animation
c

Chris Fillmore

04/06/2022, 6:59 PM
Someone may have a better answer, but you can collect the SharedFlow in a LaunchedEffect, then do what you will as new values come in
🙏 1
I have some similar-sounding requirement in my backlog currently so I’d be curious to know how this turns out for you
m

Micko Cabacungan

04/06/2022, 7:02 PM
I will give you an update if it works out 👍
t

Tobias Gronbach

04/06/2022, 11:23 PM
Had a similiar issue for navigation event. Found that extensionfunction on the web (can't remember where) and it works just fine.
@Composable
fun <T> Flow<T>.collectAsEffect(
    context: CoroutineContext = EmptyCoroutineContext,
    block: (T) -> Unit,
) {
    LaunchedEffect(key1 = Unit) {
        onEach(block).flowOn(context).launchIn(this)
    }
}
🙌 1
m

Micko Cabacungan

04/06/2022, 11:46 PM
@Chris Fillmore ^ -- I had a work around but this seems to be much cleaner
🙂 1
👍 1
Thank you @Tobias Gronbach