Skip to content

Instantly share code, notes, and snippets.

@Leeonardoo
Created June 9, 2022 01:28
Show Gist options
  • Save Leeonardoo/799daff12fdbb0ae0d0819892b18f3e9 to your computer and use it in GitHub Desktop.
Save Leeonardoo/799daff12fdbb0ae0d0819892b18f3e9 to your computer and use it in GitHub Desktop.
A quick and bad workaround for a crash from rotating the device when a bottom sheet is too tall using Jetpack Navigation for Compose
package com.example.app
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.layout.*
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.core.view.WindowCompat
import androidx.navigation.plusAssign
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import dagger.hilt.android.AndroidEntryPoint
@OptIn(ExperimentalAnimationApi::class)
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterialApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
setContent {
val navController = rememberAnimatedNavController()
val bottomSheetState = com.example.app.rememberModalBottomSheetState(
initialValue = ModalBottomSheetValue.Hidden,
skipHalfExpanded = false
)
val bottomSheetNavigator =
com.example.app.rememberBottomSheetNavigator(bottomSheetState)
navController.navigatorProvider += bottomSheetNavigator
ExampleAppTheme {
val containerColor = MaterialTheme.colorScheme.surface
val contentColor = contentColorFor(containerColor)
CompositionLocalProvider(LocalContentColor provides contentColor) {
// TODO: Replace with M3 ModalBottomSheetLayout when available
com.example.app.ModalBottomSheetLayout(
bottomSheetNavigator = bottomSheetNavigator,
bottomSheetState = bottomSheetState,
sheetBackgroundColor = MaterialTheme.colorScheme.surfaceColorAtElevation(
DrawerDefaults.ModalDrawerElevation
),
sheetContentColor = contentColor,
sheetShape = SheetShape,
// Default scrim color is onSurface which is incorrect in dark theme
// https://issuetracker.google.com/issues/183697056
scrimColor = SheetScrimColor
) {
Scaffold(
//Your scaffold params
) { paddingValues ->
//Your content
}
}
}
}
}
}
}
package com.example.app
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Dp
import com.google.accompanist.navigation.material.BottomSheetNavigator
import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi
@ExperimentalMaterialNavigationApi
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ModalBottomSheetLayout(
bottomSheetNavigator: BottomSheetNavigator,
bottomSheetState: ModalBottomSheetState,
modifier: Modifier = Modifier,
sheetShape: Shape = MaterialTheme.shapes.large,
sheetElevation: Dp = ModalBottomSheetDefaults.Elevation,
sheetBackgroundColor: Color = MaterialTheme.colors.surface,
sheetContentColor: Color = contentColorFor(sheetBackgroundColor),
scrimColor: Color = ModalBottomSheetDefaults.scrimColor,
content: @Composable () -> Unit
) {
ModalBottomSheetLayout(
sheetState = bottomSheetState,
sheetContent = bottomSheetNavigator.sheetContent,
modifier = modifier,
sheetShape = sheetShape,
sheetElevation = sheetElevation,
sheetBackgroundColor = sheetBackgroundColor,
sheetContentColor = sheetContentColor,
scrimColor = scrimColor,
content = content
)
}
package com.example.app
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetState
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.SwipeableDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import com.google.accompanist.navigation.material.BottomSheetNavigator
import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi
@ExperimentalMaterialNavigationApi
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun rememberBottomSheetNavigator(
sheetState: ModalBottomSheetState = rememberModalBottomSheetState(
initialValue= ModalBottomSheetValue.Hidden,
animationSpec = SwipeableDefaults.AnimationSpec,
skipHalfExpanded = false
),
): BottomSheetNavigator {
return remember(sheetState) {
BottomSheetNavigator(sheetState = sheetState)
}
}
@Composable
@OptIn(ExperimentalMaterialApi::class)
fun rememberModalBottomSheetState(
initialValue: ModalBottomSheetValue,
animationSpec: AnimationSpec<Float> = SwipeableDefaults.AnimationSpec,
skipHalfExpanded: Boolean,
confirmStateChange: (ModalBottomSheetValue) -> Boolean = { true }
): ModalBottomSheetState {
return remember(
initialValue, animationSpec, skipHalfExpanded, confirmStateChange
) {
ModalBottomSheetState(
initialValue = initialValue,
animationSpec = animationSpec,
isSkipHalfExpanded = skipHalfExpanded,
confirmStateChange = confirmStateChange
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment