reordering some functions to give it more sense

This commit is contained in:
tibbi
2020-11-06 23:47:43 +01:00
parent d74457b1e7
commit 15e6f060e3

View File

@ -36,52 +36,26 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
showNewResult(inputDisplayedFormula) showNewResult(inputDisplayedFormula)
} }
private fun showNewResult(value: String) { private fun zeroClicked() {
callback!!.showNewResult(value, context)
}
private fun showNewFormula(value: String) {
callback!!.showNewFormula(value, context)
}
private fun calculateResult() {
if (lastOperation == ROOT && inputDisplayedFormula.startsWith("")) {
baseValue = 1.0
}
if (lastKey != EQUALS) {
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "") val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
val parts = valueToCheck.split(operationsRegex).filter { it.trim().isNotEmpty() } val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
baseValue = parts.first().replace(",", "").toDouble() if (value != "0" || value.contains(".")) {
if (inputDisplayedFormula.startsWith("-")) { addDigit(0)
baseValue *= -1
}
secondValue = parts.getOrNull(1)?.replace(",", "")?.toDouble() ?: secondValue
}
if (lastOperation != "") {
val expression = "${baseValue.format()}${getSign(lastOperation)}${secondValue.format()}".replace("", "sqrt")
try {
val result = ExpressionBuilder(expression.replace(",", "")).build().evaluate()
showNewResult(result.format())
baseValue = result
inputDisplayedFormula = result.format()
showNewFormula(expression.replace("sqrt", ""))
} catch (e: Exception) {
context.toast(R.string.unknown_error_occurred)
}
} }
} }
// handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there private fun decimalClicked() {
private fun handlePercent() { val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
val operation = PercentOperation(baseValue, getSecondValue(), lastOperation) val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
val result = operation.getResult() if (!value.contains(".")) {
showNewFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%") when {
inputDisplayedFormula = result.format() value == "0" && !valueToCheck.contains(operationsRegex.toRegex()) -> inputDisplayedFormula = "0."
showNewResult(result.format()) value == "" -> inputDisplayedFormula += "0."
baseValue = result else -> inputDisplayedFormula += "."
}
}
showNewResult(inputDisplayedFormula)
} }
fun handleOperation(operation: String) { fun handleOperation(operation: String) {
@ -117,6 +91,16 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
showNewResult(inputDisplayedFormula) showNewResult(inputDisplayedFormula)
} }
// handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there
private fun handlePercent() {
val operation = PercentOperation(baseValue, getSecondValue(), lastOperation)
val result = operation.getResult()
showNewFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%")
inputDisplayedFormula = result.format()
showNewResult(result.format())
baseValue = result
}
fun handleEquals() { fun handleEquals() {
if (lastKey == EQUALS) { if (lastKey == EQUALS) {
calculateResult() calculateResult()
@ -141,26 +125,42 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
return value.toDouble() return value.toDouble()
} }
private fun decimalClicked() { private fun calculateResult() {
if (lastOperation == ROOT && inputDisplayedFormula.startsWith("")) {
baseValue = 1.0
}
if (lastKey != EQUALS) {
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "") val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "")
val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1) val parts = valueToCheck.split(operationsRegex).filter { it.trim().isNotEmpty() }
if (!value.contains(".")) { baseValue = parts.first().replace(",", "").toDouble()
when { if (inputDisplayedFormula.startsWith("-")) {
value == "0" && !valueToCheck.contains(operationsRegex.toRegex()) -> inputDisplayedFormula = "0." baseValue *= -1
value == "" -> inputDisplayedFormula += "0." }
else -> inputDisplayedFormula += "."
secondValue = parts.getOrNull(1)?.replace(",", "")?.toDouble() ?: secondValue
}
if (lastOperation != "") {
val expression = "${baseValue.format()}${getSign(lastOperation)}${secondValue.format()}".replace("", "sqrt")
try {
val result = ExpressionBuilder(expression.replace(",", "")).build().evaluate()
showNewResult(result.format())
baseValue = result
inputDisplayedFormula = result.format()
showNewFormula(expression.replace("sqrt", ""))
} catch (e: Exception) {
context.toast(R.string.unknown_error_occurred)
}
} }
} }
showNewResult(inputDisplayedFormula) private fun showNewResult(value: String) {
callback!!.showNewResult(value, context)
} }
private fun zeroClicked() { private fun showNewFormula(value: String) {
val valueToCheck = inputDisplayedFormula.trimStart('-').replace(",", "") callback!!.showNewFormula(value, context)
val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
if (value != "0" || value.contains(".")) {
addDigit(0)
}
} }
fun handleClear() { fun handleClear() {