From e32a4e297e5f1b7b9b77779b19a5fe54e3ac7926 Mon Sep 17 00:00:00 2001 From: Jeremy Woods Date: Wed, 13 Jul 2022 14:20:35 -0700 Subject: [PATCH] Only get transitionsInProgress when attached Make sure that the Navigator is attached before attempting to get its state. --- .../navigation/material/BottomSheetNavigator.kt | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/navigation-material/src/main/java/com/google/accompanist/navigation/material/BottomSheetNavigator.kt b/navigation-material/src/main/java/com/google/accompanist/navigation/material/BottomSheetNavigator.kt index 1a65781f1..ca94762e8 100644 --- a/navigation-material/src/main/java/com/google/accompanist/navigation/material/BottomSheetNavigator.kt +++ b/navigation-material/src/main/java/com/google/accompanist/navigation/material/BottomSheetNavigator.kt @@ -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> + get() = if (attached) { + state.transitionsInProgress + } else { + MutableStateFlow(emptySet()) + } + /** * Access properties of the [ModalBottomSheetLayout]'s [ModalBottomSheetState] */ @@ -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 @@ -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 { } @@ -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)