some renaming, reordering

This commit is contained in:
tibbi
2020-11-06 22:30:40 +01:00
parent 606fad63ef
commit f527d8d954
4 changed files with 50 additions and 47 deletions

View File

@ -143,7 +143,7 @@ class MainActivity : SimpleActivity(), Calculator {
} }
} }
override fun setValue(value: String, context: Context) { override fun showNewResult(value: String, context: Context) {
result.text = value result.text = value
} }
@ -155,7 +155,7 @@ class MainActivity : SimpleActivity(), Calculator {
} }
} }
override fun setFormula(value: String, context: Context) { override fun showNewFormula(value: String, context: Context) {
formula.text = value formula.text = value
} }
} }

View File

@ -3,7 +3,7 @@ package com.simplemobiletools.calculator.helpers
import android.content.Context import android.content.Context
interface Calculator { interface Calculator {
fun setValue(value: String, context: Context) fun showNewResult(value: String, context: Context)
fun setFormula(value: String, context: Context) fun showNewFormula(value: String, context: Context)
} }

View File

@ -20,20 +20,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
private val operationsRegex = "[-+*/^%√]".toPattern() private val operationsRegex = "[-+*/^%√]".toPattern()
init { init {
setValue("0") showNewResult("0")
}
private fun resetValues() {
baseValue = 0.0
secondValue = 0.0
displayedNumber = ""
lastKey = ""
lastOperation = ""
}
fun setValue(value: String) {
callback!!.setValue(value, context)
displayedNumber = value
} }
private fun addDigit(number: Int) { private fun addDigit(number: Int) {
@ -47,7 +34,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
} }
inputDisplayedFormula += number inputDisplayedFormula += number
setValue(inputDisplayedFormula) showNewResult(inputDisplayedFormula)
} }
private fun formatString(str: String): String { private fun formatString(str: String): String {
@ -60,17 +47,26 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
} }
private fun updateResult(value: Double) { private fun updateResult(value: Double) {
setValue(value.format()) showNewResult(value.format())
baseValue = value baseValue = value
} }
private fun showNewResult(value: String) {
callback!!.showNewResult(value, context)
displayedNumber = value
}
private fun showNewFormula(value: String) {
callback!!.showNewFormula(value, context)
}
private fun getDisplayedNumberAsDouble() = Formatter.stringToDouble(displayedNumber) private fun getDisplayedNumberAsDouble() = Formatter.stringToDouble(displayedNumber)
private fun handleResult() { private fun handleResult() {
secondValue = getSecondValue() secondValue = getSecondValue()
calculateResult() calculateResult()
baseValue = getDisplayedNumberAsDouble() baseValue = getDisplayedNumberAsDouble()
setValue(inputDisplayedFormula) showNewResult(inputDisplayedFormula)
} }
private fun calculateResult() { private fun calculateResult() {
@ -101,7 +97,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
updateResult(result) updateResult(result)
baseValue = result baseValue = result
inputDisplayedFormula = result.format() inputDisplayedFormula = result.format()
callback!!.setFormula(expression.replace("sqrt", ""), context) showNewFormula(expression.replace("sqrt", ""))
} catch (e: Exception) { } catch (e: Exception) {
context.toast(R.string.unknown_error_occurred) context.toast(R.string.unknown_error_occurred)
} }
@ -112,7 +108,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
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()
callback!!.setFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%", context) showNewFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%")
inputDisplayedFormula = result.format() inputDisplayedFormula = result.format()
updateResult(result) updateResult(result)
} }
@ -167,7 +163,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
lastKey = operation lastKey = operation
lastOperation = operation lastOperation = operation
setValue(inputDisplayedFormula) showNewResult(inputDisplayedFormula)
} }
fun handleClear() { fun handleClear() {
@ -192,17 +188,25 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
!newValue.contains('√')) { !newValue.contains('√')) {
newValue = formatString(newValue) newValue = formatString(newValue)
} }
setValue(newValue) showNewResult(newValue)
inputDisplayedFormula = if (newValue != "0") newValue else "" inputDisplayedFormula = if (newValue != "0") newValue else ""
} }
fun handleReset() { fun handleReset() {
resetValues() resetValues()
setValue("0") showNewResult("0")
callback!!.setFormula("", context) showNewFormula("")
inputDisplayedFormula = "" inputDisplayedFormula = ""
} }
private fun resetValues() {
baseValue = 0.0
secondValue = 0.0
displayedNumber = ""
lastKey = ""
lastOperation = ""
}
fun handleEquals() { fun handleEquals() {
if (lastKey == EQUALS) { if (lastKey == EQUALS) {
calculateResult() calculateResult()
@ -220,6 +224,21 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
baseValue = getDisplayedNumberAsDouble() baseValue = getDisplayedNumberAsDouble()
} }
private fun getSecondValue(): Double {
val valueToCheck = if (inputDisplayedFormula.startsWith("-")) {
inputDisplayedFormula.substring(1)
} else {
inputDisplayedFormula
}.replace(",", "")
var value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
if (value.isEmpty()) {
value = "0"
}
return value.toDouble()
}
private fun decimalClicked() { private fun decimalClicked() {
val valueToCheck = if (inputDisplayedFormula.startsWith("-")) { val valueToCheck = if (inputDisplayedFormula.startsWith("-")) {
inputDisplayedFormula.substring(1) inputDisplayedFormula.substring(1)
@ -236,22 +255,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
} }
} }
setValue(inputDisplayedFormula) showNewResult(inputDisplayedFormula)
}
private fun getSecondValue(): Double {
val valueToCheck = if (inputDisplayedFormula.startsWith("-")) {
inputDisplayedFormula.substring(1)
} else {
inputDisplayedFormula
}.replace(",", "")
var value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
if (value.isEmpty()) {
value = "0"
}
return value.toDouble()
} }
private fun zeroClicked() { private fun zeroClicked() {
@ -268,14 +272,13 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
} }
private fun getSign(lastOperation: String) = when (lastOperation) { private fun getSign(lastOperation: String) = when (lastOperation) {
PLUS -> "+"
MINUS -> "-" MINUS -> "-"
MULTIPLY -> "*" MULTIPLY -> "*"
DIVIDE -> "/" DIVIDE -> "/"
PERCENT -> "%" PERCENT -> "%"
POWER -> "^" POWER -> "^"
ROOT -> "" ROOT -> ""
else -> "" else -> "+"
} }
fun numpadClicked(id: Int) { fun numpadClicked(id: Int) {

View File

@ -114,7 +114,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
} }
} }
override fun setValue(value: String, context: Context) { override fun showNewResult(value: String, context: Context) {
val appWidgetManager = AppWidgetManager.getInstance(context) val appWidgetManager = AppWidgetManager.getInstance(context)
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach { appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
val views = RemoteViews(context.packageName, R.layout.widget) val views = RemoteViews(context.packageName, R.layout.widget)
@ -123,7 +123,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
} }
} }
override fun setFormula(value: String, context: Context) { override fun showNewFormula(value: String, context: Context) {
val appWidgetManager = AppWidgetManager.getInstance(context) val appWidgetManager = AppWidgetManager.getInstance(context)
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach { appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
val views = RemoteViews(context.packageName, R.layout.widget) val views = RemoteViews(context.packageName, R.layout.widget)