Fix keyboard management.

This commit is contained in:
Thomas Andres Gomez 2023-03-31 11:20:17 +02:00
parent 7298ab0958
commit 8c264042d0
5 changed files with 85 additions and 35 deletions

View file

@ -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()
}
}

View file

@ -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()
HandleBottomSheetData(
bottomSheetData = currentBottomSheetData,
shouldShow = {
bottomSheetState.show()
},
shouldDismiss = {
focusManager.clearFocus(force = true)
keyboard?.hide()
bottomSheetState.hide()
}
else -> {
bottomSheetState.show()
}
}
}
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() },
@ -178,3 +174,4 @@ fun FilterBottomSheet(
)
}
}

View file

@ -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() },

View file

@ -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)
}

View file

@ -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<PagingData<FilterItemUio>>