Merge branch 'dev-axel-saveInstanceState' into fix_data_lost_during_rotation

This commit is contained in:
Axel Labarre 2023-02-08 18:36:07 +01:00
commit c0c7d6da1d
2 changed files with 77 additions and 20 deletions

View File

@ -33,8 +33,12 @@ class MainActivity : SimpleActivity(), Calculator {
//============================================================
private var savedRes: String = "999"
private var savedRes: String = "0"
private var savedPreviousCalculation = ""
private var savedLastKey: String = ""
private var savedLastOperation: String = ""
private var savedBaseValue : Double= 5.5
private var savedSecondValue : Double = 7.5
//============================================================
@ -48,14 +52,38 @@ class MainActivity : SimpleActivity(), Calculator {
refreshMenuItems()
//============================================================
//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", "123") as String
savedPreviousCalculation = savedInstanceState?.getCharSequence("savedPreviousCalculation", "") 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")
}
Log.v("MainActivity", "LOG NO IF TEST");
calc = CalculatorImpl(this, applicationContext, savedRes, savedPreviousCalculation)
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())
calc = CalculatorImpl(this, applicationContext,decimalSeparator, groupingSeparator, savedRes, savedPreviousCalculation, savedLastKey, savedLastOperation, savedBaseValue, savedSecondValue)
//============================================================
@ -266,6 +294,10 @@ class MainActivity : SimpleActivity(), Calculator {
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.getSecondValue())
}
//============================================================

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.calculator.helpers
import android.content.Context
import android.util.Log
import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.models.History
import com.simplemobiletools.commons.extensions.showErrorToast
@ -11,26 +12,37 @@ import java.math.BigDecimal
class CalculatorImpl(
calculator: Calculator,
private val context: Context,
//============================================================
var res: String,
savedLastOperation: String,
//============================================================
private var decimalSeparator: String = DOT,
private var groupingSeparator: String = COMMA
private var groupingSeparator: String = COMMA,
//============================================================
aRes: String = "123",
aSavedLastOperation: String = "",
aLastKey: String = "",
aLastOperation: String = "",
aBaseValue: Double = 0.0,
aSecondValue: Double = 99.0
//============================================================
) {
private var callback: Calculator? = calculator
//============================================================
// Trying Fix it
public var mResult = res
public var previousCalculation = savedLastOperation
public var mResult = aRes
public var previousCalculation = aSavedLastOperation
public var lastKey = aLastKey
public var lastOperation = aLastOperation
public var baseValue = aBaseValue
private var secondValue = aSecondValue
//============================================================
private var baseValue = 0.0
private var secondValue = 0.0
//private var baseValue = 0.0
//private var secondValue = 0.0
private var inputDisplayedFormula = "0"
private var lastKey = ""
private var lastOperation = ""
//private var lastKey = ""
//private var lastOperation = ""
private val operations = listOf("+", "-", "×", "÷", "^", "%", "")
private val operationsRegex = "[-+×÷^%√]".toPattern()
private val numbersRegex = "[^0-9,.]".toRegex()
@ -41,6 +53,8 @@ class CalculatorImpl(
init {
//============================================================
Log.v("BASEVALUE INIT :", baseValue.toString())
Log.v("SECONDVALUE INIT :", secondValue.toString())
//showNewResult("0")
showNewResult(mResult)
showNewFormula(previousCalculation)
@ -210,7 +224,7 @@ class CalculatorImpl(
lastKey = EQUALS
}
private fun getSecondValue(): Double {
public fun getSecondValue(): Double {
val valueToCheck = inputDisplayedFormula.trimStart('-').removeGroupSeparator()
var value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
if (value == "") {
@ -230,6 +244,8 @@ 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 != "" }
@ -238,11 +254,14 @@ class CalculatorImpl(
}
baseValue = parts.first().toDouble()
if (inputDisplayedFormula.startsWith("-")) {
baseValue *= -1
}
secondValue = parts.getOrNull(1)?.toDouble() ?: secondValue
}
if (lastOperation != "") {
@ -287,10 +306,10 @@ class CalculatorImpl(
//============================================================
mResult = result.format()
//mResult = result.format()
Log.v("CalculResult", result.format())
//============================================================
showNewResult(result.format())
val newFormula = "${baseValue.format()}$sign${secondValue.format()}"
HistoryHelper(context).insertOrUpdateHistoryEntry(
@ -299,7 +318,7 @@ class CalculatorImpl(
showNewFormula(newFormula)
//============================================================
previousCalculation = newFormula
//previousCalculation = newFormula
//============================================================
inputDisplayedFormula = result.format()
@ -337,10 +356,16 @@ class CalculatorImpl(
}
private fun showNewResult(value: String) {
//============================================================
mResult = value;
//============================================================
callback!!.showNewResult(value, context)
}
private fun showNewFormula(value: String) {
//============================================================
previousCalculation = value;
//============================================================
callback!!.showNewFormula(value, context)
}