Handle state saving in ConverterView

This commit is contained in:
Ensar Sarajčić 2023-09-14 12:14:13 +02:00
parent 381dd7af38
commit 4c5a355431
3 changed files with 37 additions and 3 deletions

View File

@ -10,6 +10,7 @@ import com.simplemobiletools.calculator.databinding.ActivityUnitConverterBinding
import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.calculator.extensions.updateViewColors
import com.simplemobiletools.calculator.helpers.COMMA
import com.simplemobiletools.calculator.helpers.CONVERTER_STATE
import com.simplemobiletools.calculator.helpers.DOT
import com.simplemobiletools.calculator.helpers.converters.Converter
import com.simplemobiletools.commons.extensions.getProperTextColor
@ -65,6 +66,12 @@ class UnitConverterActivity : SimpleActivity() {
binding.viewUnitConverter.viewConverter.root.setConverter(converter)
binding.unitConverterToolbar.setTitle(converter.nameResId)
if (savedInstanceState != null) {
savedInstanceState.getBundle(CONVERTER_STATE)?.also {
binding.viewUnitConverter.viewConverter.root.restoreFromSavedState(it)
}
}
}
private fun setupOptionsMenu() {
@ -112,6 +119,11 @@ class UnitConverterActivity : SimpleActivity() {
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBundle(CONVERTER_STATE, binding.viewUnitConverter.viewConverter.root.saveState())
}
private fun checkHaptic(view: View) {
if (vibrateOnButtonPress) {
view.performHapticFeedback()

View File

@ -30,6 +30,8 @@ const val COMMA = ","
// shared prefs
const val USE_COMMA_AS_DECIMAL_MARK = "use_comma_as_decimal_mark"
// calculator state
const val RES = "res"
const val PREVIOUS_CALCULATION = "previousCalculation"
const val LAST_KEY = "lastKey"
@ -38,3 +40,9 @@ const val BASE_VALUE = "baseValue"
const val SECOND_VALUE = "secondValue"
const val INPUT_DISPLAYED_FORMULA = "inputDisplayedFormula"
const val CALCULATOR_STATE = "calculatorState"
// converter state
const val TOP_UNIT = "top_unit"
const val BOTTOM_UNIT = "bottom_unit"
const val CONVERTER_VALUE = "converter_value"
const val CONVERTER_STATE = "converter_state"

View File

@ -6,6 +6,7 @@ import android.content.res.ColorStateList
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.LayerDrawable
import android.graphics.drawable.RippleDrawable
import android.os.Bundle
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
@ -13,9 +14,7 @@ import androidx.core.content.res.ResourcesCompat
import androidx.core.widget.TextViewCompat
import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.databinding.ViewConverterBinding
import com.simplemobiletools.calculator.helpers.COMMA
import com.simplemobiletools.calculator.helpers.DOT
import com.simplemobiletools.calculator.helpers.NumberFormatHelper
import com.simplemobiletools.calculator.helpers.*
import com.simplemobiletools.calculator.helpers.converters.Converter
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.*
@ -242,4 +241,19 @@ class ConverterView @JvmOverloads constructor(
}
}
}
fun saveState(): Bundle = Bundle().apply {
putInt(TOP_UNIT, converter!!.units.indexOf(topUnit!!))
putInt(BOTTOM_UNIT, converter!!.units.indexOf(bottomUnit!!))
putString(CONVERTER_VALUE, binding.topUnitText.text.toString())
}
fun restoreFromSavedState(state: Bundle) {
topUnit = converter!!.units[state.getInt(TOP_UNIT)]
bottomUnit = converter!!.units[state.getInt(BOTTOM_UNIT)]
binding.topUnitText.text = state.getString(CONVERTER_VALUE)
updateBottomValue()
updateUnitLabelsAndSymbols()
}
}