diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/activities/UnitConverterActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/activities/UnitConverterActivity.kt index 64dd5353..177e3cc8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/activities/UnitConverterActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/activities/UnitConverterActivity.kt @@ -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() diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/Constants.kt index aa4c0362..225bdcb8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/Constants.kt @@ -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" diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/views/ConverterView.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/views/ConverterView.kt index 16c44193..5da1f2b4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/views/ConverterView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/views/ConverterView.kt @@ -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() + } }