From 5cd2b3b205b7915ea0627bf91e5a274966d87f34 Mon Sep 17 00:00:00 2001 From: Sad Ellie Date: Sat, 17 Dec 2022 13:18:16 +0400 Subject: [PATCH] Fixed crashes on large numbers (#12) Currently just showing empty conversion result when can not convert due to expression result being too big, just like in Calculator by Google. Will add a proper message later. --- .../com/sadellie/unitto/screens/main/MainViewModel.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/sadellie/unitto/screens/main/MainViewModel.kt b/app/src/main/java/com/sadellie/unitto/screens/main/MainViewModel.kt index f6ad8922..a499a042 100644 --- a/app/src/main/java/com/sadellie/unitto/screens/main/MainViewModel.kt +++ b/app/src/main/java/com/sadellie/unitto/screens/main/MainViewModel.kt @@ -137,10 +137,17 @@ class MainViewModel @Inject constructor( // Kotlin doesn't have a multi catch val calculatedInput = try { val evaluated = Expressions() - // Optimal precision, not too low, not too high. Balanced for performance and UX. .eval(cleanInput) .setScale(_userPrefs.value.digitsPrecision, RoundingMode.HALF_EVEN) .stripTrailingZeros() + /** + * Too long values crash the UI. If the number is too big, we don't do conversion. + * User probably wouldn't need numbers beyond infinity. + */ + if (evaluated.abs() > BigDecimal.valueOf(Double.MAX_VALUE)) { + _calculatedValue.value = null + return "" + } if (evaluated.compareTo(BigDecimal.ZERO) == 0) BigDecimal.ZERO else evaluated } catch (e: Exception) { // Kotlin doesn't have a multi catch