Store last selected units for each converter
This commit is contained in:
parent
f944266499
commit
4a14f7b121
12 changed files with 210 additions and 83 deletions
|
@ -71,6 +71,11 @@ class UnitConverterActivity : SimpleActivity() {
|
|||
savedInstanceState.getBundle(CONVERTER_STATE)?.also {
|
||||
binding.viewUnitConverter.viewConverter.root.restoreFromSavedState(it)
|
||||
}
|
||||
} else {
|
||||
val storedState = config.getLastConverterUnits(converter)
|
||||
if (storedState != null) {
|
||||
binding.viewUnitConverter.viewConverter.root.updateUnits(storedState.topUnit, storedState.bottomUnit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@ package com.simplemobiletools.calculator.helpers
|
|||
|
||||
import android.content.Context
|
||||
import com.simplemobiletools.calculator.extensions.sharedPreferencesCallback
|
||||
import com.simplemobiletools.calculator.helpers.converters.Converter
|
||||
import com.simplemobiletools.calculator.models.ConverterUnitsState
|
||||
import com.simplemobiletools.commons.helpers.BaseConfig
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.filterNotNull
|
||||
|
@ -15,6 +17,26 @@ class Config(context: Context) : BaseConfig(context) {
|
|||
get() = prefs.getBoolean(USE_COMMA_AS_DECIMAL_MARK, getDecimalSeparator() == COMMA)
|
||||
set(useCommaAsDecimalMark) = prefs.edit().putBoolean(USE_COMMA_AS_DECIMAL_MARK, useCommaAsDecimalMark).apply()
|
||||
|
||||
fun getLastConverterUnits(converter: Converter): ConverterUnitsState? {
|
||||
val storedState = prefs.getString("$CONVERTER_UNITS_PREFIX.${converter.key}", null)
|
||||
return if (!storedState.isNullOrEmpty()) {
|
||||
val parts = storedState.split(",").map { part ->
|
||||
converter.units.first { it.key == part }
|
||||
}
|
||||
if (parts.size == 2) {
|
||||
ConverterUnitsState(parts[0], parts[1])
|
||||
} else {
|
||||
null
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun putLastConverterUnits(converter: Converter, topUnit: Converter.Unit, bottomUnit: Converter.Unit) {
|
||||
prefs.edit().putString("$CONVERTER_UNITS_PREFIX.${converter.key}", "${topUnit.key},${bottomUnit.key}").apply()
|
||||
}
|
||||
|
||||
val preventPhoneFromSleepingFlow: Flow<Boolean> = prefs.run { sharedPreferencesCallback { preventPhoneFromSleeping } }.filterNotNull()
|
||||
val vibrateOnButtonPressFlow: Flow<Boolean> = prefs.run { sharedPreferencesCallback { vibrateOnButtonPress } }.filterNotNull()
|
||||
val wasUseEnglishToggledFlow: Flow<Boolean> = prefs.run { sharedPreferencesCallback { wasUseEnglishToggled } }.filterNotNull()
|
||||
|
|
|
@ -30,6 +30,7 @@ const val COMMA = ","
|
|||
|
||||
// shared prefs
|
||||
const val USE_COMMA_AS_DECIMAL_MARK = "use_comma_as_decimal_mark"
|
||||
const val CONVERTER_UNITS_PREFIX = "converter_last_units"
|
||||
|
||||
// calculator state
|
||||
const val RES = "res"
|
||||
|
|
|
@ -5,66 +5,77 @@ import com.simplemobiletools.calculator.R
|
|||
object AreaConverter : Converter {
|
||||
override val nameResId: Int = R.string.unit_area
|
||||
override val imageResId: Int = R.drawable.ic_box_vector
|
||||
override val key: String = "AreaConverter"
|
||||
|
||||
sealed class Unit(nameResId: Int, symbolResId: Int, factor: Double) : Converter.Unit(nameResId, symbolResId, factor) {
|
||||
sealed class Unit(nameResId: Int, symbolResId: Int, factor: Double, key: String) : Converter.Unit(nameResId, symbolResId, factor, key) {
|
||||
data object SquareKilometer : Unit(
|
||||
nameResId = R.string.unit_area_square_kilometer,
|
||||
symbolResId = R.string.unit_area_square_kilometer_symbol,
|
||||
factor = 1000000.0
|
||||
factor = 1000000.0,
|
||||
key = "SquareKilometer"
|
||||
)
|
||||
|
||||
data object SquareMeter : Unit(
|
||||
nameResId = R.string.unit_area_square_meter,
|
||||
symbolResId = R.string.unit_area_square_meter_symbol,
|
||||
factor = 1.0
|
||||
factor = 1.0,
|
||||
key = "SquareMeter"
|
||||
)
|
||||
|
||||
data object SquareCentimeter : Unit(
|
||||
nameResId = R.string.unit_area_square_centimeter,
|
||||
symbolResId = R.string.unit_area_square_centimeter_symbol,
|
||||
factor = 0.0001
|
||||
factor = 0.0001,
|
||||
key = "SquareCentimeter"
|
||||
)
|
||||
|
||||
data object SquareMillimeter : Unit(
|
||||
nameResId = R.string.unit_area_square_millimeter,
|
||||
symbolResId = R.string.unit_area_square_millimeter_symbol,
|
||||
factor = 0.000001
|
||||
factor = 0.000001,
|
||||
key = "SquareMillimeter"
|
||||
)
|
||||
|
||||
data object SquareMile : Unit(
|
||||
nameResId = R.string.unit_area_square_mile,
|
||||
symbolResId = R.string.unit_area_square_mile_symbol,
|
||||
factor = 2_589_988.110336
|
||||
factor = 2_589_988.110336,
|
||||
key = "SquareMile"
|
||||
)
|
||||
|
||||
data object SquareYard : Unit(
|
||||
nameResId = R.string.unit_area_square_yard,
|
||||
symbolResId = R.string.unit_area_square_yard_symbol,
|
||||
factor = 0.83612736
|
||||
factor = 0.83612736,
|
||||
key = "SquareYard"
|
||||
)
|
||||
|
||||
data object SquareFoot : Unit(
|
||||
nameResId = R.string.unit_area_square_foot,
|
||||
symbolResId = R.string.unit_area_square_foot_symbol,
|
||||
factor = 0.09290304
|
||||
factor = 0.09290304,
|
||||
key = "SquareFoot"
|
||||
)
|
||||
|
||||
data object SquareInch : Unit(
|
||||
nameResId = R.string.unit_area_square_inch,
|
||||
symbolResId = R.string.unit_area_square_inch_symbol,
|
||||
factor = 0.00064516
|
||||
factor = 0.00064516,
|
||||
key = "SquareInch"
|
||||
)
|
||||
|
||||
data object Acre : Unit(
|
||||
nameResId = R.string.unit_area_acre,
|
||||
symbolResId = R.string.unit_area_acre_symbol,
|
||||
factor = 4_046.8564224
|
||||
factor = 4_046.8564224,
|
||||
key = "Acre"
|
||||
)
|
||||
|
||||
data object Hectare : Unit(
|
||||
nameResId = R.string.unit_area_hectare,
|
||||
symbolResId = R.string.unit_area_hectare_symbol,
|
||||
factor = 10_000.0
|
||||
factor = 10_000.0,
|
||||
key = "Hectare"
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,8 @@ interface Converter {
|
|||
val defaultTopUnit: Unit
|
||||
val defaultBottomUnit: Unit
|
||||
|
||||
val key: String
|
||||
|
||||
fun convert(from: ValueWithUnit<Unit>, to: Unit): ValueWithUnit<Unit> {
|
||||
return ValueWithUnit(to.fromBase(from.unit.toBase(from.value)), to)
|
||||
}
|
||||
|
@ -28,7 +30,8 @@ interface Converter {
|
|||
open class Unit(
|
||||
val nameResId: Int,
|
||||
val symbolResId: Int,
|
||||
val factor: Double
|
||||
val factor: Double,
|
||||
val key: String
|
||||
) {
|
||||
|
||||
open fun toBase(value: Double) = value * factor
|
||||
|
|
|
@ -5,102 +5,119 @@ import com.simplemobiletools.calculator.R
|
|||
object LengthConverter : Converter {
|
||||
override val nameResId: Int = R.string.unit_length
|
||||
override val imageResId: Int = R.drawable.ic_height_vector
|
||||
override val key: String = "LengthConverter"
|
||||
|
||||
sealed class Unit(nameResId: Int, symbolResId: Int, factor: Double) : Converter.Unit(nameResId, symbolResId, factor) {
|
||||
sealed class Unit(nameResId: Int, symbolResId: Int, factor: Double, key: String) : Converter.Unit(nameResId, symbolResId, factor, key) {
|
||||
data object Kilometer : Unit(
|
||||
nameResId = R.string.unit_length_kilometer,
|
||||
symbolResId = R.string.unit_length_kilometer_symbol,
|
||||
factor = 1000.0
|
||||
factor = 1000.0,
|
||||
key = "Kilometer"
|
||||
)
|
||||
|
||||
data object Meter : Unit(
|
||||
nameResId = R.string.unit_length_meter,
|
||||
symbolResId = R.string.unit_length_meter_symbol,
|
||||
factor = 1.0
|
||||
factor = 1.0,
|
||||
key = "Meter"
|
||||
)
|
||||
|
||||
data object Centimeter : Unit(
|
||||
nameResId = R.string.unit_length_centimeter,
|
||||
symbolResId = R.string.unit_length_centimeter_symbol,
|
||||
factor = 0.01
|
||||
factor = 0.01,
|
||||
key = "Centimeter"
|
||||
)
|
||||
|
||||
data object Millimeter : Unit(
|
||||
nameResId = R.string.unit_length_millimeter,
|
||||
symbolResId = R.string.unit_length_millimeter_symbol,
|
||||
factor = 0.001
|
||||
factor = 0.001,
|
||||
key = "Millimeter"
|
||||
)
|
||||
|
||||
data object Micrometer : Unit(
|
||||
nameResId = R.string.unit_length_micrometer,
|
||||
symbolResId = R.string.unit_length_micrometer_symbol,
|
||||
factor = 0.000001
|
||||
factor = 0.000001,
|
||||
key = "Micrometer"
|
||||
)
|
||||
|
||||
data object Nanometer : Unit(
|
||||
nameResId = R.string.unit_length_nanometer,
|
||||
symbolResId = R.string.unit_length_nanometer_symbol,
|
||||
factor = 0.000000001
|
||||
factor = 0.000000001,
|
||||
key = "Nanometer"
|
||||
)
|
||||
|
||||
data object Angstrom : Unit(
|
||||
nameResId = R.string.unit_length_angstrom,
|
||||
symbolResId = R.string.unit_length_angstrom_symbol,
|
||||
factor = 1e-10
|
||||
factor = 1e-10,
|
||||
key = "Angstrom"
|
||||
)
|
||||
|
||||
data object Mile : Unit(
|
||||
nameResId = R.string.unit_length_mile,
|
||||
symbolResId = R.string.unit_length_mile_symbol,
|
||||
factor = 1_609.344
|
||||
factor = 1_609.344,
|
||||
key = "Mile"
|
||||
)
|
||||
|
||||
data object Yard : Unit(
|
||||
nameResId = R.string.unit_length_yard,
|
||||
symbolResId = R.string.unit_length_yard_symbol,
|
||||
factor = 0.9144
|
||||
factor = 0.9144,
|
||||
key = "Yard"
|
||||
)
|
||||
|
||||
data object Foot : Unit(
|
||||
nameResId = R.string.unit_length_foot,
|
||||
symbolResId = R.string.unit_length_foot_symbol,
|
||||
factor = 0.3048
|
||||
factor = 0.3048,
|
||||
key = "Foot"
|
||||
)
|
||||
|
||||
data object Inch : Unit(
|
||||
nameResId = R.string.unit_length_inch,
|
||||
symbolResId = R.string.unit_length_inch_symbol,
|
||||
factor = 0.0254
|
||||
factor = 0.0254,
|
||||
key = "Inch"
|
||||
)
|
||||
|
||||
data object Fathom : Unit(
|
||||
nameResId = R.string.unit_length_fathom,
|
||||
symbolResId = R.string.unit_length_fathom_symbol,
|
||||
factor = 1.852
|
||||
factor = 1.852,
|
||||
key = "Fathom"
|
||||
)
|
||||
|
||||
data object NauticalMile : Unit(
|
||||
nameResId = R.string.unit_length_nautical_mile,
|
||||
symbolResId = R.string.unit_length_nautical_mile_symbol,
|
||||
factor = 1_852.0
|
||||
factor = 1_852.0,
|
||||
key = "NauticalMile"
|
||||
)
|
||||
|
||||
data object AstronomicalUnit : Unit(
|
||||
nameResId = R.string.unit_length_astronomical_unit,
|
||||
symbolResId = R.string.unit_length_astronomical_unit_symbol,
|
||||
factor = 1.495979e+11
|
||||
factor = 1.495979e+11,
|
||||
key = "AstronomicalUnit"
|
||||
)
|
||||
|
||||
data object Parsec : Unit(
|
||||
nameResId = R.string.unit_length_parsec,
|
||||
symbolResId = R.string.unit_length_parsec_symbol,
|
||||
factor = 3.0857e+16
|
||||
factor = 3.0857e+16,
|
||||
key = "Parsec"
|
||||
)
|
||||
|
||||
data object LightYear : Unit(
|
||||
nameResId = R.string.unit_length_light_year,
|
||||
symbolResId = R.string.unit_length_light_year_symbol,
|
||||
factor = 9.4607e+15
|
||||
factor = 9.4607e+15,
|
||||
key = "LightYear"
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -5,90 +5,105 @@ import com.simplemobiletools.calculator.R
|
|||
object MassConverter : Converter {
|
||||
override val nameResId: Int = R.string.unit_mass
|
||||
override val imageResId: Int = R.drawable.ic_scale_vector
|
||||
override val key: String = "MassConverter"
|
||||
|
||||
sealed class Unit(nameResId: Int, symbolResId: Int, factor: Double) : Converter.Unit(nameResId, symbolResId, factor) {
|
||||
sealed class Unit(nameResId: Int, symbolResId: Int, factor: Double, key: String) : Converter.Unit(nameResId, symbolResId, factor, key) {
|
||||
data object Gram : Unit(
|
||||
nameResId = R.string.unit_mass_gram,
|
||||
symbolResId = R.string.unit_mass_gram_symbol,
|
||||
factor = 0.001
|
||||
factor = 0.001,
|
||||
key = "Gram"
|
||||
)
|
||||
|
||||
data object Kilogram : Unit(
|
||||
nameResId = R.string.unit_mass_kilogram,
|
||||
symbolResId = R.string.unit_mass_kilogram_symbol,
|
||||
factor = 1.0
|
||||
factor = 1.0,
|
||||
key = "Kilogram"
|
||||
)
|
||||
|
||||
data object Milligram : Unit(
|
||||
nameResId = R.string.unit_mass_milligram,
|
||||
symbolResId = R.string.unit_mass_milligram_symbol,
|
||||
factor = 0.000001
|
||||
factor = 0.000001,
|
||||
key = "Milligram"
|
||||
)
|
||||
|
||||
data object Microgram : Unit(
|
||||
nameResId = R.string.unit_mass_microgram,
|
||||
symbolResId = R.string.unit_mass_microgram_symbol,
|
||||
factor = 0.000000001
|
||||
factor = 0.000000001,
|
||||
key = "Microgram"
|
||||
)
|
||||
|
||||
data object Tonne : Unit(
|
||||
nameResId = R.string.unit_mass_tonne,
|
||||
symbolResId = R.string.unit_mass_tonne_symbol,
|
||||
factor = 1_000.0
|
||||
factor = 1_000.0,
|
||||
key = "Tonne"
|
||||
)
|
||||
|
||||
data object Pound : Unit(
|
||||
nameResId = R.string.unit_mass_pound,
|
||||
symbolResId = R.string.unit_mass_pound_symbol,
|
||||
factor = 0.45359237
|
||||
factor = 0.45359237,
|
||||
key = "Pound"
|
||||
)
|
||||
|
||||
data object Ounce : Unit(
|
||||
nameResId = R.string.unit_mass_ounce,
|
||||
symbolResId = R.string.unit_mass_ounce_symbol,
|
||||
factor = 0.028349523125
|
||||
factor = 0.028349523125,
|
||||
key = "Ounce"
|
||||
)
|
||||
|
||||
data object Grain : Unit(
|
||||
nameResId = R.string.unit_mass_grain,
|
||||
symbolResId = R.string.unit_mass_grain_symbol,
|
||||
factor = 0.00006479891
|
||||
factor = 0.00006479891,
|
||||
key = "Grain"
|
||||
)
|
||||
|
||||
data object Dram : Unit(
|
||||
nameResId = R.string.unit_mass_dram,
|
||||
symbolResId = R.string.unit_mass_dram_symbol,
|
||||
factor = 0.0017718451953125
|
||||
factor = 0.0017718451953125,
|
||||
key = "Dram"
|
||||
)
|
||||
|
||||
data object Stone : Unit(
|
||||
nameResId = R.string.unit_mass_stone,
|
||||
symbolResId = R.string.unit_mass_stone_symbol,
|
||||
factor = 6.35029318
|
||||
factor = 6.35029318,
|
||||
key = "Stone"
|
||||
)
|
||||
|
||||
data object LongTon : Unit(
|
||||
nameResId = R.string.unit_mass_long_ton,
|
||||
symbolResId = R.string.unit_mass_long_ton_symbol,
|
||||
factor = 1_016.0469088
|
||||
factor = 1_016.0469088,
|
||||
key = "LongTon"
|
||||
)
|
||||
|
||||
data object ShortTon : Unit(
|
||||
nameResId = R.string.unit_mass_short_ton,
|
||||
symbolResId = R.string.unit_mass_short_ton_symbol,
|
||||
factor = 907.18474
|
||||
factor = 907.18474,
|
||||
key = "ShortTon"
|
||||
)
|
||||
|
||||
data object Carat : Unit(
|
||||
nameResId = R.string.unit_mass_carat,
|
||||
symbolResId = R.string.unit_mass_carat_symbol,
|
||||
factor = 0.0002051965483
|
||||
factor = 0.0002051965483,
|
||||
key = "Carat"
|
||||
)
|
||||
|
||||
data object CaratMetric : Unit(
|
||||
nameResId = R.string.unit_mass_carat_metric,
|
||||
symbolResId = R.string.unit_mass_carat_metric_symbol,
|
||||
factor = 0.0002
|
||||
factor = 0.0002,
|
||||
key = "CaratMetric"
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -5,13 +5,15 @@ import com.simplemobiletools.calculator.R
|
|||
object TemperatureConverter : Converter {
|
||||
override val nameResId: Int = R.string.unit_temperature
|
||||
override val imageResId: Int = R.drawable.ic_thermostat_vector
|
||||
override val key: String = "TemperatureConverter"
|
||||
|
||||
sealed class Unit(nameResId: Int, symbolResId: Int, factor: Double) : Converter.Unit(nameResId, symbolResId, factor) {
|
||||
sealed class Unit(nameResId: Int, symbolResId: Int, factor: Double, key: String) : Converter.Unit(nameResId, symbolResId, factor, key) {
|
||||
|
||||
data object Celsius : Unit(
|
||||
nameResId = R.string.unit_temperature_celsius,
|
||||
symbolResId = R.string.unit_temperature_celsius_symbol,
|
||||
factor = 1.0
|
||||
factor = 1.0,
|
||||
key = "Celsius"
|
||||
) {
|
||||
const val KELVIN_OFFSET = 273.15
|
||||
|
||||
|
@ -22,7 +24,8 @@ object TemperatureConverter : Converter {
|
|||
data object Fahrenheit : Unit(
|
||||
nameResId = R.string.unit_temperature_fahrenheit,
|
||||
symbolResId = R.string.unit_temperature_fahrenheit_symbol,
|
||||
factor = 9.0 / 5
|
||||
factor = 9.0 / 5,
|
||||
key = "Fahrenheit"
|
||||
) {
|
||||
private const val CELSIUS_OFFSET = 32
|
||||
|
||||
|
@ -34,13 +37,15 @@ object TemperatureConverter : Converter {
|
|||
data object Rankine : Unit(
|
||||
nameResId = R.string.unit_temperature_rankine,
|
||||
symbolResId = R.string.unit_temperature_rankine_symbol,
|
||||
factor = 5.0 / 9
|
||||
factor = 5.0 / 9,
|
||||
key = "Rankine"
|
||||
)
|
||||
|
||||
data object Kelvin : Unit(
|
||||
nameResId = R.string.unit_temperature_kelvin,
|
||||
symbolResId = R.string.unit_temperature_kelvin_symbol,
|
||||
factor = 1.0
|
||||
factor = 1.0,
|
||||
key = "Kelvin"
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -5,8 +5,9 @@ import com.simplemobiletools.calculator.R
|
|||
object TimeConverter : Converter {
|
||||
override val nameResId: Int = R.string.unit_time
|
||||
override val imageResId: Int = com.simplemobiletools.commons.R.drawable.ic_clock_vector
|
||||
override val key: String = "TimeConverter"
|
||||
|
||||
sealed class Unit(nameResId: Int, symbolResId: Int, factor: Double) : Converter.Unit(nameResId, symbolResId, factor) {
|
||||
sealed class Unit(nameResId: Int, symbolResId: Int, factor: Double, key: String) : Converter.Unit(nameResId, symbolResId, factor, key) {
|
||||
companion object {
|
||||
private const val MINUTE = 60.0
|
||||
private const val HOUR = 60.0 * MINUTE
|
||||
|
@ -17,43 +18,50 @@ object TimeConverter : Converter {
|
|||
data object Hour : Unit(
|
||||
nameResId = R.string.unit_time_hour,
|
||||
symbolResId = R.string.unit_time_hour_symbol,
|
||||
factor = HOUR
|
||||
factor = HOUR,
|
||||
key = "Hour"
|
||||
)
|
||||
|
||||
data object Minute : Unit(
|
||||
nameResId = R.string.unit_time_minute,
|
||||
symbolResId = R.string.unit_time_minute_symbol,
|
||||
factor = MINUTE
|
||||
factor = MINUTE,
|
||||
key = "Minute"
|
||||
)
|
||||
|
||||
data object Second : Unit(
|
||||
nameResId = R.string.unit_time_second,
|
||||
symbolResId = R.string.unit_time_second_symbol,
|
||||
factor = 1.0
|
||||
factor = 1.0,
|
||||
key = "Second"
|
||||
)
|
||||
|
||||
data object Millisecond : Unit(
|
||||
nameResId = R.string.unit_time_millisecond,
|
||||
symbolResId = R.string.unit_time_millisecond_symbol,
|
||||
factor = 0.001
|
||||
factor = 0.001,
|
||||
key = "Millisecond"
|
||||
)
|
||||
|
||||
data object Day : Unit(
|
||||
nameResId = R.string.unit_time_day,
|
||||
symbolResId = R.string.unit_time_day_symbol,
|
||||
factor = DAY
|
||||
factor = DAY,
|
||||
key = "Day"
|
||||
)
|
||||
|
||||
data object Week : Unit(
|
||||
nameResId = R.string.unit_time_week,
|
||||
symbolResId = R.string.unit_time_week_symbol,
|
||||
factor = DAY * 7
|
||||
factor = DAY * 7,
|
||||
key = "Week"
|
||||
)
|
||||
|
||||
data object Year : Unit(
|
||||
nameResId = R.string.unit_time_year,
|
||||
symbolResId = R.string.unit_time_year_symbol,
|
||||
factor = GREGORIAN_YEAR
|
||||
factor = GREGORIAN_YEAR,
|
||||
key = "Year"
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -5,144 +5,168 @@ import com.simplemobiletools.calculator.R
|
|||
object VolumeConverter : Converter {
|
||||
override val nameResId: Int = R.string.unit_volume
|
||||
override val imageResId: Int = R.drawable.ic_drop_vector
|
||||
override val key: String = "VolumeConverter"
|
||||
|
||||
sealed class Unit(nameResId: Int, symbolResId: Int, factor: Double) : Converter.Unit(nameResId, symbolResId, factor) {
|
||||
sealed class Unit(nameResId: Int, symbolResId: Int, factor: Double, key: String) : Converter.Unit(nameResId, symbolResId, factor, key) {
|
||||
data object CubicMeter : Unit(
|
||||
nameResId = R.string.unit_volume_cubic_meter,
|
||||
symbolResId = R.string.unit_volume_cubic_meter_symbol,
|
||||
factor = 1.0
|
||||
factor = 1.0,
|
||||
key = "CubicMeter"
|
||||
)
|
||||
|
||||
data object CubicDecimeter : Unit(
|
||||
nameResId = R.string.unit_volume_cubic_decimeter,
|
||||
symbolResId = R.string.unit_volume_cubic_decimeter_symbol,
|
||||
factor = 0.001
|
||||
factor = 0.001,
|
||||
key = "CubicDecimeter"
|
||||
)
|
||||
|
||||
data object CubicCentimeter : Unit(
|
||||
nameResId = R.string.unit_volume_cubic_centimeter,
|
||||
symbolResId = R.string.unit_volume_cubic_centimeter_symbol,
|
||||
factor = 0.000001
|
||||
factor = 0.000001,
|
||||
key = "CubicCentimeter"
|
||||
)
|
||||
|
||||
data object CubicMillimeter : Unit(
|
||||
nameResId = R.string.unit_volume_cubic_millimeter,
|
||||
symbolResId = R.string.unit_volume_cubic_millimeter_symbol,
|
||||
factor = 0.000000001
|
||||
factor = 0.000000001,
|
||||
key = "CubicMillimeter"
|
||||
)
|
||||
|
||||
data object Liter : Unit(
|
||||
nameResId = R.string.unit_volume_liter,
|
||||
symbolResId = R.string.unit_volume_liter_symbol,
|
||||
factor = 0.001
|
||||
factor = 0.001,
|
||||
key = "Liter"
|
||||
)
|
||||
|
||||
data object Centiliter : Unit(
|
||||
nameResId = R.string.unit_volume_centiliter,
|
||||
symbolResId = R.string.unit_volume_centiliter_symbol,
|
||||
factor = 0.0001
|
||||
factor = 0.0001,
|
||||
key = "Centiliter"
|
||||
)
|
||||
|
||||
data object Deciliter : Unit(
|
||||
nameResId = R.string.unit_volume_deciliter,
|
||||
symbolResId = R.string.unit_volume_deciliter_symbol,
|
||||
factor = 0.00001
|
||||
factor = 0.00001,
|
||||
key = "Deciliter"
|
||||
)
|
||||
|
||||
data object Milliliter : Unit(
|
||||
nameResId = R.string.unit_volume_milliliter,
|
||||
symbolResId = R.string.unit_volume_milliliter_symbol,
|
||||
factor = 0.000001
|
||||
factor = 0.000001,
|
||||
key = "Milliliter"
|
||||
)
|
||||
|
||||
data object AcreFoot : Unit(
|
||||
nameResId = R.string.unit_volume_acre_foot,
|
||||
symbolResId = R.string.unit_volume_acre_foot_symbol,
|
||||
factor = 1_233.48183754752
|
||||
factor = 1_233.48183754752,
|
||||
key = "AcreFoot"
|
||||
)
|
||||
|
||||
data object CubicFoot : Unit(
|
||||
nameResId = R.string.unit_volume_cubic_foot,
|
||||
symbolResId = R.string.unit_volume_cubic_foot_symbol,
|
||||
factor = 0.028316846592
|
||||
factor = 0.028316846592,
|
||||
key = "CubicFoot"
|
||||
)
|
||||
|
||||
data object CubicInch : Unit(
|
||||
nameResId = R.string.unit_volume_cubic_inch,
|
||||
symbolResId = R.string.unit_volume_cubic_inch_symbol,
|
||||
factor = 0.000016387064
|
||||
factor = 0.000016387064,
|
||||
key = "CubicInch"
|
||||
)
|
||||
|
||||
data object BarrelUS : Unit(
|
||||
nameResId = R.string.unit_volume_barrel_us,
|
||||
symbolResId = R.string.unit_volume_barrel_us_symbol,
|
||||
factor = 0.119240471196
|
||||
factor = 0.119240471196,
|
||||
key = "BarrelUS"
|
||||
)
|
||||
|
||||
data object GallonUS : Unit(
|
||||
nameResId = R.string.unit_volume_gallon_us,
|
||||
symbolResId = R.string.unit_volume_gallon_us_symbol,
|
||||
factor = 0.003785411784
|
||||
factor = 0.003785411784,
|
||||
key = "GallonUS"
|
||||
)
|
||||
|
||||
data object QuartUS : Unit(
|
||||
nameResId = R.string.unit_volume_quart_us,
|
||||
symbolResId = R.string.unit_volume_quart_us_symbol,
|
||||
factor = 0.000946352946
|
||||
factor = 0.000946352946,
|
||||
key = "QuartUS"
|
||||
)
|
||||
|
||||
data object PintUS : Unit(
|
||||
nameResId = R.string.unit_volume_pint_us,
|
||||
symbolResId = R.string.unit_volume_pint_us_symbol,
|
||||
factor = 0.000473176473
|
||||
factor = 0.000473176473,
|
||||
key = "PintUS"
|
||||
)
|
||||
|
||||
data object GillUS : Unit(
|
||||
nameResId = R.string.unit_volume_gill_us,
|
||||
symbolResId = R.string.unit_volume_gill_us_symbol,
|
||||
factor = 0.00011829411825
|
||||
factor = 0.00011829411825,
|
||||
key = "GillUS"
|
||||
)
|
||||
|
||||
data object FluidOunceUS : Unit(
|
||||
nameResId = R.string.unit_volume_fluid_ounce_us,
|
||||
symbolResId = R.string.unit_volume_fluid_ounce_us_symbol,
|
||||
factor = 0.00003
|
||||
factor = 0.00003,
|
||||
key = "FluidOunceUS"
|
||||
)
|
||||
|
||||
data object BarrelImperial : Unit(
|
||||
nameResId = R.string.unit_volume_barrel_imperial,
|
||||
symbolResId = R.string.unit_volume_barrel_imperial_symbol,
|
||||
factor = 0.16365924
|
||||
factor = 0.16365924,
|
||||
key = "BarrelImperial"
|
||||
)
|
||||
|
||||
data object GallonImperial : Unit(
|
||||
nameResId = R.string.unit_volume_gallon_imperial,
|
||||
symbolResId = R.string.unit_volume_gallon_imperial_symbol,
|
||||
factor = 0.00454609
|
||||
factor = 0.00454609,
|
||||
key = "GallonImperial"
|
||||
)
|
||||
|
||||
data object QuartImperial : Unit(
|
||||
nameResId = R.string.unit_volume_quart_imperial,
|
||||
symbolResId = R.string.unit_volume_quart_imperial_symbol,
|
||||
factor = 0.0011365225
|
||||
factor = 0.0011365225,
|
||||
key = "QuartImperial"
|
||||
)
|
||||
|
||||
data object PintImperial : Unit(
|
||||
nameResId = R.string.unit_volume_pint_imperial,
|
||||
symbolResId = R.string.unit_volume_pint_imperial_symbol,
|
||||
factor = 0.00056826125
|
||||
factor = 0.00056826125,
|
||||
key = "PintImperial"
|
||||
)
|
||||
|
||||
data object GillImperial : Unit(
|
||||
nameResId = R.string.unit_volume_gill_imperial,
|
||||
symbolResId = R.string.unit_volume_gill_imperial_symbol,
|
||||
factor = 0.0001420653125
|
||||
factor = 0.0001420653125,
|
||||
key = "GillImperial"
|
||||
)
|
||||
|
||||
data object FluidOunceImperial : Unit(
|
||||
nameResId = R.string.unit_volume_fluid_ounce_imperial,
|
||||
symbolResId = R.string.unit_volume_fluid_ounce_imperial_symbol,
|
||||
factor = 0.0000284130625
|
||||
factor = 0.0000284130625,
|
||||
key = "FluidOunceImperial"
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package com.simplemobiletools.calculator.models
|
||||
|
||||
import com.simplemobiletools.calculator.helpers.converters.Converter
|
||||
|
||||
data class ConverterUnitsState(
|
||||
val topUnit: Converter.Unit,
|
||||
val bottomUnit: Converter.Unit,
|
||||
)
|
|
@ -14,6 +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.extensions.config
|
||||
import com.simplemobiletools.calculator.helpers.*
|
||||
import com.simplemobiletools.calculator.helpers.converters.Converter
|
||||
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
|
||||
|
@ -243,6 +244,7 @@ class ConverterView @JvmOverloads constructor(
|
|||
updateBottomValue()
|
||||
}
|
||||
updateUnitLabelsAndSymbols()
|
||||
context.config.putLastConverterUnits(converter!!, topUnit!!, bottomUnit!!)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -254,9 +256,15 @@ class ConverterView @JvmOverloads constructor(
|
|||
}
|
||||
|
||||
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)
|
||||
val storedTopUnit = converter!!.units[state.getInt(TOP_UNIT)]
|
||||
val storedBottomUnit = converter!!.units[state.getInt(BOTTOM_UNIT)]
|
||||
updateUnits(storedTopUnit, storedBottomUnit)
|
||||
}
|
||||
|
||||
fun updateUnits(newTopUnit: Converter.Unit, newBottomUnit: Converter.Unit) {
|
||||
topUnit = newTopUnit
|
||||
bottomUnit = newBottomUnit
|
||||
|
||||
updateBottomValue()
|
||||
updateUnitLabelsAndSymbols()
|
||||
|
|
Loading…
Reference in a new issue