mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-06-05 21:49:13 +02:00
cleaning code
This commit is contained in:
@ -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).
|
|
@ -3,7 +3,6 @@ package com.simplemobiletools.calculator.activities
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import androidx.core.content.res.ResourcesCompat
|
import androidx.core.content.res.ResourcesCompat
|
||||||
@ -31,18 +30,7 @@ class MainActivity : SimpleActivity(), Calculator {
|
|||||||
private var decimalSeparator = DOT
|
private var decimalSeparator = DOT
|
||||||
private var groupingSeparator = COMMA
|
private var groupingSeparator = COMMA
|
||||||
|
|
||||||
//============================================================
|
private var saveCalculatorState: String = ""
|
||||||
|
|
||||||
//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 lateinit var calc: CalculatorImpl
|
private lateinit var calc: CalculatorImpl
|
||||||
|
|
||||||
@ -59,34 +47,12 @@ class MainActivity : SimpleActivity(), Calculator {
|
|||||||
updateMaterialActivityViews(main_coordinator, null, useTransparentNavigation = false, useTopSearchMenu = false)
|
updateMaterialActivityViews(main_coordinator, null, useTransparentNavigation = false, useTopSearchMenu = false)
|
||||||
setupMaterialScrollListener(main_nested_scrollview, main_toolbar)
|
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) {
|
if (savedInstanceState != null) {
|
||||||
Log.v("MainActivity", "LOG TEST");
|
saveCalculatorState = savedInstanceState.getCharSequence("calculatorState") as String
|
||||||
//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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
calc = CalculatorImpl(this, applicationContext, decimalSeparator, groupingSeparator, json)
|
calc = CalculatorImpl(this, applicationContext, decimalSeparator, groupingSeparator, saveCalculatorState)
|
||||||
|
|
||||||
//============================================================
|
|
||||||
|
|
||||||
btn_plus.setOnClickOperation(PLUS)
|
btn_plus.setOnClickOperation(PLUS)
|
||||||
btn_minus.setOnClickOperation(MINUS)
|
btn_minus.setOnClickOperation(MINUS)
|
||||||
@ -288,21 +254,9 @@ class MainActivity : SimpleActivity(), Calculator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//============================================================
|
|
||||||
|
|
||||||
override fun onSaveInstanceState(bundle: Bundle) {
|
override fun onSaveInstanceState(bundle: Bundle) {
|
||||||
super.onSaveInstanceState(bundle)
|
super.onSaveInstanceState(bundle)
|
||||||
/*
|
|
||||||
bundle.putString("res", calc.mResult)
|
bundle.putString("calculatorState", calc.getCalculatorStateJson().toString())
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
//============================================================
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package com.simplemobiletools.calculator.helpers
|
package com.simplemobiletools.calculator.helpers
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.util.Log
|
|
||||||
import com.google.gson.JsonObject
|
|
||||||
import com.simplemobiletools.calculator.R
|
import com.simplemobiletools.calculator.R
|
||||||
import com.simplemobiletools.calculator.models.History
|
import com.simplemobiletools.calculator.models.History
|
||||||
import com.simplemobiletools.commons.extensions.showErrorToast
|
import com.simplemobiletools.commons.extensions.showErrorToast
|
||||||
@ -17,17 +15,11 @@ class CalculatorImpl(
|
|||||||
private val context: Context,
|
private val context: Context,
|
||||||
private var decimalSeparator: String = DOT,
|
private var decimalSeparator: String = DOT,
|
||||||
private var groupingSeparator: String = COMMA,
|
private var groupingSeparator: String = COMMA,
|
||||||
private var json: String = ""
|
calculatorState: String = ""
|
||||||
|
|
||||||
|
|
||||||
) {
|
) {
|
||||||
private var callback: Calculator? = calculator
|
private var callback: Calculator? = calculator
|
||||||
|
private var stateInstance = calculatorState
|
||||||
//============================================================
|
private var currentResult = "0"
|
||||||
|
|
||||||
//============================================================
|
|
||||||
private var jsonObj = json
|
|
||||||
private var mResult = "0"
|
|
||||||
private var previousCalculation = ""
|
private var previousCalculation = ""
|
||||||
private var baseValue = 0.0
|
private var baseValue = 0.0
|
||||||
private var secondValue = 0.0
|
private var secondValue = 0.0
|
||||||
@ -42,16 +34,11 @@ class CalculatorImpl(
|
|||||||
)
|
)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
//============================================================
|
if (stateInstance != "") {
|
||||||
if (jsonObj != "") {
|
setFromSaveInstanceState(stateInstance)
|
||||||
setFromSaveInstanceState(jsonObj)
|
|
||||||
}
|
}
|
||||||
Log.v("BASEVALUE INIT :", baseValue.toString())
|
showNewResult(currentResult)
|
||||||
Log.v("SECONDVALUE INIT :", secondValue.toString())
|
|
||||||
//showNewResult("0")
|
|
||||||
showNewResult(mResult)
|
|
||||||
showNewFormula(previousCalculation)
|
showNewFormula(previousCalculation)
|
||||||
//============================================================
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addDigit(number: Int) {
|
private fun addDigit(number: Int) {
|
||||||
@ -219,10 +206,8 @@ class CalculatorImpl(
|
|||||||
|
|
||||||
private fun getSecondValue(): Double {
|
private fun getSecondValue(): Double {
|
||||||
val valueToCheck = inputDisplayedFormula.trimStart('-').removeGroupSeparator()
|
val valueToCheck = inputDisplayedFormula.trimStart('-').removeGroupSeparator()
|
||||||
Log.v("VALUEToCheck GETSV :", valueToCheck)
|
|
||||||
|
|
||||||
var value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
|
var value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
|
||||||
Log.v("VALUE GETSV :", value)
|
|
||||||
if (value == "") {
|
if (value == "") {
|
||||||
value = "0"
|
value = "0"
|
||||||
}
|
}
|
||||||
@ -240,8 +225,6 @@ class CalculatorImpl(
|
|||||||
baseValue = 1.0
|
baseValue = 1.0
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.v("LASKEY CR :", lastKey)
|
|
||||||
|
|
||||||
if (lastKey != EQUALS) {
|
if (lastKey != EQUALS) {
|
||||||
val valueToCheck = inputDisplayedFormula.trimStart('-').removeGroupSeparator()
|
val valueToCheck = inputDisplayedFormula.trimStart('-').removeGroupSeparator()
|
||||||
val parts = valueToCheck.split(operationsRegex).filter { it != "" }
|
val parts = valueToCheck.split(operationsRegex).filter { it != "" }
|
||||||
@ -304,14 +287,6 @@ class CalculatorImpl(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//============================================================
|
|
||||||
|
|
||||||
//mResult = result.format()
|
|
||||||
Log.v("CalculResult", result.format())
|
|
||||||
Log.v("BASEVALUE CR :", baseValue.toString())
|
|
||||||
Log.v("SECONDVALUE CR :", secondValue.toString())
|
|
||||||
|
|
||||||
//============================================================
|
|
||||||
showNewResult(result.format())
|
showNewResult(result.format())
|
||||||
val newFormula = "${baseValue.format()}$sign${secondValue.format()}"
|
val newFormula = "${baseValue.format()}$sign${secondValue.format()}"
|
||||||
HistoryHelper(context).insertOrUpdateHistoryEntry(
|
HistoryHelper(context).insertOrUpdateHistoryEntry(
|
||||||
@ -319,10 +294,6 @@ class CalculatorImpl(
|
|||||||
)
|
)
|
||||||
showNewFormula(newFormula)
|
showNewFormula(newFormula)
|
||||||
|
|
||||||
//============================================================
|
|
||||||
//previousCalculation = newFormula
|
|
||||||
//============================================================
|
|
||||||
|
|
||||||
inputDisplayedFormula = result.format()
|
inputDisplayedFormula = result.format()
|
||||||
baseValue = result
|
baseValue = result
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@ -358,18 +329,12 @@ class CalculatorImpl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun showNewResult(value: String) {
|
private fun showNewResult(value: String) {
|
||||||
//============================================================
|
currentResult = value
|
||||||
mResult = value
|
|
||||||
//============================================================
|
|
||||||
callback!!.showNewResult(value, context)
|
callback!!.showNewResult(value, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showNewFormula(value: String) {
|
private fun showNewFormula(value: String) {
|
||||||
//============================================================
|
|
||||||
previousCalculation = value
|
previousCalculation = value
|
||||||
Log.v("BASEVALUE SHOWNF :", baseValue.toString())
|
|
||||||
Log.v("SECONDVALUE SHOWNF:", secondValue.toString())
|
|
||||||
//============================================================
|
|
||||||
callback!!.showNewFormula(value, context)
|
callback!!.showNewFormula(value, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -470,29 +435,23 @@ class CalculatorImpl(
|
|||||||
|
|
||||||
private fun String.removeGroupSeparator() = formatter.removeGroupingSeparator(this)
|
private fun String.removeGroupSeparator() = formatter.removeGroupingSeparator(this)
|
||||||
|
|
||||||
fun getSecondValueV2(): Double {
|
|
||||||
return this.secondValue
|
|
||||||
}
|
|
||||||
|
|
||||||
|
fun getCalculatorStateJson(): JSONObject {
|
||||||
//JSON
|
|
||||||
fun getJson(): JSONObject {
|
|
||||||
|
|
||||||
val jsonObj = JSONObject()
|
val jsonObj = JSONObject()
|
||||||
jsonObj.put("res", mResult)
|
jsonObj.put("res", currentResult)
|
||||||
jsonObj.put("previousCalculation", previousCalculation)
|
jsonObj.put("previousCalculation", previousCalculation)
|
||||||
jsonObj.put("lastKey", lastKey)
|
jsonObj.put("lastKey", lastKey)
|
||||||
jsonObj.put("lastOperation", lastOperation)
|
jsonObj.put("lastOperation", lastOperation)
|
||||||
jsonObj.put("baseValue", baseValue)
|
jsonObj.put("baseValue", baseValue)
|
||||||
jsonObj.put("secondValue", secondValue)
|
jsonObj.put("secondValue", secondValue)
|
||||||
jsonObj.put("inputDisplayedFormula", inputDisplayedFormula)
|
jsonObj.put("inputDisplayedFormula", inputDisplayedFormula)
|
||||||
|
|
||||||
return jsonObj
|
return jsonObj
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setFromSaveInstanceState(json: String) {
|
private fun setFromSaveInstanceState(json: String) {
|
||||||
val jsonObject = JSONTokener(json).nextValue() as JSONObject
|
val jsonObject = JSONTokener(json).nextValue() as JSONObject
|
||||||
mResult = jsonObject.getString("res")
|
currentResult = jsonObject.getString("res")
|
||||||
previousCalculation = jsonObject.getString("previousCalculation")
|
previousCalculation = jsonObject.getString("previousCalculation")
|
||||||
baseValue = jsonObject.getDouble("baseValue")
|
baseValue = jsonObject.getDouble("baseValue")
|
||||||
secondValue = jsonObject.getDouble("secondValue")
|
secondValue = jsonObject.getDouble("secondValue")
|
||||||
@ -500,6 +459,4 @@ class CalculatorImpl(
|
|||||||
lastKey = jsonObject.getString("lastKey")
|
lastKey = jsonObject.getString("lastKey")
|
||||||
lastOperation = jsonObject.getString("lastOperation")
|
lastOperation = jsonObject.getString("lastOperation")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user