MaterialNavigation causes SideEffects Channel to not execute in LaunchedEffect
Describe the bug
After including the com.google.accompanist:accompanist-navigation-material:0.16.1 library into my module, my side effects which are collected as flow are not executing anymore after I send a SideEffect via a Channel.
To Reproduce
Steps to reproduce the behavior:
- Include dependency for materialNavigation :
com.google.accompanist:accompanist-navigation-material:0.16.1 - You have to collect the sideEffects Channel in a LaunchedEffect in your composable
- Trigger you sideEffect via ex. a button click
- Observe the sideEffect channel is not collected
Expected behavior
SideEffects channel would work even when including the materialNavigation library. I would expect my Toast to appear.
Environment:
- Android OS version: [e.g. Android 10.0]
- Device: [e.g. Emulator Pixel 3a, also same real device]
- Accompanist version: [v0.16.0, v0.16.1]
Additional context
Not sure if this is a dependency problem or what, when I remove this library everything works fine. I am using the materialNavigation for the modal bottom sheet functionality. Here is an example Composable and ViewModel:
@Composable
fun MyScreenScreen() {
val viewModel = hiltViewModel<MyScreenViewModel>()
val viewState = viewModel.viewState.collectAsState().value
val intentChannel = viewModel.intentChannel
val sideEffects = viewModel.sideEffects
MyScreenSideEffects(sideEffects)
MyScreenScreen(viewState, intentChannel)
}
@Composable
private fun MyScreenSideEffects(
sideEffects: Channel<MyScreenSideEffect>
) {
val context = LocalContext.current
LaunchedEffect(true) {
sideEffects.consumeAsFlow().collect { sideEffect ->
when (sideEffect) {
is MyScreenSideEffect.ShowToast -> {
Toast.makeText(context, "Message", Toast.LENGTH_SHORT).show()
}
}
}
}
}
@Composable
private fun MyScreenScreen(
viewState: MyScreenViewState,
intentChannel: Channel<MyScreenIntent> = Channel()
) {
Button(onClick = { intentChannel.trySend(MyScreenIntent.ButtonClicked) }) {
Text(text = "Click me")
}
}
@HiltViewModel
class MyScreenViewModel @Inject constructor(
val dispatcherProvider: DispatcherProvider
) : ViewModel() {
private val _viewState: MutableStateFlow<MyScreenViewState> =
MutableStateFlow(MyScreenViewState())
val viewState = _viewState.asStateFlow()
private val _intentChannel = Channel<MyScreenIntent>(Channel.UNLIMITED)
val intentChannel = _intentChannel
private val _sideEffects = Channel<MyScreenSideEffect>(Channel.UNLIMITED)
val sideEffects = _sideEffects
init {
viewModelScope.launch(dispatcherProvider.main()) {
launch {
_intentChannel.consumeAsFlow().collect { intent ->
when (intent) {
is MyScreenIntent.ButtonClicked -> {
_sideEffects.trySend(MyScreenSideEffect.ShowToast)
}
is MyScreenIntent.BackButtonClicked -> navigateBack()
}
}
}
}
}
}
Just to be clear, is that without actually using any code from Navigation Material, just by including the dependency? Or are you using code from Navigation Material? If yes, please provide a repro with that.
Just to be clear, is that without actually using any code from Navigation Material, just by including the dependency? Or are you using code from Navigation Material? If yes, please provide a repro with that.
This is just by including the dependency.
Did you already have a dependency on Navigation Compose 2.4.0-alpha06? Accompanist Navigation 0.16.X will upgrade you to at least that version.
If you were still depending on an older version of Navigation, I'd upgrade that separately and see if you see the issue with just moving to Navigation 2.4.0-alpha06. You'd want to file an issue against Navigation if you do see an issue due to that upgrade. That will at least rule out Navigation Material as the source of your issue.
Hey thanks for the fast responses, I had Navigation Compose 2.4.0-alpha01 where everything worked, but after updating to alpha06 I had the same issue. I tried creating a simple sample project to reproduce the bug but I wasn't able to do reproduce it. Will need to dig deeper into my dependencies to try and figure out what else could be wrong.
Gotcha. If your issue was caused by upgrading just Navigation Compose, then this would rule out this being an issue associated with specifically Accompanist Navigation Material.
If you do manage to reproduce it in a sample project, please file that issue against Navigation (and perhaps link it here for posterity) and we can confirm if the in-flight fixes to Navigation Compose also fix your issue.
Hi, I managed to reproduce the issue in a sample project and opened an issue here: https://issuetracker.google.com/u/1/issues/196076691