Clear focus after equal click

#165
This commit is contained in:
Sad Ellie 2024-01-19 00:30:36 +03:00
parent 9f8c5b8c36
commit c1dce24645
2 changed files with 16 additions and 13 deletions

View File

@ -285,7 +285,7 @@ private fun Ready(
clearSymbols = clearSymbols,
deleteSymbol = deleteSymbol,
toggleAngleMode = toggleAngleMode,
equal = equal,
equal = { focusManager.clearFocus(); equal() },
middleZero = uiState.middleZero,
acButton = uiState.acButton,
addBracket = addBracket

View File

@ -116,16 +116,16 @@ internal class CalculatorViewModel @Inject constructor(
.stateIn(viewModelScope, CalculatorUIState.Loading)
fun addTokens(tokens: String) = _input.update {
var newValue = if (_equalClicked.value and Token.Digit.allWithDot.contains(tokens)) {
val isClearInputNeeded = _equalClicked.value and Token.Digit.allWithDot.contains(tokens)
var newValue = when {
// Clean input after clicking "=" and any token that is a Digit
TextFieldValue().addTokens(tokens)
} else {
it.addTokens(tokens)
}
// Cursor is set to 0 when equal is clicked
if (_equalClicked.value) {
newValue = newValue.copy(selection = TextRange(it.text.length))
isClearInputNeeded -> TextFieldValue()
_equalClicked.value -> it.copy(selection = TextRange(it.text.length))
else -> it
}
newValue = newValue.addTokens(tokens)
_equalClicked.update { false }
_fractionJob?.cancel()
savedStateHandle[_inputKey] = newValue.text
@ -133,11 +133,14 @@ internal class CalculatorViewModel @Inject constructor(
}
fun addBracket() = _input.update {
var newValue = it.addBracket()
// Cursor is set to 0 when equal is clicked
if (_equalClicked.value) {
newValue = newValue.copy(selection = TextRange(it.text.length))
var newValue = if (_equalClicked.value) {
// Cursor is set to 0 when equal is clicked
it.copy(selection = TextRange(it.text.length))
} else {
it
}
newValue = newValue.addBracket()
_equalClicked.update { false }
_fractionJob?.cancel()
savedStateHandle[_inputKey] = newValue.text