diff --git a/app/src/main/java/com/pixelized/biblib/ui/composable/scaffold/BottomSheetHelpers.kt b/app/src/main/java/com/pixelized/biblib/ui/composable/scaffold/BottomSheetHelpers.kt new file mode 100644 index 0000000..47d09f8 --- /dev/null +++ b/app/src/main/java/com/pixelized/biblib/ui/composable/scaffold/BottomSheetHelpers.kt @@ -0,0 +1,45 @@ +package com.pixelized.biblib.ui.composable.scaffold + +import androidx.compose.material.ExperimentalMaterialApi +import androidx.compose.material.ModalBottomSheetState +import androidx.compose.material.ModalBottomSheetValue +import androidx.compose.runtime.* + +@Composable +fun HandleBottomSheetData( + bottomSheetData: Any?, + shouldShow: suspend () -> Unit, + shouldDismiss: suspend () -> Unit, +) { + val currentShouldShow by rememberUpdatedState( + newValue = shouldShow + ) + val currentShouldDismiss by rememberUpdatedState( + newValue = shouldDismiss + ) + LaunchedEffect(bottomSheetData) { + when (bottomSheetData) { + null -> currentShouldDismiss() + else -> currentShouldShow() + } + } +} + +@OptIn(ExperimentalMaterialApi::class) +@Composable +fun HandleBottomSheetDismiss( + bottomSheetState: ModalBottomSheetState, + onDismiss: () -> Unit, +) { + val currentOnDismiss by rememberUpdatedState( + newValue = onDismiss, + ) + val haveBeenDismissed by remember(bottomSheetState) { + derivedStateOf { + bottomSheetState.currentValue != ModalBottomSheetValue.Hidden && bottomSheetState.targetValue == ModalBottomSheetValue.Hidden + } + } + if (haveBeenDismissed) { + currentOnDismiss() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/pixelized/biblib/ui/composable/scaffold/FilterBottomSheet.kt b/app/src/main/java/com/pixelized/biblib/ui/composable/scaffold/FilterBottomSheet.kt index 5ba7e52..4bdbac4 100644 --- a/app/src/main/java/com/pixelized/biblib/ui/composable/scaffold/FilterBottomSheet.kt +++ b/app/src/main/java/com/pixelized/biblib/ui/composable/scaffold/FilterBottomSheet.kt @@ -3,12 +3,8 @@ package com.pixelized.biblib.ui.composable.scaffold import androidx.activity.compose.BackHandler import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.height -import androidx.compose.material.ExperimentalMaterialApi -import androidx.compose.material.ModalBottomSheetLayout -import androidx.compose.material.ModalBottomSheetState -import androidx.compose.material.ModalBottomSheetValue.Expanded +import androidx.compose.material.* import androidx.compose.material.ModalBottomSheetValue.Hidden -import androidx.compose.material.rememberModalBottomSheetState import androidx.compose.runtime.* import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier @@ -113,23 +109,23 @@ fun FilterBottomSheet( val currentBottomSheetData = sheetState.currentBottomSheetData // Check the state of the currentALOBottomSheetData and show / hide the bottomSheet accordingly. - LaunchedEffect(currentBottomSheetData) { - when (currentBottomSheetData) { - null -> { - bottomSheetState.hide() - focusManager.clearFocus(force = true) - keyboard?.hide() - } - else -> { - bottomSheetState.show() - } + HandleBottomSheetData( + bottomSheetData = currentBottomSheetData, + shouldShow = { + bottomSheetState.show() + }, + shouldDismiss = { + focusManager.clearFocus(force = true) + keyboard?.hide() + bottomSheetState.hide() } - } - - if (bottomSheetState.currentValue == Expanded && bottomSheetState.targetValue == Hidden) { - currentBottomSheetData?.dismiss() - } - + ) + // Check the state of the bottomSheetState and call for dismiss if needed. + HandleBottomSheetDismiss( + bottomSheetState = bottomSheetState, + onDismiss = { currentBottomSheetData?.dismiss() }, + ) + // Handle back event. BackHandler( enabled = bottomSheetState.isVisible, onBack = { currentBottomSheetData?.dismiss() }, @@ -177,4 +173,5 @@ fun FilterBottomSheet( }, ) } -} \ No newline at end of file +} + diff --git a/app/src/main/java/com/pixelized/biblib/ui/composable/scaffold/SortBottomSheet.kt b/app/src/main/java/com/pixelized/biblib/ui/composable/scaffold/SortBottomSheet.kt index 6691e56..5d1d6b4 100644 --- a/app/src/main/java/com/pixelized/biblib/ui/composable/scaffold/SortBottomSheet.kt +++ b/app/src/main/java/com/pixelized/biblib/ui/composable/scaffold/SortBottomSheet.kt @@ -92,17 +92,17 @@ fun SortBottomSheet( val currentBottomSheetData = sheetState.currentBottomSheetData // Check the state of the currentALOBottomSheetData and show / hide the bottomSheet accordingly. - LaunchedEffect(currentBottomSheetData) { - when (currentBottomSheetData) { - null -> bottomSheetState.hide() - else -> bottomSheetState.show() - } - } - - if (bottomSheetState.currentValue == Expanded && bottomSheetState.targetValue == Hidden) { - currentBottomSheetData?.dismiss() - } - + HandleBottomSheetData( + bottomSheetData = currentBottomSheetData, + shouldShow = { bottomSheetState.show() }, + shouldDismiss = { bottomSheetState.hide() }, + ) + // Check the state of the bottomSheetState and call for dismiss if needed. + HandleBottomSheetDismiss( + bottomSheetState =bottomSheetState, + onDismiss = { currentBottomSheetData?.dismiss() } + ) + // Handle back event. BackHandler( enabled = bottomSheetState.isVisible, onBack = { currentBottomSheetData?.dismiss() }, diff --git a/app/src/main/java/com/pixelized/biblib/ui/screen/home/HomeScreen.kt b/app/src/main/java/com/pixelized/biblib/ui/screen/home/HomeScreen.kt index 37fa6a5..a344084 100644 --- a/app/src/main/java/com/pixelized/biblib/ui/screen/home/HomeScreen.kt +++ b/app/src/main/java/com/pixelized/biblib/ui/screen/home/HomeScreen.kt @@ -46,7 +46,9 @@ import com.pixelized.biblib.ui.composable.scaffold.* import com.pixelized.biblib.ui.navigation.LocalScreenNavHostController import com.pixelized.biblib.ui.navigation.navigateToProfile import com.pixelized.biblib.ui.screen.home.common.item.* -import com.pixelized.biblib.ui.screen.home.filter.* +import com.pixelized.biblib.ui.screen.home.filter.FilterChip +import com.pixelized.biblib.ui.screen.home.filter.FilterChipUio +import com.pixelized.biblib.ui.screen.home.filter.filterPreview import com.pixelized.biblib.ui.screen.home.header.LazyGridCollapsingHeaderLayout import com.pixelized.biblib.ui.screen.home.options.Options import com.pixelized.biblib.ui.screen.home.options.OptionsUio @@ -126,6 +128,10 @@ fun HomeScreen( } }, onSort = { + // close the keyboard. + focus.clearFocus(force = true) + keyboard?.hide() + // open the sorting bottom sheet. scope.launch { val result = sortingState.show(sortBy = bookViewModel.sortBy) if (result is SortBottomSheetResult.ActionPerformed) { @@ -149,8 +155,10 @@ fun HomeScreen( microListState = microListState, microList = bookViewModel.microPaging, onBook = { + // close the keyboard. focus.clearFocus(force = true) keyboard?.hide() + // opent the detail bottom sheet. scope.launch { detailState.expandBookDetail(id = it) } diff --git a/app/src/main/java/com/pixelized/biblib/ui/screen/home/filter/viewModel/SeriesFilterViewModel.kt b/app/src/main/java/com/pixelized/biblib/ui/screen/home/filter/viewModel/SeriesFilterViewModel.kt index d44c550..550b979 100644 --- a/app/src/main/java/com/pixelized/biblib/ui/screen/home/filter/viewModel/SeriesFilterViewModel.kt +++ b/app/src/main/java/com/pixelized/biblib/ui/screen/home/filter/viewModel/SeriesFilterViewModel.kt @@ -26,7 +26,7 @@ class SeriesFilterViewModel @Inject constructor( private val searchRepository: ISearchRepository, ) : ViewModel(), IFilterViewModel { - override val title: String = application.getString(R.string.search_filter_genre) + override val title: String = application.getString(R.string.search_filter_series) private var source: SeriesSearchSource? = null override val paging: Flow>