Skip to content

Commit

Permalink
Only get transitionsInProgress when attached
Browse files Browse the repository at this point in the history
Make sure that the Navigator is attached before attempting to get its
state.
  • Loading branch information
Jeremy Woods committed Jul 13, 2022
1 parent 3f8a0ce commit e32a4e2
Showing 1 changed file with 15 additions and 2 deletions.
Expand Up @@ -150,6 +150,18 @@ public class BottomSheetNavigator(
MutableStateFlow(emptyList())
}

/**
* Get the transitionsInProgress from the [state]. In some cases, the [sheetContent] might be
* composed before the Navigator is attached, so we specifically return an empty flow if we
* aren't attached yet.
*/
private val transitionsInProgress: StateFlow<Set<NavBackStackEntry>>
get() = if (attached) {
state.transitionsInProgress
} else {
MutableStateFlow(emptySet())
}

/**
* Access properties of the [ModalBottomSheetLayout]'s [ModalBottomSheetState]
*/
Expand All @@ -163,6 +175,7 @@ public class BottomSheetNavigator(
val columnScope = this
val saveableStateHolder = rememberSaveableStateHolder()
val backStackEntries by backStack.collectAsState()
val transitionsInProgressEntries by transitionsInProgress.collectAsState()

// We always replace the sheet's content instead of overlaying and nesting floating
// window destinations. That means that only *one* concurrent destination is supported by
Expand All @@ -177,7 +190,7 @@ public class BottomSheetNavigator(
// currently displaying because it will have its transition completed when the sheet's
// animation has completed
DisposableEffect(backStackEntries) {
state.transitionsInProgress.value.forEach {
transitionsInProgressEntries.forEach {
if (it != latestEntry) state.markTransitionComplete(it)
}
onDispose { }
Expand All @@ -194,7 +207,7 @@ public class BottomSheetNavigator(
onSheetDismissed = { backStackEntry ->
// Sheet dismissal can be started through popBackStack in which case we have a
// transition that we'll want to complete
if (state.transitionsInProgress.value.contains(backStackEntry)) {
if (transitionsInProgressEntries.contains(backStackEntry)) {
state.markTransitionComplete(backStackEntry)
} else {
state.pop(popUpTo = backStackEntry, saveState = false)
Expand Down

0 comments on commit e32a4e2

Please sign in to comment.