Switched to EvalEx

Will need to refactor some convert method in the future.
This commit is contained in:
Sad Ellie 2022-11-21 17:24:37 +04:00
parent e620794358
commit 193170c734
3 changed files with 18 additions and 18 deletions

View File

@ -200,6 +200,6 @@ dependencies {
// ComposeReorderable // ComposeReorderable
implementation("org.burnoutcrew.composereorderable:reorderable:0.9.6") implementation("org.burnoutcrew.composereorderable:reorderable:0.9.6")
// ExprK // EvalEx
implementation("com.github.Keelar:ExprK:30c00415a8") implementation("com.ezylang:EvalEx:3.0.1")
} }

View File

@ -29,11 +29,11 @@ data class AppLibrary(
val ALL_LIBRARIES = lazy { val ALL_LIBRARIES = lazy {
listOf( listOf(
AppLibrary( AppLibrary(
name = "ExprK", name = "EvalEx",
dev = "Keelar", dev = "ezylang",
website = "https://github.com/Keelar/ExprK", website = "https://github.com/ezylang/EvalEx",
license = "MIT license", license = "Apache-2.0",
description = "A simple mathematical expression evaluator for Kotlin and Java, written in Kotlin." description = "EvalEx is a handy expression evaluator for Java, that allows to evaluate simple mathematical and boolean expressions."
), ),
AppLibrary( AppLibrary(
name = "currency-api", name = "currency-api",

View File

@ -24,8 +24,8 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.github.keelar.exprk.ExpressionException import com.ezylang.evalex.BaseException
import com.github.keelar.exprk.Expressions import com.ezylang.evalex.Expression
import com.sadellie.unitto.FirebaseHelper import com.sadellie.unitto.FirebaseHelper
import com.sadellie.unitto.data.KEY_0 import com.sadellie.unitto.data.KEY_0
import com.sadellie.unitto.data.KEY_1 import com.sadellie.unitto.data.KEY_1
@ -126,14 +126,17 @@ class MainViewModel @Inject constructor(
// Kotlin doesn't have a multi catch // Kotlin doesn't have a multi catch
val calculatedInput = try { val calculatedInput = try {
Expressions() val evaluated = Expression(cleanInput)
// Optimal precision, not too low, not too high. Balanced for performance and UX. // Optimal precision, not too low, not too high. Balanced for performance and UX.
.setPrecision(128) .evaluate()
.eval(cleanInput) .numberValue
.setScale(_userPrefs.value.digitsPrecision, RoundingMode.HALF_EVEN)
.stripTrailingZeros()
if (evaluated.compareTo(BigDecimal.ZERO) == 0) BigDecimal.ZERO else evaluated
} catch (e: Exception) { } catch (e: Exception) {
// Kotlin doesn't have a multi catch // Kotlin doesn't have a multi catch
when (e) { when (e) {
is ExpressionException, is ArrayIndexOutOfBoundsException, is NumberFormatException, is ArithmeticException -> return mainFlow.value.resultValue is BaseException, is ArrayIndexOutOfBoundsException, is NumberFormatException, is ArithmeticException -> return mainFlow.value.resultValue
else -> throw e else -> throw e
} }
} }
@ -157,10 +160,7 @@ class MainViewModel @Inject constructor(
* consists of ZEROS only (0.00000 as an example). This check is a workaround. If the result * consists of ZEROS only (0.00000 as an example). This check is a workaround. If the result
* is zero, than we make sure there are no trailing zeros. * is zero, than we make sure there are no trailing zeros.
*/ */
val resultValue = if (convertedValue == BigDecimal.ZERO.setScale( val resultValue = if (convertedValue.compareTo(BigDecimal.ZERO) == 0) {
_userPrefs.value.digitsPrecision, RoundingMode.HALF_EVEN
)
) {
KEY_0 KEY_0
} else { } else {
convertedValue.toStringWith(_userPrefs.value.outputFormat) convertedValue.toStringWith(_userPrefs.value.outputFormat)