mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 00:35:26 +02:00
Fixed old fix that fixed fix
This commit is contained in:
parent
febcec5201
commit
67b7af080f
@ -74,6 +74,7 @@ import kotlinx.coroutines.isActive
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.math.BigDecimal
|
import java.math.BigDecimal
|
||||||
|
import java.math.RoundingMode
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
@ -394,8 +395,6 @@ class MainViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun convertInput() {
|
private suspend fun convertInput() {
|
||||||
// Loading don't do anything
|
|
||||||
if ((_unitFrom.value == null) or (_unitTo.value == null)) return
|
|
||||||
withContext(Dispatchers.Default) {
|
withContext(Dispatchers.Default) {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
when (_unitFrom.value?.group) {
|
when (_unitFrom.value?.group) {
|
||||||
@ -408,10 +407,14 @@ class MainViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun convertAsNumberBase() {
|
private fun convertAsNumberBase() {
|
||||||
val conversionResult: String = try {
|
// Units are still loading, don't convert anything yet
|
||||||
(_unitFrom.value as NumberBaseUnit).convertToBase(
|
val unitFrom = _unitFrom.value ?: return
|
||||||
|
val unitTo = _unitTo.value ?: return
|
||||||
|
|
||||||
|
val conversionResult = try {
|
||||||
|
(unitFrom as NumberBaseUnit).convertToBase(
|
||||||
input = _input.value,
|
input = _input.value,
|
||||||
toBase = (_unitTo.value as NumberBaseUnit).base
|
toBase = (unitTo as NumberBaseUnit).base
|
||||||
)
|
)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
when (e) {
|
when (e) {
|
||||||
@ -424,11 +427,15 @@ class MainViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun convertAsExpression() {
|
private fun convertAsExpression() {
|
||||||
|
// Units are still loading, don't convert anything yet
|
||||||
|
val unitFrom = _unitFrom.value ?: return
|
||||||
|
val unitTo = _unitTo.value ?: return
|
||||||
|
|
||||||
// First we clean the input from garbage at the end
|
// First we clean the input from garbage at the end
|
||||||
var cleanInput = _input.value.dropLastWhile { !it.isDigit() }
|
var cleanInput = _input.value.dropLastWhile { !it.isDigit() }
|
||||||
|
|
||||||
// Now we close open brackets that user didn't close
|
// Now we close open brackets that user didn't close
|
||||||
// AUTO-CLOSE ALL BRACKETS
|
// AUTOCLOSE ALL BRACKETS
|
||||||
val leftBrackets = _input.value.count { it.toString() == KEY_LEFT_BRACKET }
|
val leftBrackets = _input.value.count { it.toString() == KEY_LEFT_BRACKET }
|
||||||
val rightBrackets = _input.value.count { it.toString() == KEY_RIGHT_BRACKET }
|
val rightBrackets = _input.value.count { it.toString() == KEY_RIGHT_BRACKET }
|
||||||
val neededBrackets = leftBrackets - rightBrackets
|
val neededBrackets = leftBrackets - rightBrackets
|
||||||
@ -437,38 +444,31 @@ class MainViewModel @Inject constructor(
|
|||||||
// Now we evaluate expression in input
|
// Now we evaluate expression in input
|
||||||
val evaluationResult: BigDecimal = try {
|
val evaluationResult: BigDecimal = try {
|
||||||
Expressions().eval(cleanInput)
|
Expressions().eval(cleanInput)
|
||||||
|
.setScale(_userPrefs.value.digitsPrecision, RoundingMode.HALF_EVEN)
|
||||||
|
.trimZeros()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
when (e) {
|
when (e) {
|
||||||
// Invalid expression, don't do anything
|
is ExpressionException,
|
||||||
is ArrayIndexOutOfBoundsException,
|
is ArrayIndexOutOfBoundsException,
|
||||||
is IndexOutOfBoundsException,
|
is IndexOutOfBoundsException,
|
||||||
is ExpressionException -> return
|
is NumberFormatException,
|
||||||
// Divide by zero or SQRT of negative
|
is ArithmeticException -> {
|
||||||
is NumberFormatException, is ArithmeticException -> {
|
// Invalid expression, can't do anything further
|
||||||
_calculated.update { null }
|
|
||||||
_result.update { "" }
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
else -> throw e
|
else -> throw e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we just convert.
|
|
||||||
// We can use evaluation result here, input is valid
|
|
||||||
val conversionResult: BigDecimal = _unitFrom.value!!.convert(
|
|
||||||
_unitTo.value!!,
|
|
||||||
evaluationResult,
|
|
||||||
_userPrefs.value.digitsPrecision
|
|
||||||
)
|
|
||||||
|
|
||||||
// Evaluated. Hide calculated result if no expression entered.
|
// Evaluated. Hide calculated result if no expression entered.
|
||||||
// 123.456 will be true
|
// 123.456 will be true
|
||||||
// -123.456 will be true
|
// -123.456 will be true
|
||||||
// -123.456-123 will be false (first minus gets removed, ending with 123.456)
|
// -123.456-123 will be false (first minus gets removed, ending with 123.456)
|
||||||
_calculated.update {
|
if (_input.value.removePrefix(KEY_MINUS).all { it.toString() !in OPERATORS }) {
|
||||||
if (_input.value.removePrefix(KEY_MINUS).all { it.toString() !in OPERATORS }) {
|
// No operators
|
||||||
null
|
_calculated.update { null }
|
||||||
} else {
|
} else {
|
||||||
|
_calculated.update {
|
||||||
evaluationResult
|
evaluationResult
|
||||||
.setMinimumRequiredScale(_userPrefs.value.digitsPrecision)
|
.setMinimumRequiredScale(_userPrefs.value.digitsPrecision)
|
||||||
.trimZeros()
|
.trimZeros()
|
||||||
@ -476,6 +476,15 @@ class MainViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Now we just convert.
|
||||||
|
// We can use evaluation result here, input is valid.
|
||||||
|
val conversionResult: BigDecimal = unitFrom.convert(
|
||||||
|
unitTo,
|
||||||
|
evaluationResult,
|
||||||
|
_userPrefs.value.digitsPrecision
|
||||||
|
)
|
||||||
|
|
||||||
|
// Converted
|
||||||
_result.update { conversionResult.toStringWith(_userPrefs.value.outputFormat) }
|
_result.update { conversionResult.toStringWith(_userPrefs.value.outputFormat) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user