Restore old calculation logic

This commit is contained in:
Sad Ellie 2023-09-12 13:39:38 +03:00
parent d7db2780c8
commit 0930eead24
2 changed files with 11 additions and 5 deletions

View File

@ -25,8 +25,10 @@ import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.expandHorizontally
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
@ -310,8 +312,10 @@ private fun Default(
formatterSymbols = uiState.formatterSymbols,
)
AnimatedVisibility(
visible = uiState.calculation != null,
modifier = Modifier.weight(1f)
visible = calculation.text.isNotEmpty(),
modifier = Modifier.weight(1f),
enter = expandVertically(clip = false),
exit = shrinkVertically(clip = false)
) {
ExpressionTextField(
modifier = Modifier,

View File

@ -38,6 +38,7 @@ import com.sadellie.unitto.data.units.stateIn
import com.sadellie.unitto.data.userprefs.UserPreferencesRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.sadellie.evaluatto.Expression
import io.github.sadellie.evaluatto.ExpressionException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
@ -349,13 +350,14 @@ internal class ConverterViewModel @Inject constructor(
) = viewModelScope.launch(Dispatchers.Default) {
val calculated = try {
Expression(input.text.ifEmpty { Token.Digit._0 }).calculate()
} catch (e: ExpressionException.DivideByZero) {
_calculation.update { null }
return@launch
} catch (e: Exception) {
null
return@launch
}
_calculation.update { if (input.text.isExpression()) calculated else null }
if (calculated == null) return@launch
try {
if ((unitFrom.group == UnitGroup.TIME) and (formatTime)) {
_result.update { formatTime(calculated.multiply(unitFrom.basicUnit)) }