Fix keyboard for AddSubtractPage

This commit is contained in:
Sad Ellie 2023-09-19 10:55:54 +03:00
parent 06c9224cbb
commit d8a41de848
2 changed files with 138 additions and 123 deletions

View File

@ -64,6 +64,7 @@ internal fun DateCalculatorScreen(
val differenceLabel = stringResource(R.string.difference) val differenceLabel = stringResource(R.string.difference)
val focusManager = LocalFocusManager.current val focusManager = LocalFocusManager.current
var topBarShown by remember { mutableStateOf(true) } var topBarShown by remember { mutableStateOf(true) }
var showKeyboard by remember { mutableStateOf(false) }
val allTabs = remember { mutableListOf(addSubtractLabel, differenceLabel) } val allTabs = remember { mutableListOf(addSubtractLabel, differenceLabel) }
val pagerState = rememberPagerState { allTabs.size } val pagerState = rememberPagerState { allTabs.size }
@ -101,11 +102,14 @@ internal fun DateCalculatorScreen(
) { page -> ) { page ->
when (page) { when (page) {
0 -> AddSubtractPage( 0 -> AddSubtractPage(
toggleTopBar = { topBarShown = it } toggleTopBar = { topBarShown = it },
showKeyboard = showKeyboard,
toggleKeyboard = {showKeyboard = it }
) )
1 -> { 1 -> {
focusManager.clearFocus(true) focusManager.clearFocus(true)
topBarShown = true topBarShown = true
showKeyboard = false
DateDifferencePage() DateDifferencePage()
} }

View File

@ -34,12 +34,13 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Event import androidx.compose.material.icons.filled.Event
import androidx.compose.material.icons.outlined.Add import androidx.compose.material.icons.outlined.Add
@ -83,12 +84,16 @@ import java.time.ZonedDateTime
internal fun AddSubtractPage( internal fun AddSubtractPage(
viewModel: AddSubtractViewModel = hiltViewModel(), viewModel: AddSubtractViewModel = hiltViewModel(),
toggleTopBar: (Boolean) -> Unit, toggleTopBar: (Boolean) -> Unit,
showKeyboard: Boolean,
toggleKeyboard: (Boolean) -> Unit,
) { ) {
val uiState = viewModel.uiState.collectAsStateWithLifecycle().value val uiState = viewModel.uiState.collectAsStateWithLifecycle().value
AddSubtractView( AddSubtractView(
uiState = uiState, uiState = uiState,
toggleTopBar = toggleTopBar, toggleTopBar = toggleTopBar,
showKeyboard = showKeyboard,
toggleKeyboard = toggleKeyboard,
updateStart = viewModel::updateStart, updateStart = viewModel::updateStart,
updateYears = viewModel::updateYears, updateYears = viewModel::updateYears,
updateMonths = viewModel::updateMonths, updateMonths = viewModel::updateMonths,
@ -105,6 +110,8 @@ internal fun AddSubtractPage(
private fun AddSubtractView( private fun AddSubtractView(
uiState: AddSubtractState, uiState: AddSubtractState,
toggleTopBar: (Boolean) -> Unit, toggleTopBar: (Boolean) -> Unit,
showKeyboard: Boolean,
toggleKeyboard: (Boolean) -> Unit,
updateStart: (ZonedDateTime) -> Unit, updateStart: (ZonedDateTime) -> Unit,
updateYears: (TextFieldValue) -> Unit, updateYears: (TextFieldValue) -> Unit,
updateMonths: (TextFieldValue) -> Unit, updateMonths: (TextFieldValue) -> Unit,
@ -113,18 +120,22 @@ private fun AddSubtractView(
updateMinutes: (TextFieldValue) -> Unit, updateMinutes: (TextFieldValue) -> Unit,
updateAddition: (Boolean) -> Unit, updateAddition: (Boolean) -> Unit,
) { ) {
var dialogState by remember { mutableStateOf(DialogState.NONE) }
val mContext = LocalContext.current val mContext = LocalContext.current
val focusManager = LocalFocusManager.current
val landscape = !isPortrait()
var dialogState by remember { mutableStateOf(DialogState.NONE) }
var addSymbol: ((TextFieldValue) -> Unit)? by remember { mutableStateOf(null) } var addSymbol: ((TextFieldValue) -> Unit)? by remember { mutableStateOf(null) }
var focusedTextFieldValue: TextFieldValue? by remember { mutableStateOf(null) } var focusedTextFieldValue: TextFieldValue? by remember { mutableStateOf(null) }
val showKeyboard = (addSymbol != null) and (focusedTextFieldValue != null)
val landscape = !isPortrait()
val focusManager = LocalFocusManager.current
LaunchedEffect(showKeyboard, landscape) { LaunchedEffect(showKeyboard, landscape) {
toggleTopBar(!(showKeyboard and landscape)) toggleTopBar(!(showKeyboard and landscape))
} }
LaunchedEffect(addSymbol, focusedTextFieldValue) {
toggleKeyboard((addSymbol != null) and (focusedTextFieldValue != null))
}
BackHandler(showKeyboard) { BackHandler(showKeyboard) {
focusManager.clearFocus() focusManager.clearFocus()
addSymbol = null addSymbol = null
@ -147,16 +158,17 @@ private fun AddSubtractView(
contentDescription = null, contentDescription = null,
) )
} }
} },
contentWindowInsets = WindowInsets(0,0,0,0)
) { ) {
LazyColumn( Column(
modifier = Modifier.padding(horizontal = 16.dp), modifier = Modifier
verticalArrangement = Arrangement.spacedBy(16.dp), .verticalScroll(rememberScrollState())
contentPadding = PaddingValues(top = 16.dp) .padding(horizontal = 16.dp),
verticalArrangement = Arrangement.spacedBy(2.dp),
) { ) {
item("dates") {
FlowRow( FlowRow(
modifier = Modifier, modifier = Modifier.padding(vertical = 16.dp),
maxItemsInEachRow = 2, maxItemsInEachRow = 2,
verticalArrangement = Arrangement.spacedBy(16.dp), verticalArrangement = Arrangement.spacedBy(16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp) horizontalArrangement = Arrangement.spacedBy(16.dp)
@ -181,9 +193,7 @@ private fun AddSubtractView(
dateTime = uiState.result, dateTime = uiState.result,
) )
} }
}
item("modes") {
SingleChoiceSegmentedButtonRow( SingleChoiceSegmentedButtonRow(
modifier = Modifier modifier = Modifier
.fillMaxWidth(), .fillMaxWidth(),
@ -205,10 +215,7 @@ private fun AddSubtractView(
Icon(Icons.Outlined.Remove, null) Icon(Icons.Outlined.Remove, null)
} }
} }
}
item("textFields") {
Column {
TimeUnitTextField( TimeUnitTextField(
modifier = Modifier.onFocusEvent { modifier = Modifier.onFocusEvent {
if (it.hasFocus) { if (it.hasFocus) {
@ -221,6 +228,7 @@ private fun AddSubtractView(
label = stringResource(R.string.date_difference_years), label = stringResource(R.string.date_difference_years),
formatterSymbols = uiState.formatterSymbols formatterSymbols = uiState.formatterSymbols
) )
TimeUnitTextField( TimeUnitTextField(
modifier = Modifier.onFocusEvent { modifier = Modifier.onFocusEvent {
if (it.hasFocus) { if (it.hasFocus) {
@ -233,6 +241,7 @@ private fun AddSubtractView(
label = stringResource(R.string.date_difference_months), label = stringResource(R.string.date_difference_months),
formatterSymbols = uiState.formatterSymbols formatterSymbols = uiState.formatterSymbols
) )
TimeUnitTextField( TimeUnitTextField(
modifier = Modifier.onFocusEvent { modifier = Modifier.onFocusEvent {
if (it.hasFocus) { if (it.hasFocus) {
@ -245,6 +254,7 @@ private fun AddSubtractView(
label = stringResource(R.string.date_difference_days), label = stringResource(R.string.date_difference_days),
formatterSymbols = uiState.formatterSymbols formatterSymbols = uiState.formatterSymbols
) )
TimeUnitTextField( TimeUnitTextField(
modifier = Modifier.onFocusEvent { modifier = Modifier.onFocusEvent {
if (it.hasFocus) { if (it.hasFocus) {
@ -257,6 +267,7 @@ private fun AddSubtractView(
label = stringResource(R.string.date_difference_hours), label = stringResource(R.string.date_difference_hours),
formatterSymbols = uiState.formatterSymbols formatterSymbols = uiState.formatterSymbols
) )
TimeUnitTextField( TimeUnitTextField(
modifier = Modifier.onFocusEvent { modifier = Modifier.onFocusEvent {
if (it.hasFocus) { if (it.hasFocus) {
@ -271,8 +282,6 @@ private fun AddSubtractView(
) )
} }
} }
}
}
AnimatedVisibility( AnimatedVisibility(
visible = showKeyboard, visible = showKeyboard,
enter = slideInVertically { it / 2 } + fadeIn(), enter = slideInVertically { it / 2 } + fadeIn(),
@ -343,6 +352,8 @@ fun AddSubtractViewPreview() {
years = TextFieldValue("12") years = TextFieldValue("12")
), ),
toggleTopBar = {}, toggleTopBar = {},
showKeyboard = false,
toggleKeyboard = {},
updateStart = {}, updateStart = {},
updateYears = {}, updateYears = {},
updateMonths = {}, updateMonths = {},