Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Fix: Prevent consecutive operator input in CalculatorModal #3359

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,31 @@ fun BoxWithConstraintsScope.CalculatorModal(
text = "÷",
testTag = "key_/"
) {
expression += "÷"
expression = handleOperator(expression, "÷")
}
},
FirstRowExtra = {
KeypadCircleButton(
text = "×",
testTag = "key_*"
) {
expression += "×"
expression = handleOperator(expression, "×")
}
},
SecondRowExtra = {
KeypadCircleButton(
text = "−",
testTag = "key_-"
) {
expression += "−"
expression = handleOperator(expression, "−")
}
},
ThirdRowExtra = {
KeypadCircleButton(
text = "+",
testTag = "key_+"
) {
expression += "+"
expression = handleOperator(expression, "+")
}
},
FourthRowExtra = {
Expand Down Expand Up @@ -179,6 +179,19 @@ fun BoxWithConstraintsScope.CalculatorModal(
}
}

private fun handleOperator(expression: String, operator: String): String {
ILIYANGERMANOV marked this conversation as resolved.
Show resolved Hide resolved
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

Expand Down
Loading