mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-02-08 23:58:42 +01:00
bit more cleanup
This commit is contained in:
parent
de1bf98bcf
commit
606fad63ef
@ -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,33 +171,29 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun handleClear() {
|
fun handleClear() {
|
||||||
if (displayedNumber.equals(NAN)) {
|
val oldValue = displayedNumber
|
||||||
handleReset()
|
var newValue = "0"
|
||||||
} else {
|
val len = oldValue.length
|
||||||
val oldValue = displayedNumber
|
var minLen = 1
|
||||||
var newValue = "0"
|
if (oldValue.contains("-"))
|
||||||
val len = oldValue!!.length
|
minLen++
|
||||||
var minLen = 1
|
|
||||||
if (oldValue.contains("-"))
|
|
||||||
minLen++
|
|
||||||
|
|
||||||
if (len > minLen) {
|
if (len > minLen) {
|
||||||
newValue = oldValue.substring(0, len - 1)
|
newValue = oldValue.substring(0, len - 1)
|
||||||
}
|
|
||||||
|
|
||||||
newValue = newValue.replace("\\.$".toRegex(), "")
|
|
||||||
if (!newValue.contains('+') &&
|
|
||||||
!newValue.contains('-') &&
|
|
||||||
!newValue.contains('*') &&
|
|
||||||
!newValue.contains('/') &&
|
|
||||||
!newValue.contains('%') &&
|
|
||||||
!newValue.contains('^') &&
|
|
||||||
!newValue.contains('√')) {
|
|
||||||
newValue = formatString(newValue)
|
|
||||||
}
|
|
||||||
setValue(newValue)
|
|
||||||
inputDisplayedFormula = if (newValue != "0") newValue else ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newValue = newValue.replace("\\.$".toRegex(), "")
|
||||||
|
if (!newValue.contains('+') &&
|
||||||
|
!newValue.contains('-') &&
|
||||||
|
!newValue.contains('*') &&
|
||||||
|
!newValue.contains('/') &&
|
||||||
|
!newValue.contains('%') &&
|
||||||
|
!newValue.contains('^') &&
|
||||||
|
!newValue.contains('√')) {
|
||||||
|
newValue = formatString(newValue)
|
||||||
|
}
|
||||||
|
setValue(newValue)
|
||||||
|
inputDisplayedFormula = if (newValue != "0") newValue else ""
|
||||||
}
|
}
|
||||||
|
|
||||||
fun handleReset() {
|
fun handleReset() {
|
||||||
@ -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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user