diff --git a/app/compose/compose-ui/src/main/kotlin/com/hedvig/android/compose/ui/SharedElements.kt b/app/compose/compose-ui/src/main/kotlin/com/hedvig/android/compose/ui/SharedElements.kt index d9e78be5d7..f4ed1bd270 100644 --- a/app/compose/compose-ui/src/main/kotlin/com/hedvig/android/compose/ui/SharedElements.kt +++ b/app/compose/compose-ui/src/main/kotlin/com/hedvig/android/compose/ui/SharedElements.kt @@ -20,6 +20,7 @@ import androidx.compose.animation.fadeOut import androidx.compose.runtime.Composable import androidx.compose.runtime.ProvidableCompositionLocal import androidx.compose.runtime.compositionLocalOf +import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.ui.Alignment.Companion.Center import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Rect @@ -32,33 +33,40 @@ import androidx.compose.ui.unit.LayoutDirection * A local which contains the SharedTransitionScope wrapping the entire app. This is always taking up the entire * screen's size and must be provided by the app's main activity */ -val LocalSharedTransitionScope: ProvidableCompositionLocal = compositionLocalOf { - error("The app must provide a SharedTransitionScope to the entire compose hierarchy") +val LocalSharedTransitionScope: ProvidableCompositionLocal = staticCompositionLocalOf { + null } @Composable -fun rememberGlobalSharedContentState(key: Any): SharedContentState = - LocalSharedTransitionScope.current.rememberSharedContentState(key) +fun rememberGlobalSharedContentState(key: Any): SharedContentState? = + LocalSharedTransitionScope.current?.rememberSharedContentState(key) +/** + * Is a no-op if [SharedTransitionScope], [AnimatedVisibilityScope] or [SharedContentState] are null + */ fun Modifier.globalSharedElement( - sharedTransitionScope: SharedTransitionScope, - animatedVisibilityScope: AnimatedVisibilityScope, - state: SharedContentState, + sharedTransitionScope: SharedTransitionScope?, + animatedVisibilityScope: AnimatedVisibilityScope?, + state: SharedContentState?, boundsTransform: BoundsTransform = DefaultBoundsTransform, placeHolderSize: PlaceHolderSize = contentSize, renderInOverlayDuringTransition: Boolean = true, zIndexInOverlay: Float = 0f, clipInOverlayDuringTransition: OverlayClip = ParentClip, -): Modifier = with(sharedTransitionScope) { - this@globalSharedElement.sharedElement( - state = state, - animatedVisibilityScope = animatedVisibilityScope, - boundsTransform = boundsTransform, - placeHolderSize = placeHolderSize, - renderInOverlayDuringTransition = renderInOverlayDuringTransition, - zIndexInOverlay = zIndexInOverlay, - clipInOverlayDuringTransition = clipInOverlayDuringTransition, - ) +): Modifier = if (sharedTransitionScope == null || animatedVisibilityScope == null || state == null) { + this +} else { + with(sharedTransitionScope) { + this@globalSharedElement.sharedElement( + state = state, + animatedVisibilityScope = animatedVisibilityScope, + boundsTransform = boundsTransform, + placeHolderSize = placeHolderSize, + renderInOverlayDuringTransition = renderInOverlayDuringTransition, + zIndexInOverlay = zIndexInOverlay, + clipInOverlayDuringTransition = clipInOverlayDuringTransition, + ) + } } fun Modifier.globalSharedBounds( diff --git a/app/navigation/navigation-compose/src/main/kotlin/com/hedvig/android/navigation/compose/LocalNavAnimatedVisibilityScope.kt b/app/navigation/navigation-compose/src/main/kotlin/com/hedvig/android/navigation/compose/LocalNavAnimatedVisibilityScope.kt index 703b2689e4..cdd06b9fa2 100644 --- a/app/navigation/navigation-compose/src/main/kotlin/com/hedvig/android/navigation/compose/LocalNavAnimatedVisibilityScope.kt +++ b/app/navigation/navigation-compose/src/main/kotlin/com/hedvig/android/navigation/compose/LocalNavAnimatedVisibilityScope.kt @@ -9,6 +9,6 @@ import androidx.navigation.NavGraphBuilder * A local which contains the AnimatedVisibilityScope tied to the current navigation's destination. * See [NavGraphBuilder.navdestination] for how it's provided. */ -val LocalNavAnimatedVisibilityScope: ProvidableCompositionLocal = compositionLocalOf { - error("Must be under a compose `navdestination`") +val LocalNavAnimatedVisibilityScope: ProvidableCompositionLocal = compositionLocalOf { + null }