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.activity.compose.BackHandler
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.material.ExperimentalMaterialApi import androidx.compose.material.*
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetState
import androidx.compose.material.ModalBottomSheetValue.Expanded
import androidx.compose.material.ModalBottomSheetValue.Hidden import androidx.compose.material.ModalBottomSheetValue.Hidden
import androidx.compose.material.rememberModalBottomSheetState
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@ -113,23 +109,23 @@ fun FilterBottomSheet(
val currentBottomSheetData = sheetState.currentBottomSheetData val currentBottomSheetData = sheetState.currentBottomSheetData
// Check the state of the currentALOBottomSheetData and show / hide the bottomSheet accordingly. // Check the state of the currentALOBottomSheetData and show / hide the bottomSheet accordingly.
LaunchedEffect(currentBottomSheetData) { HandleBottomSheetData(
when (currentBottomSheetData) { bottomSheetData = currentBottomSheetData,
null -> { shouldShow = {
bottomSheetState.hide() bottomSheetState.show()
focusManager.clearFocus(force = true) },
keyboard?.hide() shouldDismiss = {
} focusManager.clearFocus(force = true)
else -> { keyboard?.hide()
bottomSheetState.show() bottomSheetState.hide()
}
} }
} )
// Check the state of the bottomSheetState and call for dismiss if needed.
if (bottomSheetState.currentValue == Expanded && bottomSheetState.targetValue == Hidden) { HandleBottomSheetDismiss(
currentBottomSheetData?.dismiss() bottomSheetState = bottomSheetState,
} onDismiss = { currentBottomSheetData?.dismiss() },
)
// Handle back event.
BackHandler( BackHandler(
enabled = bottomSheetState.isVisible, enabled = bottomSheetState.isVisible,
onBack = { currentBottomSheetData?.dismiss() }, onBack = { currentBottomSheetData?.dismiss() },
@ -177,4 +173,5 @@ fun FilterBottomSheet(
}, },
) )
} }
} }

View file

@ -92,17 +92,17 @@ fun SortBottomSheet(
val currentBottomSheetData = sheetState.currentBottomSheetData val currentBottomSheetData = sheetState.currentBottomSheetData
// Check the state of the currentALOBottomSheetData and show / hide the bottomSheet accordingly. // Check the state of the currentALOBottomSheetData and show / hide the bottomSheet accordingly.
LaunchedEffect(currentBottomSheetData) { HandleBottomSheetData(
when (currentBottomSheetData) { bottomSheetData = currentBottomSheetData,
null -> bottomSheetState.hide() shouldShow = { bottomSheetState.show() },
else -> bottomSheetState.show() shouldDismiss = { bottomSheetState.hide() },
} )
} // Check the state of the bottomSheetState and call for dismiss if needed.
HandleBottomSheetDismiss(
if (bottomSheetState.currentValue == Expanded && bottomSheetState.targetValue == Hidden) { bottomSheetState =bottomSheetState,
currentBottomSheetData?.dismiss() onDismiss = { currentBottomSheetData?.dismiss() }
} )
// Handle back event.
BackHandler( BackHandler(
enabled = bottomSheetState.isVisible, enabled = bottomSheetState.isVisible,
onBack = { currentBottomSheetData?.dismiss() }, 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.LocalScreenNavHostController
import com.pixelized.biblib.ui.navigation.navigateToProfile import com.pixelized.biblib.ui.navigation.navigateToProfile
import com.pixelized.biblib.ui.screen.home.common.item.* 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.header.LazyGridCollapsingHeaderLayout
import com.pixelized.biblib.ui.screen.home.options.Options import com.pixelized.biblib.ui.screen.home.options.Options
import com.pixelized.biblib.ui.screen.home.options.OptionsUio import com.pixelized.biblib.ui.screen.home.options.OptionsUio
@ -126,6 +128,10 @@ fun HomeScreen(
} }
}, },
onSort = { onSort = {
// close the keyboard.
focus.clearFocus(force = true)
keyboard?.hide()
// open the sorting bottom sheet.
scope.launch { scope.launch {
val result = sortingState.show(sortBy = bookViewModel.sortBy) val result = sortingState.show(sortBy = bookViewModel.sortBy)
if (result is SortBottomSheetResult.ActionPerformed) { if (result is SortBottomSheetResult.ActionPerformed) {
@ -149,8 +155,10 @@ fun HomeScreen(
microListState = microListState, microListState = microListState,
microList = bookViewModel.microPaging, microList = bookViewModel.microPaging,
onBook = { onBook = {
// close the keyboard.
focus.clearFocus(force = true) focus.clearFocus(force = true)
keyboard?.hide() keyboard?.hide()
// opent the detail bottom sheet.
scope.launch { scope.launch {
detailState.expandBookDetail(id = it) detailState.expandBookDetail(id = it)
} }

View file

@ -26,7 +26,7 @@ class SeriesFilterViewModel @Inject constructor(
private val searchRepository: ISearchRepository, private val searchRepository: ISearchRepository,
) : ViewModel(), IFilterViewModel { ) : 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 private var source: SeriesSearchSource? = null
override val paging: Flow<PagingData<FilterItemUio>> override val paging: Flow<PagingData<FilterItemUio>>