mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-03-11 17:00:05 +01:00
more cleanup and reordering
This commit is contained in:
parent
06486aad1c
commit
e9167c9183
@ -36,11 +36,6 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
showNewResult(inputDisplayedFormula)
|
showNewResult(inputDisplayedFormula)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateResult(value: Double) {
|
|
||||||
showNewResult(value.format())
|
|
||||||
baseValue = value
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun showNewResult(value: String) {
|
private fun showNewResult(value: String) {
|
||||||
callback!!.showNewResult(value, context)
|
callback!!.showNewResult(value, context)
|
||||||
}
|
}
|
||||||
@ -49,12 +44,6 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
callback!!.showNewFormula(value, context)
|
callback!!.showNewFormula(value, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleResult() {
|
|
||||||
secondValue = getSecondValue()
|
|
||||||
calculateResult()
|
|
||||||
showNewResult(inputDisplayedFormula)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun calculateResult() {
|
private fun calculateResult() {
|
||||||
if (lastOperation == ROOT && inputDisplayedFormula.startsWith("√")) {
|
if (lastOperation == ROOT && inputDisplayedFormula.startsWith("√")) {
|
||||||
baseValue = 1.0
|
baseValue = 1.0
|
||||||
@ -75,7 +64,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
val expression = "${baseValue.format()}${getSign(lastOperation)}${secondValue.format()}".replace("√", "sqrt")
|
val expression = "${baseValue.format()}${getSign(lastOperation)}${secondValue.format()}".replace("√", "sqrt")
|
||||||
try {
|
try {
|
||||||
val result = ExpressionBuilder(expression.replace(",", "")).build().evaluate()
|
val result = ExpressionBuilder(expression.replace(",", "")).build().evaluate()
|
||||||
updateResult(result)
|
showNewResult(result.format())
|
||||||
baseValue = result
|
baseValue = result
|
||||||
inputDisplayedFormula = result.format()
|
inputDisplayedFormula = result.format()
|
||||||
showNewFormula(expression.replace("sqrt", "√"))
|
showNewFormula(expression.replace("sqrt", "√"))
|
||||||
@ -85,13 +74,14 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. % is used only for modulo there
|
// handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there
|
||||||
private fun handlePercent() {
|
private fun handlePercent() {
|
||||||
val operation = PercentOperation(baseValue, getSecondValue(), lastOperation)
|
val operation = PercentOperation(baseValue, getSecondValue(), lastOperation)
|
||||||
val result = operation.getResult()
|
val result = operation.getResult()
|
||||||
showNewFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%")
|
showNewFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%")
|
||||||
inputDisplayedFormula = result.format()
|
inputDisplayedFormula = result.format()
|
||||||
updateResult(result)
|
showNewResult(result.format())
|
||||||
|
baseValue = result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun handleOperation(operation: String) {
|
fun handleOperation(operation: String) {
|
||||||
@ -103,23 +93,11 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
inputDisplayedFormula = "√"
|
inputDisplayedFormula = "√"
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inputDisplayedFormula.last() == '-' ||
|
if (operations.contains(inputDisplayedFormula.last().toString())) {
|
||||||
inputDisplayedFormula.last() == '+' ||
|
|
||||||
inputDisplayedFormula.last() == '*' ||
|
|
||||||
inputDisplayedFormula.last() == '/' ||
|
|
||||||
inputDisplayedFormula.last() == '^' ||
|
|
||||||
inputDisplayedFormula.last() == '%' ||
|
|
||||||
inputDisplayedFormula.last() == '√') {
|
|
||||||
inputDisplayedFormula = inputDisplayedFormula.dropLast(1)
|
inputDisplayedFormula = inputDisplayedFormula.dropLast(1)
|
||||||
inputDisplayedFormula += getSign(operation)
|
inputDisplayedFormula += getSign(operation)
|
||||||
} else {
|
} else {
|
||||||
if (!inputDisplayedFormula.trimStart('-').contains('-') &&
|
if (!inputDisplayedFormula.trimStart('-').contains(operationsRegex.toRegex())) {
|
||||||
!inputDisplayedFormula.contains('+') &&
|
|
||||||
!inputDisplayedFormula.contains('*') &&
|
|
||||||
!inputDisplayedFormula.contains('/') &&
|
|
||||||
!inputDisplayedFormula.contains('^') &&
|
|
||||||
!inputDisplayedFormula.contains('%') &&
|
|
||||||
!inputDisplayedFormula.contains('√')) {
|
|
||||||
inputDisplayedFormula += getSign(operation)
|
inputDisplayedFormula += getSign(operation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -130,14 +108,9 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
lastKey = tempOperation
|
lastKey = tempOperation
|
||||||
lastOperation = tempOperation
|
lastOperation = tempOperation
|
||||||
} else if (lastKey == DIGIT) {
|
} else if (lastKey == DIGIT) {
|
||||||
handleResult()
|
secondValue = getSecondValue()
|
||||||
if (inputDisplayedFormula.last() != '-' &&
|
calculateResult()
|
||||||
inputDisplayedFormula.last() != '+' &&
|
if (!operations.contains(inputDisplayedFormula.last().toString())) {
|
||||||
inputDisplayedFormula.last() != '*' &&
|
|
||||||
inputDisplayedFormula.last() != '/' &&
|
|
||||||
inputDisplayedFormula.last() != '^' &&
|
|
||||||
inputDisplayedFormula.last() != '%' &&
|
|
||||||
inputDisplayedFormula.last() != '√') {
|
|
||||||
inputDisplayedFormula += getSign(operation)
|
inputDisplayedFormula += getSign(operation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -147,31 +120,6 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
showNewResult(inputDisplayedFormula)
|
showNewResult(inputDisplayedFormula)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun handleClear() {
|
|
||||||
var newValue = inputDisplayedFormula.dropLast(1)
|
|
||||||
if (newValue.isEmpty()) {
|
|
||||||
newValue = "0"
|
|
||||||
}
|
|
||||||
|
|
||||||
newValue = newValue.trimEnd(',')
|
|
||||||
inputDisplayedFormula = newValue
|
|
||||||
showNewResult(newValue)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun handleReset() {
|
|
||||||
resetValues()
|
|
||||||
showNewResult("0")
|
|
||||||
showNewFormula("")
|
|
||||||
inputDisplayedFormula = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun resetValues() {
|
|
||||||
baseValue = 0.0
|
|
||||||
secondValue = 0.0
|
|
||||||
lastKey = ""
|
|
||||||
lastOperation = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
fun handleEquals() {
|
fun handleEquals() {
|
||||||
if (lastKey == EQUALS) {
|
if (lastKey == EQUALS) {
|
||||||
calculateResult()
|
calculateResult()
|
||||||
@ -211,18 +159,38 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun zeroClicked() {
|
private fun zeroClicked() {
|
||||||
val valueToCheck = if (inputDisplayedFormula.startsWith("-")) {
|
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
|
||||||
inputDisplayedFormula.substring(1)
|
|
||||||
} else {
|
|
||||||
inputDisplayedFormula
|
|
||||||
}
|
|
||||||
|
|
||||||
val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
|
val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
|
||||||
if (value != "0" || value.contains(".")) {
|
if (value != "0" || value.contains(".")) {
|
||||||
addDigit(0)
|
addDigit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun handleClear() {
|
||||||
|
var newValue = inputDisplayedFormula.dropLast(1)
|
||||||
|
if (newValue.isEmpty()) {
|
||||||
|
newValue = "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
newValue = newValue.trimEnd(',')
|
||||||
|
inputDisplayedFormula = newValue
|
||||||
|
showNewResult(newValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun handleReset() {
|
||||||
|
resetValues()
|
||||||
|
showNewResult("0")
|
||||||
|
showNewFormula("")
|
||||||
|
inputDisplayedFormula = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun resetValues() {
|
||||||
|
baseValue = 0.0
|
||||||
|
secondValue = 0.0
|
||||||
|
lastKey = ""
|
||||||
|
lastOperation = ""
|
||||||
|
}
|
||||||
|
|
||||||
private fun getSign(lastOperation: String) = when (lastOperation) {
|
private fun getSign(lastOperation: String) = when (lastOperation) {
|
||||||
MINUS -> "-"
|
MINUS -> "-"
|
||||||
MULTIPLY -> "*"
|
MULTIPLY -> "*"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user