diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/legacy/ui/theme/modal/edit/CalculatorModal.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/legacy/ui/theme/modal/edit/CalculatorModal.kt index 404f0e84a9..409a13a42e 100644 --- a/temp/legacy-code/src/main/java/com/ivy/legacy/legacy/ui/theme/modal/edit/CalculatorModal.kt +++ b/temp/legacy-code/src/main/java/com/ivy/legacy/legacy/ui/theme/modal/edit/CalculatorModal.kt @@ -117,7 +117,7 @@ fun BoxWithConstraintsScope.CalculatorModal( text = "÷", testTag = "key_/" ) { - expression += "÷" + expression = handleOperator(expression, "÷") } }, FirstRowExtra = { @@ -125,7 +125,7 @@ fun BoxWithConstraintsScope.CalculatorModal( text = "×", testTag = "key_*" ) { - expression += "×" + expression = handleOperator(expression, "×") } }, SecondRowExtra = { @@ -133,7 +133,7 @@ fun BoxWithConstraintsScope.CalculatorModal( text = "−", testTag = "key_-" ) { - expression += "−" + expression = handleOperator(expression, "−") } }, ThirdRowExtra = { @@ -141,7 +141,7 @@ fun BoxWithConstraintsScope.CalculatorModal( text = "+", testTag = "key_+" ) { - expression += "+" + expression = handleOperator(expression, "+") } }, FourthRowExtra = { @@ -179,6 +179,19 @@ fun BoxWithConstraintsScope.CalculatorModal( } } +private fun handleOperator(expression: String, operator: String): String { + return if (expression.isNotEmpty() && expression.last().isOperator()) { + expression.dropLast(1) + operator + } else { + expression + operator + } +} + +fun Char.isOperator(): Boolean = when (this) { + '+', '−', '×', '÷' -> true + else -> false +} + private fun formatExpression(expression: String, currency: String): String { var formattedExpression = expression