cleaning code

This commit is contained in:
theom 2023-02-22 23:06:23 +01:00
parent 790302184a
commit f9e4629938
3 changed files with 15 additions and 112 deletions

View File

@ -1,8 +0,0 @@
### Reporting
Before you report something, read the reporting rules [here](https://github.com/SimpleMobileTools/General-Discussion#how-do-i-suggest-an-improvement-ask-a-question-or-report-an-issue) please.
### Contributing as a developer
Some instructions about code style and everything that has to be done to increase the change of your code getting accepted can be found at the [General Discussion](https://github.com/SimpleMobileTools/General-Discussion#contribution-rules-for-developers) section.
### Contributing as a non developer
In case you just want to for example improve a translation, you can find the way of doing it [here](https://github.com/SimpleMobileTools/General-Discussion#how-can-i-suggest-an-edit-to-a-file).

View File

@ -3,7 +3,6 @@ package com.simplemobiletools.calculator.activities
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.core.content.res.ResourcesCompat
@ -31,18 +30,7 @@ class MainActivity : SimpleActivity(), Calculator {
private var decimalSeparator = DOT
private var groupingSeparator = COMMA
//============================================================
//private var savedRes: String = "0"
//private var savedPreviousCalculation = ""
//private var savedLastKey: String = ""
//private var savedLastOperation: String = ""
//private var savedBaseValue : Double= 0.0
//private var savedSecondValue : Double = 0.0
//private var savedInputDisplayedFormula : String = "0"
private var json: String = ""
//============================================================
private var saveCalculatorState: String = ""
private lateinit var calc: CalculatorImpl
@ -59,34 +47,12 @@ class MainActivity : SimpleActivity(), Calculator {
updateMaterialActivityViews(main_coordinator, null, useTransparentNavigation = false, useTopSearchMenu = false)
setupMaterialScrollListener(main_nested_scrollview, main_toolbar)
//============================================================
//Log.v("SAVEDRES : ", savedRes)
//Log.v("SAVEDPREVIOUS : ", savedPreviousCalculation)
//Log.v("SAVEDLASTKEY : ", savedLastKey)
//Log.v("SAVEDLASTOP : ", savedLastOperation)
//Log.v("DECIMALSEP : ", decimalSeparator)
//Log.v("GROUPINGSEP : ", groupingSeparator)
//Log.v("BASEVALUE : ", savedBaseValue.toString())
//Log.v("SECONDVALUE : ", savedSecondValue.toString())
if (savedInstanceState != null) {
Log.v("MainActivity", "LOG TEST");
//savedRes = savedInstanceState?.getCharSequence("res") as String
//savedPreviousCalculation = savedInstanceState?.getCharSequence("savedPreviousCalculation") as String
//savedLastKey = savedInstanceState?.getCharSequence("savedLastKey") as String
//savedLastOperation = savedInstanceState?.getCharSequence("savedLastOperation") as String
//savedBaseValue = savedInstanceState.getDouble("savedBaseValue")
//savedSecondValue = savedInstanceState.getDouble("savedSecondValue")
//savedInputDisplayedFormula = savedInstanceState.getCharSequence("savedInputDisplayedFormula") as String
json = savedInstanceState.getCharSequence("myJsonObject") as String
saveCalculatorState = savedInstanceState.getCharSequence("calculatorState") as String
}
calc = CalculatorImpl(this, applicationContext, decimalSeparator, groupingSeparator, json)
calc = CalculatorImpl(this, applicationContext, decimalSeparator, groupingSeparator, saveCalculatorState)
//============================================================
btn_plus.setOnClickOperation(PLUS)
btn_minus.setOnClickOperation(MINUS)
@ -288,21 +254,9 @@ class MainActivity : SimpleActivity(), Calculator {
}
}
//============================================================
override fun onSaveInstanceState(bundle: Bundle) {
super.onSaveInstanceState(bundle)
/*
bundle.putString("res", calc.mResult)
bundle.putString("savedPreviousCalculation", calc.previousCalculation)
bundle.putString("savedLastKey", calc.lastKey)
bundle.putString("savedLastOperation", calc.lastOperation)
bundle.putDouble("savedBaseValue", calc.baseValue)
bundle.putDouble("savedSecondValue", calc.getSecondValueV2())
bundle.putString("savedInputDisplayedFormula", calc.inputDisplayedFormula)
*/
//JSON
bundle.putString("myJsonObject", calc.getJson().toString())
bundle.putString("calculatorState", calc.getCalculatorStateJson().toString())
}
//============================================================
}

View File

@ -1,8 +1,6 @@
package com.simplemobiletools.calculator.helpers
import android.content.Context
import android.util.Log
import com.google.gson.JsonObject
import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.models.History
import com.simplemobiletools.commons.extensions.showErrorToast
@ -17,17 +15,11 @@ class CalculatorImpl(
private val context: Context,
private var decimalSeparator: String = DOT,
private var groupingSeparator: String = COMMA,
private var json: String = ""
calculatorState: String = ""
) {
private var callback: Calculator? = calculator
//============================================================
//============================================================
private var jsonObj = json
private var mResult = "0"
private var stateInstance = calculatorState
private var currentResult = "0"
private var previousCalculation = ""
private var baseValue = 0.0
private var secondValue = 0.0
@ -42,16 +34,11 @@ class CalculatorImpl(
)
init {
//============================================================
if (jsonObj != "") {
setFromSaveInstanceState(jsonObj)
if (stateInstance != "") {
setFromSaveInstanceState(stateInstance)
}
Log.v("BASEVALUE INIT :", baseValue.toString())
Log.v("SECONDVALUE INIT :", secondValue.toString())
//showNewResult("0")
showNewResult(mResult)
showNewResult(currentResult)
showNewFormula(previousCalculation)
//============================================================
}
private fun addDigit(number: Int) {
@ -219,10 +206,8 @@ class CalculatorImpl(
private fun getSecondValue(): Double {
val valueToCheck = inputDisplayedFormula.trimStart('-').removeGroupSeparator()
Log.v("VALUEToCheck GETSV :", valueToCheck)
var value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
Log.v("VALUE GETSV :", value)
if (value == "") {
value = "0"
}
@ -240,8 +225,6 @@ class CalculatorImpl(
baseValue = 1.0
}
Log.v("LASKEY CR :", lastKey)
if (lastKey != EQUALS) {
val valueToCheck = inputDisplayedFormula.trimStart('-').removeGroupSeparator()
val parts = valueToCheck.split(operationsRegex).filter { it != "" }
@ -304,14 +287,6 @@ class CalculatorImpl(
return
}
//============================================================
//mResult = result.format()
Log.v("CalculResult", result.format())
Log.v("BASEVALUE CR :", baseValue.toString())
Log.v("SECONDVALUE CR :", secondValue.toString())
//============================================================
showNewResult(result.format())
val newFormula = "${baseValue.format()}$sign${secondValue.format()}"
HistoryHelper(context).insertOrUpdateHistoryEntry(
@ -319,10 +294,6 @@ class CalculatorImpl(
)
showNewFormula(newFormula)
//============================================================
//previousCalculation = newFormula
//============================================================
inputDisplayedFormula = result.format()
baseValue = result
} catch (e: Exception) {
@ -358,18 +329,12 @@ class CalculatorImpl(
}
private fun showNewResult(value: String) {
//============================================================
mResult = value
//============================================================
currentResult = value
callback!!.showNewResult(value, context)
}
private fun showNewFormula(value: String) {
//============================================================
previousCalculation = value
Log.v("BASEVALUE SHOWNF :", baseValue.toString())
Log.v("SECONDVALUE SHOWNF:", secondValue.toString())
//============================================================
callback!!.showNewFormula(value, context)
}
@ -470,29 +435,23 @@ class CalculatorImpl(
private fun String.removeGroupSeparator() = formatter.removeGroupingSeparator(this)
fun getSecondValueV2(): Double {
return this.secondValue
}
//JSON
fun getJson(): JSONObject {
fun getCalculatorStateJson(): JSONObject {
val jsonObj = JSONObject()
jsonObj.put("res", mResult)
jsonObj.put("res", currentResult)
jsonObj.put("previousCalculation", previousCalculation)
jsonObj.put("lastKey", lastKey)
jsonObj.put("lastOperation", lastOperation)
jsonObj.put("baseValue", baseValue)
jsonObj.put("secondValue", secondValue)
jsonObj.put("inputDisplayedFormula", inputDisplayedFormula)
return jsonObj
}
private fun setFromSaveInstanceState(json: String) {
val jsonObject = JSONTokener(json).nextValue() as JSONObject
mResult = jsonObject.getString("res")
currentResult = jsonObject.getString("res")
previousCalculation = jsonObject.getString("previousCalculation")
baseValue = jsonObject.getDouble("baseValue")
secondValue = jsonObject.getDouble("secondValue")
@ -500,6 +459,4 @@ class CalculatorImpl(
lastKey = jsonObject.getString("lastKey")
lastOperation = jsonObject.getString("lastOperation")
}
}