Add unit converter for units of temperature
This commit is contained in:
parent
4e8819e559
commit
1067397205
@ -35,7 +35,8 @@ open class UnitConverter(
|
||||
lazy { DataConverter(context) },
|
||||
lazy { TimeConverter(context) },
|
||||
lazy { VelocityConverter(context) },
|
||||
lazy { AreaConverter(context) }
|
||||
lazy { AreaConverter(context) },
|
||||
lazy { TemperatureConverter(context) }
|
||||
)
|
||||
for (conv in converters) {
|
||||
val converter = conv.value
|
||||
|
||||
@ -3,6 +3,7 @@ package de.mm20.launcher2.unitconverter.converters
|
||||
import android.content.Context
|
||||
import de.mm20.launcher2.search.data.UnitConverter
|
||||
import de.mm20.launcher2.unitconverter.ConverterUtils
|
||||
import de.mm20.launcher2.unitconverter.MeasureUnit
|
||||
import de.mm20.launcher2.unitconverter.UnitValue
|
||||
|
||||
/**
|
||||
@ -40,6 +41,6 @@ abstract class SimpleFactorConverter: Converter {
|
||||
|
||||
data class MeasureUnitWithFactor(
|
||||
val factor: Double,
|
||||
val symbol: String,
|
||||
val nameResource: Int
|
||||
)
|
||||
override val symbol: String,
|
||||
override val nameResource: Int
|
||||
): MeasureUnit
|
||||
@ -1,8 +1,116 @@
|
||||
package de.mm20.launcher2.unitconverter.converters
|
||||
|
||||
import android.content.Context
|
||||
import de.mm20.launcher2.unitconverter.Dimension
|
||||
import de.mm20.launcher2.search.data.UnitConverter
|
||||
import de.mm20.launcher2.unitconverter.*
|
||||
|
||||
class TemperatureConverter(context: Context): SimpleFactorConverter() {
|
||||
class TemperatureConverter(context: Context) : Converter {
|
||||
override val dimension = Dimension.Temperature
|
||||
|
||||
private val units = listOf(
|
||||
TemperatureMeasureUnit(
|
||||
context.getString(R.string.unit_degree_celsius_symbol),
|
||||
R.plurals.unit_degree_celsius,
|
||||
TemperatureUnit.DegreeCelsius
|
||||
),
|
||||
TemperatureMeasureUnit(
|
||||
context.getString(R.string.unit_degree_fahrenheit_symbol),
|
||||
R.plurals.unit_degree_fahrenheit,
|
||||
TemperatureUnit.DegreeFahrenheit
|
||||
),
|
||||
TemperatureMeasureUnit(
|
||||
context.getString(R.string.unit_kelvin_symbol),
|
||||
R.plurals.unit_kelvin,
|
||||
TemperatureUnit.Kelvin
|
||||
),
|
||||
)
|
||||
|
||||
override suspend fun isValidUnit(symbol: String): Boolean {
|
||||
return units.any { it.symbol == symbol }
|
||||
}
|
||||
|
||||
override suspend fun convert(
|
||||
context: Context,
|
||||
fromUnit: String,
|
||||
value: Double,
|
||||
toUnit: String?
|
||||
): UnitConverter {
|
||||
val from = units.first { it.symbol == fromUnit }
|
||||
val to = toUnit?.let { unit -> units.first { it.symbol == unit } }
|
||||
|
||||
val values = mutableListOf<UnitValue>()
|
||||
|
||||
if (to != null) {
|
||||
val toValue = convertTemperature(value, from.unit, to.unit)
|
||||
|
||||
values += UnitValue(
|
||||
value = value,
|
||||
symbol = toUnit,
|
||||
formattedName = ConverterUtils.formatName(context, to, toValue),
|
||||
formattedValue = ConverterUtils.formatValue(context, to, toValue),
|
||||
)
|
||||
} else {
|
||||
for (to in units) {
|
||||
if (to.symbol == from.symbol) continue
|
||||
val v = convertTemperature(value, from.unit, to.unit)
|
||||
values += UnitValue(
|
||||
v,
|
||||
to.symbol,
|
||||
ConverterUtils.formatName(context, to, v),
|
||||
ConverterUtils.formatValue(context, to, v)
|
||||
)
|
||||
}
|
||||
}
|
||||
return UnitConverter(
|
||||
dimension = Dimension.Temperature,
|
||||
inputValue = UnitValue(
|
||||
value = value,
|
||||
symbol = fromUnit,
|
||||
formattedName = ConverterUtils.formatName(context, from, value),
|
||||
formattedValue = ConverterUtils.formatValue(context, from, value),
|
||||
),
|
||||
values = values
|
||||
)
|
||||
}
|
||||
|
||||
private fun convertTemperature(
|
||||
value: Double,
|
||||
from: TemperatureUnit,
|
||||
to: TemperatureUnit
|
||||
): Double {
|
||||
if (from == to) return value
|
||||
if (from == TemperatureUnit.Kelvin && to == TemperatureUnit.DegreeCelsius) {
|
||||
return value - 273.15
|
||||
}
|
||||
if (from == TemperatureUnit.DegreeCelsius && to == TemperatureUnit.Kelvin) {
|
||||
return value + 273.15
|
||||
}
|
||||
if (from === TemperatureUnit.DegreeCelsius && to == TemperatureUnit.DegreeFahrenheit) {
|
||||
return value * (9.0 / 5.0) + 32.0
|
||||
}
|
||||
if (from === TemperatureUnit.DegreeFahrenheit && to == TemperatureUnit.DegreeCelsius) {
|
||||
return (value - 32.0) * (5.0 / 9.0)
|
||||
}
|
||||
|
||||
if (from === TemperatureUnit.Kelvin && to == TemperatureUnit.DegreeFahrenheit) {
|
||||
return (value - 273.15) * (9.0 / 5.0) + 32.0
|
||||
}
|
||||
if (from === TemperatureUnit.DegreeFahrenheit && to == TemperatureUnit.Kelvin) {
|
||||
return (value - 32.0) * (5.0 / 9.0) + 273.15
|
||||
}
|
||||
throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
||||
private data class TemperatureMeasureUnit(
|
||||
override val symbol: String,
|
||||
override val nameResource: Int,
|
||||
val unit: TemperatureUnit
|
||||
) :
|
||||
MeasureUnit
|
||||
|
||||
private enum class TemperatureUnit {
|
||||
DegreeCelsius,
|
||||
DegreeFahrenheit,
|
||||
Kelvin,
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user