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

View File

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