mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-02-08 15:49:03 +01:00
Merge pull request #305 from inepsie/fix_data_lost_during_rotation
Fix data lost during rotation
This commit is contained in:
commit
6c8d43b540
@ -29,7 +29,7 @@ class MainActivity : SimpleActivity(), Calculator {
|
||||
private var storedUseCommaAsDecimalMark = false
|
||||
private var decimalSeparator = DOT
|
||||
private var groupingSeparator = COMMA
|
||||
|
||||
private var saveCalculatorState: String = ""
|
||||
private lateinit var calc: CalculatorImpl
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@ -39,12 +39,14 @@ class MainActivity : SimpleActivity(), Calculator {
|
||||
appLaunched(BuildConfig.APPLICATION_ID)
|
||||
setupOptionsMenu()
|
||||
refreshMenuItems()
|
||||
|
||||
updateMaterialActivityViews(main_coordinator, null, useTransparentNavigation = false, useTopSearchMenu = false)
|
||||
setupMaterialScrollListener(main_nested_scrollview, main_toolbar)
|
||||
|
||||
calc = CalculatorImpl(this, applicationContext)
|
||||
if (savedInstanceState != null) {
|
||||
saveCalculatorState = savedInstanceState.getCharSequence(CALCULATOR_STATE) as String
|
||||
}
|
||||
|
||||
calc = CalculatorImpl(this, applicationContext, decimalSeparator, groupingSeparator, saveCalculatorState)
|
||||
btn_plus.setOnClickOperation(PLUS)
|
||||
btn_minus.setOnClickOperation(MINUS)
|
||||
btn_multiply.setOnClickOperation(MULTIPLY)
|
||||
@ -53,7 +55,6 @@ class MainActivity : SimpleActivity(), Calculator {
|
||||
btn_power.setOnClickOperation(POWER)
|
||||
btn_root.setOnClickOperation(ROOT)
|
||||
btn_minus.setOnLongClickListener { calc.turnToNegative() }
|
||||
|
||||
btn_clear.setVibratingOnClickListener { calc.handleClear() }
|
||||
btn_clear.setOnLongClickListener {
|
||||
calc.handleReset()
|
||||
@ -69,7 +70,6 @@ class MainActivity : SimpleActivity(), Calculator {
|
||||
btn_equals.setVibratingOnClickListener { calc.handleEquals() }
|
||||
formula.setOnLongClickListener { copyToClipboard(false) }
|
||||
result.setOnLongClickListener { copyToClipboard(true) }
|
||||
|
||||
AutofitHelper.create(result)
|
||||
AutofitHelper.create(formula)
|
||||
storeStateVariables()
|
||||
@ -123,6 +123,11 @@ class MainActivity : SimpleActivity(), Calculator {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(bundle: Bundle) {
|
||||
super.onSaveInstanceState(bundle)
|
||||
bundle.putString(CALCULATOR_STATE, calc.getCalculatorStateJson().toString())
|
||||
}
|
||||
|
||||
private fun setupOptionsMenu() {
|
||||
main_toolbar.setOnMenuItemClickListener { menuItem ->
|
||||
when (menuItem.itemId) {
|
||||
|
@ -6,16 +6,21 @@ import com.simplemobiletools.calculator.models.History
|
||||
import com.simplemobiletools.commons.extensions.showErrorToast
|
||||
import com.simplemobiletools.commons.extensions.toast
|
||||
import net.objecthunter.exp4j.ExpressionBuilder
|
||||
import org.json.JSONObject
|
||||
import org.json.JSONTokener
|
||||
import java.math.BigDecimal
|
||||
|
||||
class CalculatorImpl(
|
||||
calculator: Calculator,
|
||||
private val context: Context,
|
||||
private var decimalSeparator: String = DOT,
|
||||
private var groupingSeparator: String = COMMA
|
||||
private var groupingSeparator: String = COMMA,
|
||||
calculatorState: String = ""
|
||||
) {
|
||||
private var callback: Calculator? = calculator
|
||||
|
||||
private var stateInstance = calculatorState
|
||||
private var currentResult = "0"
|
||||
private var previousCalculation = ""
|
||||
private var baseValue = 0.0
|
||||
private var secondValue = 0.0
|
||||
private var inputDisplayedFormula = "0"
|
||||
@ -24,13 +29,16 @@ class CalculatorImpl(
|
||||
private val operations = listOf("+", "-", "×", "÷", "^", "%", "√")
|
||||
private val operationsRegex = "[-+×÷^%√]".toPattern()
|
||||
private val numbersRegex = "[^0-9,.]".toRegex()
|
||||
|
||||
private val formatter = NumberFormatHelper(
|
||||
decimalSeparator = decimalSeparator, groupingSeparator = groupingSeparator
|
||||
)
|
||||
|
||||
init {
|
||||
showNewResult("0")
|
||||
if (stateInstance != "") {
|
||||
setFromSaveInstanceState(stateInstance)
|
||||
}
|
||||
showNewResult(currentResult)
|
||||
showNewFormula(previousCalculation)
|
||||
}
|
||||
|
||||
private fun addDigit(number: Int) {
|
||||
@ -198,6 +206,7 @@ class CalculatorImpl(
|
||||
|
||||
private fun getSecondValue(): Double {
|
||||
val valueToCheck = inputDisplayedFormula.trimStart('-').removeGroupSeparator()
|
||||
|
||||
var value = valueToCheck.substring(valueToCheck.indexOfAny(operations) + 1)
|
||||
if (value == "") {
|
||||
value = "0"
|
||||
@ -318,10 +327,12 @@ class CalculatorImpl(
|
||||
}
|
||||
|
||||
private fun showNewResult(value: String) {
|
||||
currentResult = value
|
||||
callback!!.showNewResult(value, context)
|
||||
}
|
||||
|
||||
private fun showNewFormula(value: String) {
|
||||
previousCalculation = value
|
||||
callback!!.showNewFormula(value, context)
|
||||
}
|
||||
|
||||
@ -421,4 +432,27 @@ class CalculatorImpl(
|
||||
private fun Double.format() = formatter.doubleToString(this)
|
||||
|
||||
private fun String.removeGroupSeparator() = formatter.removeGroupingSeparator(this)
|
||||
|
||||
fun getCalculatorStateJson(): JSONObject {
|
||||
val jsonObj = JSONObject()
|
||||
jsonObj.put(RES, currentResult)
|
||||
jsonObj.put(PREVIOUS_CALCULATION, previousCalculation)
|
||||
jsonObj.put(LAST_KEY, lastKey)
|
||||
jsonObj.put(LAST_OPERATION, lastOperation)
|
||||
jsonObj.put(BASE_VALUE, baseValue)
|
||||
jsonObj.put(SECOND_VALUE, secondValue)
|
||||
jsonObj.put(INPUT_DISPLAYED_FORMULA, inputDisplayedFormula)
|
||||
return jsonObj
|
||||
}
|
||||
|
||||
private fun setFromSaveInstanceState(json: String) {
|
||||
val jsonObject = JSONTokener(json).nextValue() as JSONObject
|
||||
currentResult = jsonObject.getString(RES)
|
||||
previousCalculation = jsonObject.getString(PREVIOUS_CALCULATION)
|
||||
lastKey = jsonObject.getString(LAST_KEY)
|
||||
lastOperation = jsonObject.getString(LAST_OPERATION)
|
||||
baseValue = jsonObject.getDouble(BASE_VALUE)
|
||||
secondValue = jsonObject.getDouble(SECOND_VALUE)
|
||||
inputDisplayedFormula = jsonObject.getString(INPUT_DISPLAYED_FORMULA)
|
||||
}
|
||||
}
|
||||
|
@ -30,3 +30,11 @@ const val COMMA = ","
|
||||
|
||||
// shared prefs
|
||||
const val USE_COMMA_AS_DECIMAL_MARK = "use_comma_as_decimal_mark"
|
||||
const val RES = "res"
|
||||
const val PREVIOUS_CALCULATION = "previousCalculation"
|
||||
const val LAST_KEY = "lastKey"
|
||||
const val LAST_OPERATION = "lastOperation"
|
||||
const val BASE_VALUE = "baseValue"
|
||||
const val SECOND_VALUE = "secondValue"
|
||||
const val INPUT_DISPLAYED_FORMULA = "inputDisplayedFormula"
|
||||
const val CALCULATOR_STATE = "calculatorState"
|
||||
|
Loading…
x
Reference in New Issue
Block a user