bit more cleanup

This commit is contained in:
tibbi 2020-11-06 22:20:17 +01:00
parent de1bf98bcf
commit 606fad63ef

View File

@ -8,19 +8,18 @@ import com.simplemobiletools.commons.extensions.toast
import net.objecthunter.exp4j.ExpressionBuilder import net.objecthunter.exp4j.ExpressionBuilder
class CalculatorImpl(calculator: Calculator, private val context: Context) { class CalculatorImpl(calculator: Calculator, private val context: Context) {
private var displayedNumber: String? = null
private var inputDisplayedFormula = "0"
private var callback: Calculator? = calculator private var callback: Calculator? = calculator
private var baseValue = 0.0 private var baseValue = 0.0
private var secondValue = 0.0 private var secondValue = 0.0
private var inputDisplayedFormula = "0"
private var displayedNumber = ""
private var lastKey = "" private var lastKey = ""
private var lastOperation = "" private var lastOperation = ""
private val operations = listOf("+", "-", "*", "/", "^", "%", "") private val operations = listOf("+", "-", "*", "/", "^", "%", "")
private val operationsRegex = "[-+*/^%√]".toPattern() private val operationsRegex = "[-+*/^%√]".toPattern()
init { init {
resetValues()
setValue("0") setValue("0")
} }
@ -65,7 +64,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
baseValue = value baseValue = value
} }
private fun getDisplayedNumberAsDouble() = Formatter.stringToDouble(displayedNumber!!) private fun getDisplayedNumberAsDouble() = Formatter.stringToDouble(displayedNumber)
private fun handleResult() { private fun handleResult() {
secondValue = getSecondValue() secondValue = getSecondValue()
@ -172,12 +171,9 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
} }
fun handleClear() { fun handleClear() {
if (displayedNumber.equals(NAN)) {
handleReset()
} else {
val oldValue = displayedNumber val oldValue = displayedNumber
var newValue = "0" var newValue = "0"
val len = oldValue!!.length val len = oldValue.length
var minLen = 1 var minLen = 1
if (oldValue.contains("-")) if (oldValue.contains("-"))
minLen++ minLen++
@ -199,7 +195,6 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
setValue(newValue) setValue(newValue)
inputDisplayedFormula = if (newValue != "0") newValue else "" inputDisplayedFormula = if (newValue != "0") newValue else ""
} }
}
fun handleReset() { fun handleReset() {
resetValues() resetValues()
@ -221,7 +216,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
displayedNumber = secondValue.toString() displayedNumber = secondValue.toString()
calculateResult() calculateResult()
lastKey = EQUALS lastKey = EQUALS
inputDisplayedFormula = displayedNumber ?: "0" inputDisplayedFormula = displayedNumber
baseValue = getDisplayedNumberAsDouble() baseValue = getDisplayedNumberAsDouble()
} }