mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 00:35:26 +02:00
Fix keyboard for AddSubtractPage
This commit is contained in:
parent
06c9224cbb
commit
d8a41de848
@ -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()
|
||||||
}
|
}
|
||||||
|
@ -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,130 +158,128 @@ 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.padding(vertical = 16.dp),
|
||||||
modifier = Modifier,
|
maxItemsInEachRow = 2,
|
||||||
maxItemsInEachRow = 2,
|
verticalArrangement = Arrangement.spacedBy(16.dp),
|
||||||
verticalArrangement = Arrangement.spacedBy(16.dp),
|
horizontalArrangement = Arrangement.spacedBy(16.dp)
|
||||||
horizontalArrangement = Arrangement.spacedBy(16.dp)
|
) {
|
||||||
) {
|
DateTimeSelectorBlock(
|
||||||
DateTimeSelectorBlock(
|
|
||||||
modifier = Modifier
|
|
||||||
.weight(1f)
|
|
||||||
.fillMaxWidth(),
|
|
||||||
title = stringResource(R.string.date_difference_start),
|
|
||||||
dateTime = uiState.start,
|
|
||||||
onLongClick = { updateStart(ZonedDateTime.now()) },
|
|
||||||
onClick = { dialogState = DialogState.FROM },
|
|
||||||
onTimeClick = { dialogState = DialogState.FROM_TIME },
|
|
||||||
onDateClick = { dialogState = DialogState.FROM_DATE },
|
|
||||||
)
|
|
||||||
|
|
||||||
DateTimeSelectorBlock(
|
|
||||||
modifier = Modifier
|
|
||||||
.weight(1f)
|
|
||||||
.fillMaxWidth(),
|
|
||||||
title = stringResource(R.string.date_difference_end),
|
|
||||||
dateTime = uiState.result,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
item("modes") {
|
|
||||||
SingleChoiceSegmentedButtonRow(
|
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
|
.weight(1f)
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth(),
|
||||||
|
title = stringResource(R.string.date_difference_start),
|
||||||
|
dateTime = uiState.start,
|
||||||
|
onLongClick = { updateStart(ZonedDateTime.now()) },
|
||||||
|
onClick = { dialogState = DialogState.FROM },
|
||||||
|
onTimeClick = { dialogState = DialogState.FROM_TIME },
|
||||||
|
onDateClick = { dialogState = DialogState.FROM_DATE },
|
||||||
|
)
|
||||||
|
|
||||||
|
DateTimeSelectorBlock(
|
||||||
|
modifier = Modifier
|
||||||
|
.weight(1f)
|
||||||
|
.fillMaxWidth(),
|
||||||
|
title = stringResource(R.string.date_difference_end),
|
||||||
|
dateTime = uiState.result,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
SingleChoiceSegmentedButtonRow(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth(),
|
||||||
|
) {
|
||||||
|
SegmentedButton(
|
||||||
|
selected = uiState.addition,
|
||||||
|
onClick = { updateAddition(true) },
|
||||||
|
shape = SegmentedButtonDefaults.itemShape(index = 0, count = 2),
|
||||||
|
icon = {}
|
||||||
) {
|
) {
|
||||||
SegmentedButton(
|
Icon(Icons.Outlined.Add, null)
|
||||||
selected = uiState.addition,
|
}
|
||||||
onClick = { updateAddition(true) },
|
SegmentedButton(
|
||||||
shape = SegmentedButtonDefaults.itemShape(index = 0, count = 2),
|
selected = !uiState.addition,
|
||||||
icon = {}
|
onClick = { updateAddition(false) },
|
||||||
) {
|
shape = SegmentedButtonDefaults.itemShape(index = 1, count = 2),
|
||||||
Icon(Icons.Outlined.Add, null)
|
icon = {}
|
||||||
}
|
) {
|
||||||
SegmentedButton(
|
Icon(Icons.Outlined.Remove, null)
|
||||||
selected = !uiState.addition,
|
|
||||||
onClick = { updateAddition(false) },
|
|
||||||
shape = SegmentedButtonDefaults.itemShape(index = 1, count = 2),
|
|
||||||
icon = {}
|
|
||||||
) {
|
|
||||||
Icon(Icons.Outlined.Remove, null)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
item("textFields") {
|
TimeUnitTextField(
|
||||||
Column {
|
modifier = Modifier.onFocusEvent {
|
||||||
TimeUnitTextField(
|
if (it.hasFocus) {
|
||||||
modifier = Modifier.onFocusEvent {
|
addSymbol = updateYears
|
||||||
if (it.hasFocus) {
|
focusedTextFieldValue = uiState.years
|
||||||
addSymbol = updateYears
|
}
|
||||||
focusedTextFieldValue = uiState.years
|
},
|
||||||
}
|
value = uiState.years,
|
||||||
},
|
onValueChange = updateYears,
|
||||||
value = uiState.years,
|
label = stringResource(R.string.date_difference_years),
|
||||||
onValueChange = updateYears,
|
formatterSymbols = uiState.formatterSymbols
|
||||||
label = stringResource(R.string.date_difference_years),
|
)
|
||||||
formatterSymbols = uiState.formatterSymbols
|
|
||||||
)
|
TimeUnitTextField(
|
||||||
TimeUnitTextField(
|
modifier = Modifier.onFocusEvent {
|
||||||
modifier = Modifier.onFocusEvent {
|
if (it.hasFocus) {
|
||||||
if (it.hasFocus) {
|
addSymbol = updateMonths
|
||||||
addSymbol = updateMonths
|
focusedTextFieldValue = uiState.months
|
||||||
focusedTextFieldValue = uiState.months
|
}
|
||||||
}
|
},
|
||||||
},
|
value = uiState.months,
|
||||||
value = uiState.months,
|
onValueChange = updateMonths,
|
||||||
onValueChange = updateMonths,
|
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) {
|
||||||
addSymbol = updateDays
|
addSymbol = updateDays
|
||||||
focusedTextFieldValue = uiState.days
|
focusedTextFieldValue = uiState.days
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
value = uiState.days,
|
value = uiState.days,
|
||||||
onValueChange = updateDays,
|
onValueChange = updateDays,
|
||||||
label = stringResource(R.string.date_difference_days),
|
label = stringResource(R.string.date_difference_days),
|
||||||
formatterSymbols = uiState.formatterSymbols
|
formatterSymbols = uiState.formatterSymbols
|
||||||
)
|
)
|
||||||
TimeUnitTextField(
|
|
||||||
modifier = Modifier.onFocusEvent {
|
TimeUnitTextField(
|
||||||
if (it.hasFocus) {
|
modifier = Modifier.onFocusEvent {
|
||||||
addSymbol = updateHours
|
if (it.hasFocus) {
|
||||||
focusedTextFieldValue = uiState.hours
|
addSymbol = updateHours
|
||||||
}
|
focusedTextFieldValue = uiState.hours
|
||||||
},
|
}
|
||||||
value = uiState.hours,
|
},
|
||||||
onValueChange = updateHours,
|
value = uiState.hours,
|
||||||
label = stringResource(R.string.date_difference_hours),
|
onValueChange = updateHours,
|
||||||
formatterSymbols = uiState.formatterSymbols
|
label = stringResource(R.string.date_difference_hours),
|
||||||
)
|
formatterSymbols = uiState.formatterSymbols
|
||||||
TimeUnitTextField(
|
)
|
||||||
modifier = Modifier.onFocusEvent {
|
|
||||||
if (it.hasFocus) {
|
TimeUnitTextField(
|
||||||
addSymbol = updateMinutes
|
modifier = Modifier.onFocusEvent {
|
||||||
focusedTextFieldValue = uiState.minutes
|
if (it.hasFocus) {
|
||||||
}
|
addSymbol = updateMinutes
|
||||||
},
|
focusedTextFieldValue = uiState.minutes
|
||||||
value = uiState.minutes,
|
}
|
||||||
onValueChange = updateMinutes,
|
},
|
||||||
label = stringResource(R.string.date_difference_minutes),
|
value = uiState.minutes,
|
||||||
formatterSymbols = uiState.formatterSymbols
|
onValueChange = updateMinutes,
|
||||||
)
|
label = stringResource(R.string.date_difference_minutes),
|
||||||
}
|
formatterSymbols = uiState.formatterSymbols
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AnimatedVisibility(
|
AnimatedVisibility(
|
||||||
@ -343,6 +352,8 @@ fun AddSubtractViewPreview() {
|
|||||||
years = TextFieldValue("12")
|
years = TextFieldValue("12")
|
||||||
),
|
),
|
||||||
toggleTopBar = {},
|
toggleTopBar = {},
|
||||||
|
showKeyboard = false,
|
||||||
|
toggleKeyboard = {},
|
||||||
updateStart = {},
|
updateStart = {},
|
||||||
updateYears = {},
|
updateYears = {},
|
||||||
updateMonths = {},
|
updateMonths = {},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user