couple more glitch fixes

This commit is contained in:
tibbi
2020-11-06 17:13:53 +01:00
parent f8f10c40ba
commit 6504233dd2

View File

@ -20,6 +20,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
private var baseValue = 0.0 private var baseValue = 0.0
private var secondValue = 0.0 private var secondValue = 0.0
private val operations = listOf("+", "-", "*", "/", "^", "%", "") private val operations = listOf("+", "-", "*", "/", "^", "%", "")
private val operationsRegex = "[+,-,*,/,^,%,√]".toRegex()
private var moreOperationsInRaw = false private var moreOperationsInRaw = false
init { init {
@ -79,13 +80,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
inputDisplayedFormula = "" inputDisplayedFormula = ""
} }
val valueToCheck = if (inputDisplayedFormula.startsWith("-")) { val value = getSecondValue().toString()
inputDisplayedFormula.substring(1)
} else {
inputDisplayedFormula
}
val value = valueToCheck.substring(valueToCheck.indexOfAny(operations, 0, false) + 1)
if (value == "0" && number.toString().areDigitsOnly()) { if (value == "0" && number.toString().areDigitsOnly()) {
inputDisplayedFormula = inputDisplayedFormula.dropLast(1) inputDisplayedFormula = inputDisplayedFormula.dropLast(1)
} }
@ -114,11 +109,12 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
fun handleResult() { fun handleResult() {
if (moreOperationsInRaw) { if (moreOperationsInRaw) {
val index = displayedNumber!!.indexOfAny(operations, 0, false) val index = displayedNumber!!.indexOfAny(operations)
displayedNumber = displayedNumber!!.substring(index + 1) displayedNumber = displayedNumber!!.substring(index + 1)
} }
moreOperationsInRaw = false moreOperationsInRaw = false
secondValue = getDisplayedNumberAsDouble() secondValue = getSecondValue()
calculateResult() calculateResult()
baseValue = getDisplayedNumberAsDouble() baseValue = getDisplayedNumberAsDouble()
setValue(inputDisplayedFormula) setValue(inputDisplayedFormula)
@ -179,10 +175,10 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
} }
if (lastKey == DIGIT && !lastOperation.isNullOrEmpty() && operation == PERCENT) { if (lastKey == DIGIT && !lastOperation.isNullOrEmpty() && operation == PERCENT) {
val tempOp = lastOperation val tempOperation = lastOperation
handlePercent() handlePercent()
lastKey = tempOp lastKey = tempOperation
lastOperation = tempOp lastOperation = tempOperation
} else if (lastKey == DIGIT) { } else if (lastKey == DIGIT) {
handleResult() handleResult()
if (inputDisplayedFormula.last() != '+' && if (inputDisplayedFormula.last() != '+' &&
@ -257,14 +253,8 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
return return
} }
val valueToCheck = if (inputDisplayedFormula.startsWith("-")) { secondValue = getSecondValue()
inputDisplayedFormula.substring(1) displayedNumber = secondValue.toString()
} else {
inputDisplayedFormula
}
displayedNumber = valueToCheck.substring(valueToCheck.indexOfAny(operations, 0, false) + 1)
secondValue = getDisplayedNumberAsDouble()
calculateResult() calculateResult()
lastKey = EQUALS lastKey = EQUALS
inputDisplayedFormula = displayedNumber ?: "0" inputDisplayedFormula = displayedNumber ?: "0"
@ -278,19 +268,15 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
inputDisplayedFormula inputDisplayedFormula
} }
var value = valueToCheck.substring(valueToCheck.indexOfAny(operations, 0, false) + 1) val value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
if (!value.contains(".")) { if (!value.contains(".")) {
when (value) { when {
"0" -> inputDisplayedFormula = "0." value == "0" && !valueToCheck.contains(operationsRegex) -> inputDisplayedFormula = "0."
"" -> inputDisplayedFormula += "0." value == "" -> inputDisplayedFormula += "0."
else -> inputDisplayedFormula += "." else -> inputDisplayedFormula += "."
} }
} else {
value = valueToCheck.substring(valueToCheck.indexOfAny(operations, 0, false) + 1)
if (!value.contains(".")) {
inputDisplayedFormula += "."
}
} }
setValue(inputDisplayedFormula) setValue(inputDisplayedFormula)
} }
@ -301,17 +287,16 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
inputDisplayedFormula inputDisplayedFormula
} }
return valueToCheck.substring(valueToCheck.indexOfAny(operations, 0, false) + 1).toDouble() var value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
if (value.isEmpty()) {
value = "0"
}
return value.toDouble()
} }
private fun zeroClicked() { private fun zeroClicked() {
val valueToCheck = if (inputDisplayedFormula.startsWith("-")) { val value = getSecondValue().toString()
inputDisplayedFormula.substring(1)
} else {
inputDisplayedFormula
}
val value = valueToCheck.substring(valueToCheck.indexOfAny(operations, 0, false) + 1)
if (value != "0") { if (value != "0") {
addDigit(0) addDigit(0)
} }