diff --git a/calf-ui/src/commonMain/kotlin/com/mohamedrejeb/calf/ui/sheet/AdaptiveSheetState.kt b/calf-ui/src/commonMain/kotlin/com/mohamedrejeb/calf/ui/sheet/AdaptiveSheetState.kt index 6e9fdcf..a7c5dbc 100644 --- a/calf-ui/src/commonMain/kotlin/com/mohamedrejeb/calf/ui/sheet/AdaptiveSheetState.kt +++ b/calf-ui/src/commonMain/kotlin/com/mohamedrejeb/calf/ui/sheet/AdaptiveSheetState.kt @@ -3,26 +3,49 @@ package com.mohamedrejeb.calf.ui.sheet import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.SheetValue import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.Stable +import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.Saver import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.snapshotFlow import kotlinx.coroutines.CancellationException +private class SheetValueHolder @OptIn(ExperimentalMaterial3Api::class) constructor( + var value: SheetValue +) + @OptIn(ExperimentalMaterial3Api::class) @Composable fun rememberAdaptiveSheetState( skipPartiallyExpanded: Boolean = false, confirmValueChange: (SheetValue) -> Boolean = { true }, ): AdaptiveSheetState { - return rememberSaveable( - skipPartiallyExpanded, confirmValueChange, + val sheetValueHolder = remember { + SheetValueHolder(SheetValue.Hidden) + } + + val state = rememberSaveable( + skipPartiallyExpanded, + confirmValueChange, saver = AdaptiveSheetState.Saver( skipPartiallyExpanded = skipPartiallyExpanded, confirmValueChange = confirmValueChange ) ) { - AdaptiveSheetState(skipPartiallyExpanded, SheetValue.Hidden, confirmValueChange) + if (skipPartiallyExpanded && sheetValueHolder.value == SheetValue.PartiallyExpanded) + sheetValueHolder.value = SheetValue.Expanded + + AdaptiveSheetState(skipPartiallyExpanded, sheetValueHolder.value, confirmValueChange) } + + + LaunchedEffect(Unit) { + snapshotFlow { state.currentValue } + .collect { sheetValueHolder.value = it } + } + + return state } @OptIn(ExperimentalMaterial3Api::class)