Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Navigation Material] Mark transitionsInProgress complete in BottomSheetNavigator #1244

Merged
merged 2 commits into from Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,10 +17,13 @@
package com.google.accompanist.navigation.material

import android.os.Bundle
import androidx.activity.OnBackPressedDispatcher
import androidx.activity.compose.LocalOnBackPressedDispatcherOwner
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material.Button
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetState
import androidx.compose.material.ModalBottomSheetValue
Expand All @@ -34,10 +37,14 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.plusAssign
import androidx.navigation.testing.TestNavigatorState
Expand Down Expand Up @@ -305,6 +312,62 @@ internal class BottomSheetNavigatorTest {
.containsExactly(entry)
}

@Test
fun testBackPressedDestroysEntry() {
lateinit var onBackPressedDispatcher: OnBackPressedDispatcher
lateinit var navController: NavHostController

composeTestRule.setContent {
val bottomSheetNavigator = rememberBottomSheetNavigator()
navController = rememberNavController(bottomSheetNavigator)
onBackPressedDispatcher =
LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher!!

ModalBottomSheetLayout(bottomSheetNavigator) {
Box(modifier = Modifier.fillMaxSize()) {
NavHost(
navController = navController,
startDestination = "mainScreen"
) {

composable(
route = "mainScreen",
content = {
Button(onClick = { navController.navigate("bottomSheet") }) {
Text(text = "open drawer")
}
}
)

bottomSheet(
route = "bottomSheet",
content = {
Box(modifier = Modifier.fillMaxSize()) {
Text(
text = "bottomSheet"
)
}
}
)
}
}
}
}

composeTestRule.onNodeWithText("open drawer").performClick()

lateinit var bottomSheetEntry: NavBackStackEntry

composeTestRule.runOnIdle {
bottomSheetEntry = navController.currentBackStackEntry!!
onBackPressedDispatcher.onBackPressed()
}

composeTestRule.runOnIdle {
assertThat(bottomSheetEntry.lifecycle.currentState).isEqualTo(Lifecycle.State.DESTROYED)
}
}

/**
* Create a [BottomSheetNavigator.Destination] with some fake content
* Having an empty Composable will result in the sheet's height being 0 which crashes
Expand Down
Expand Up @@ -177,7 +177,7 @@ public class BottomSheetNavigator(
// currently displaying because it will have its transition completed when the sheet's
// animation has completed
DisposableEffect(backStackEntries) {
backStackEntries.forEach {
state.transitionsInProgress.value.forEach {
if (it != latestEntry) state.markTransitionComplete(it)
}
onDispose { }
Expand Down