From 883fb40e175b31160b929188ef227b063e2b2ffb Mon Sep 17 00:00:00 2001 From: mimai-hen <123109475+mimai-hen@users.noreply.github.com> Date: Tue, 24 Sep 2024 22:19:49 +0200 Subject: [PATCH] BottomSheetHost custom properties (#51) * BottomSheetHost custom properties * BottomSheetHost custom properties - fix lint issues --------- Co-authored-by: Mari-Mai Henaff --- .gitignore | 2 + .../navigation/bottomsheet/BottomSheetHost.kt | 71 ++++++++++++++++--- build.gradle.kts | 1 - .../dev/hrach/navigation/demo/NavHost.kt | 13 +++- 4 files changed, 77 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index e4e18cd..06500f5 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ .externalNativeBuild .cxx local.properties +.kotlin +.idea diff --git a/bottomsheet/src/main/kotlin/dev/hrach/navigation/bottomsheet/BottomSheetHost.kt b/bottomsheet/src/main/kotlin/dev/hrach/navigation/bottomsheet/BottomSheetHost.kt index e6dfe7d..262e163 100644 --- a/bottomsheet/src/main/kotlin/dev/hrach/navigation/bottomsheet/BottomSheetHost.kt +++ b/bottomsheet/src/main/kotlin/dev/hrach/navigation/bottomsheet/BottomSheetHost.kt @@ -1,10 +1,12 @@ package dev.hrach.navigation.bottomsheet import androidx.compose.foundation.layout.ExperimentalLayoutApi +import androidx.compose.material3.BottomSheetDefaults import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ModalBottomSheet import androidx.compose.material3.ModalBottomSheetProperties import androidx.compose.material3.SheetState +import androidx.compose.material3.contentColorFor import androidx.compose.material3.rememberModalBottomSheetState import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect @@ -16,6 +18,10 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.SaveableStateHolder import androidx.compose.runtime.saveable.rememberSaveableStateHolder import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.Shape +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.navigation.NavBackStackEntry import androidx.navigation.compose.LocalOwnersProvider @@ -23,10 +29,20 @@ import kotlinx.coroutines.CancellationException import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow +@OptIn(ExperimentalMaterial3Api::class) @Composable public fun BottomSheetHost( navigator: BottomSheetNavigator, modifier: Modifier = Modifier, + sheetMaxWidth: Dp = BottomSheetDefaults.SheetMaxWidth, + shape: Shape = BottomSheetDefaults.ExpandedShape, + containerColor: Color = BottomSheetDefaults.ContainerColor, + contentColor: Color = contentColorFor(containerColor), + tonalElevation: Dp = 0.dp, + scrimColor: Color = BottomSheetDefaults.ScrimColor, + dragHandle: + @Composable() + (() -> Unit)? = { BottomSheetDefaults.DragHandle() }, ) { val saveableStateHolder = rememberSaveableStateHolder() @@ -47,9 +63,16 @@ public fun BottomSheetHost( val backStackEntry = sheetBackStackState.second.getOrNull(i) BottomSheetHost( navigator = navigator, + modifier = modifier, + sheetMaxWidth = sheetMaxWidth, + shape = shape, + containerColor = containerColor, + contentColor = contentColor, + tonalElevation = tonalElevation, + scrimColor = scrimColor, + dragHandle = dragHandle, saveableStateHolder = saveableStateHolder, targetBackStackEntry = backStackEntry, - modifier = modifier, ) } } @@ -67,9 +90,18 @@ private fun Flow>.zipWithPreviousCount(): Flow Unit)?, saveableStateHolder: SaveableStateHolder, targetBackStackEntry: NavBackStackEntry?, - modifier: Modifier = Modifier, ) { val destination = targetBackStackEntry?.destination as? BottomSheetNavigator.Destination val sheetState = rememberModalBottomSheetState( @@ -90,22 +122,38 @@ private fun BottomSheetHost( } BottomSheetHost( - sheetState = sheetState, navigator = navigator, + modifier = modifier, + sheetMaxWidth = sheetMaxWidth, + shape = shape, + containerColor = containerColor, + contentColor = contentColor, + tonalElevation = tonalElevation, + scrimColor = scrimColor, + dragHandle = dragHandle, + sheetState = sheetState, saveableStateHolder = saveableStateHolder, backStackEntry = backStackEntry ?: return, - modifier = modifier, ) } @OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class) @Composable private fun BottomSheetHost( - sheetState: SheetState, navigator: BottomSheetNavigator, + modifier: Modifier = Modifier, + sheetMaxWidth: Dp, + shape: Shape, + containerColor: Color, + contentColor: Color, + tonalElevation: Dp, + scrimColor: Color, + dragHandle: + @Composable() + (() -> Unit)?, + sheetState: SheetState, saveableStateHolder: SaveableStateHolder, backStackEntry: NavBackStackEntry, - modifier: Modifier = Modifier, ) { LaunchedEffect(backStackEntry) { sheetState.show() @@ -115,10 +163,17 @@ private fun BottomSheetHost( val destination = backStackEntry.destination as BottomSheetNavigator.Destination ModalBottomSheet( - sheetState = sheetState, - properties = ModalBottomSheetProperties(securePolicy = destination.securePolicy), onDismissRequest = { navigator.dismiss(backStackEntry) }, modifier = modifier, + sheetState = sheetState, + sheetMaxWidth = sheetMaxWidth, + shape = shape, + containerColor = containerColor, + contentColor = contentColor, + tonalElevation = tonalElevation, + scrimColor = scrimColor, + dragHandle = dragHandle, + properties = ModalBottomSheetProperties(securePolicy = destination.securePolicy), ) { LaunchedEffect(backStackEntry) { navigator.onTransitionComplete(backStackEntry) diff --git a/build.gradle.kts b/build.gradle.kts index 8f27302..e9b1a1f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,4 @@ import java.util.Properties -import kotlin.math.sign import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile diff --git a/demo/src/main/kotlin/dev/hrach/navigation/demo/NavHost.kt b/demo/src/main/kotlin/dev/hrach/navigation/demo/NavHost.kt index 5b934e2..b34c7ba 100644 --- a/demo/src/main/kotlin/dev/hrach/navigation/demo/NavHost.kt +++ b/demo/src/main/kotlin/dev/hrach/navigation/demo/NavHost.kt @@ -1,7 +1,10 @@ package dev.hrach.navigation.demo +import androidx.compose.foundation.shape.CornerSize +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.Composable +import androidx.compose.ui.unit.dp import androidx.navigation.NavHostController import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable @@ -36,5 +39,13 @@ internal fun NavHost( bottomSheet { BottomSheet(navController) } } ModalSheetHost(modalSheetNavigator, containerColor = MaterialTheme.colorScheme.background) - BottomSheetHost(bottomSheetNavigator) + BottomSheetHost( + bottomSheetNavigator, + shape = RoundedCornerShape( // optional, just an example of bottom sheet custom property + topStart = CornerSize(12.dp), + topEnd = CornerSize(12.dp), + bottomStart = CornerSize(0.dp), + bottomEnd = CornerSize(0.dp), + ), + ) }