mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 08:45:27 +02:00
parent
f88fba44bb
commit
caa2979d42
@ -116,12 +116,16 @@ internal class CalculatorViewModel @Inject constructor(
|
|||||||
.stateIn(viewModelScope, CalculatorUIState.Loading)
|
.stateIn(viewModelScope, CalculatorUIState.Loading)
|
||||||
|
|
||||||
fun addTokens(tokens: String) = _input.update {
|
fun addTokens(tokens: String) = _input.update {
|
||||||
val newValue = if (_equalClicked.value and Token.Digit.allWithDot.contains(tokens)) {
|
var newValue = if (_equalClicked.value and Token.Digit.allWithDot.contains(tokens)) {
|
||||||
// Clean input after clicking "=" and any token that is a Digit
|
// Clean input after clicking "=" and any token that is a Digit
|
||||||
TextFieldValue().addTokens(tokens)
|
TextFieldValue().addTokens(tokens)
|
||||||
} else {
|
} else {
|
||||||
it.addTokens(tokens)
|
it.addTokens(tokens)
|
||||||
}
|
}
|
||||||
|
// Cursor is set to 0 when equal is clicked
|
||||||
|
if (_equalClicked.value) {
|
||||||
|
newValue = newValue.copy(selection = TextRange(it.text.length))
|
||||||
|
}
|
||||||
_equalClicked.update { false }
|
_equalClicked.update { false }
|
||||||
_fractionJob?.cancel()
|
_fractionJob?.cancel()
|
||||||
savedStateHandle[_inputKey] = newValue.text
|
savedStateHandle[_inputKey] = newValue.text
|
||||||
@ -129,7 +133,11 @@ internal class CalculatorViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun addBracket() = _input.update {
|
fun addBracket() = _input.update {
|
||||||
val newValue = it.addBracket()
|
var newValue = it.addBracket()
|
||||||
|
// Cursor is set to 0 when equal is clicked
|
||||||
|
if (_equalClicked.value) {
|
||||||
|
newValue = newValue.copy(selection = TextRange(it.text.length))
|
||||||
|
}
|
||||||
_equalClicked.update { false }
|
_equalClicked.update { false }
|
||||||
_fractionJob?.cancel()
|
_fractionJob?.cancel()
|
||||||
savedStateHandle[_inputKey] = newValue.text
|
savedStateHandle[_inputKey] = newValue.text
|
||||||
@ -138,7 +146,7 @@ internal class CalculatorViewModel @Inject constructor(
|
|||||||
|
|
||||||
fun deleteTokens() = _input.update {
|
fun deleteTokens() = _input.update {
|
||||||
val newValue = if (_equalClicked.value) {
|
val newValue = if (_equalClicked.value) {
|
||||||
TextFieldValue().deleteTokens()
|
TextFieldValue()
|
||||||
} else {
|
} else {
|
||||||
it.deleteTokens()
|
it.deleteTokens()
|
||||||
}
|
}
|
||||||
@ -155,7 +163,12 @@ internal class CalculatorViewModel @Inject constructor(
|
|||||||
TextFieldValue()
|
TextFieldValue()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onCursorChange(selection: TextRange) = _input.update { it.copy(selection = selection) }
|
fun onCursorChange(selection: TextRange) = _input.update {
|
||||||
|
// Without this line: will place token (even in the middle of the input) and place cursor at
|
||||||
|
// the end. This line also removes fractional output once user touches input text field
|
||||||
|
_equalClicked.update { false }
|
||||||
|
it.copy(selection = selection)
|
||||||
|
}
|
||||||
|
|
||||||
fun updateRadianMode(newValue: Boolean) = viewModelScope.launch {
|
fun updateRadianMode(newValue: Boolean) = viewModelScope.launch {
|
||||||
userPrefsRepository.updateRadianMode(newValue)
|
userPrefsRepository.updateRadianMode(newValue)
|
||||||
@ -203,7 +216,7 @@ internal class CalculatorViewModel @Inject constructor(
|
|||||||
_fractionJob = launch(Dispatchers.Default) {
|
_fractionJob = launch(Dispatchers.Default) {
|
||||||
val fraction = result.toFractionalString()
|
val fraction = result.toFractionalString()
|
||||||
|
|
||||||
_input.update { TextFieldValue(resultFormatted, TextRange(resultFormatted.length)) }
|
_input.update { TextFieldValue(resultFormatted, TextRange.Zero) }
|
||||||
_result.update { CalculationResult.Fraction(fraction) }
|
_result.update { CalculationResult.Fraction(fraction) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user