Move unit converter supported units to a different screen
This commit is contained in:
@@ -8,9 +8,7 @@ import kotlin.math.roundToInt
|
||||
internal object ConverterUtils {
|
||||
|
||||
fun formatName(context: Context, unit: MeasureUnit, value: Double): String {
|
||||
val resId = unit.nameResource
|
||||
val text = context.resources.getQuantityString(resId, value.roundToInt())
|
||||
return text
|
||||
return unit.formatName(context, value)
|
||||
}
|
||||
|
||||
fun formatValue(context: Context, unit: MeasureUnit, value: Double): String {
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package de.mm20.launcher2.unitconverter
|
||||
|
||||
import android.content.Context
|
||||
|
||||
interface MeasureUnit {
|
||||
val symbol: String
|
||||
fun formatName(context: Context, value: Double): String
|
||||
}
|
||||
-6
@@ -1,6 +0,0 @@
|
||||
package de.mm20.launcher2.unitconverter
|
||||
|
||||
interface MeasureUnit {
|
||||
val symbol: String
|
||||
val nameResource: Int
|
||||
}
|
||||
+4
-3
@@ -19,6 +19,7 @@ import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -26,7 +27,7 @@ import org.koin.core.component.KoinComponent
|
||||
|
||||
interface UnitConverterRepository {
|
||||
fun search(query: String): Flow<UnitConverter?>
|
||||
fun availableConverters(includeCurrencies: Boolean) : List<Converter>
|
||||
suspend fun getAvailableConverters(includeCurrencies: Boolean) : List<Converter>
|
||||
}
|
||||
|
||||
internal class UnitConverterRepositoryImpl(
|
||||
@@ -55,7 +56,7 @@ internal class UnitConverterRepositoryImpl(
|
||||
}
|
||||
}
|
||||
|
||||
override fun availableConverters(includeCurrencies: Boolean) : List<Converter> {
|
||||
override suspend fun getAvailableConverters(includeCurrencies: Boolean) : List<Converter> {
|
||||
val converters = mutableListOf(
|
||||
MassConverter(context),
|
||||
LengthConverter(context),
|
||||
@@ -90,7 +91,7 @@ internal class UnitConverterRepositoryImpl(
|
||||
val value = valueStr.toDoubleOrNull() ?: valueStr.replace(',', '.').toDoubleOrNull()
|
||||
?: return null
|
||||
|
||||
val converters = availableConverters(includeCurrencies)
|
||||
val converters = getAvailableConverters(includeCurrencies)
|
||||
|
||||
for (converter in converters) {
|
||||
if (!converter.isValidUnit(unitStr)) continue
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import android.content.Context
|
||||
import de.mm20.launcher2.unitconverter.Dimension
|
||||
import de.mm20.launcher2.unitconverter.R
|
||||
|
||||
class AreaConverter(context: Context) : SimpleFactorConverter() {
|
||||
internal class AreaConverter(context: Context) : SimpleFactorConverter() {
|
||||
override val dimension = Dimension.Area
|
||||
|
||||
override val standardUnits = listOf(
|
||||
|
||||
+3
@@ -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.Dimension
|
||||
import de.mm20.launcher2.unitconverter.MeasureUnit
|
||||
|
||||
interface Converter {
|
||||
val dimension: Dimension
|
||||
@@ -15,5 +16,7 @@ interface Converter {
|
||||
value: Double,
|
||||
toUnit: String?
|
||||
): UnitConverter
|
||||
|
||||
suspend fun getSupportedUnits(): List<MeasureUnit>
|
||||
}
|
||||
|
||||
|
||||
+61
-5
@@ -7,6 +7,7 @@ import de.mm20.launcher2.currencies.CurrencyRepository
|
||||
import de.mm20.launcher2.search.data.CurrencyUnitConverter
|
||||
import de.mm20.launcher2.search.data.UnitConverter
|
||||
import de.mm20.launcher2.unitconverter.Dimension
|
||||
import de.mm20.launcher2.unitconverter.MeasureUnit
|
||||
import de.mm20.launcher2.unitconverter.UnitValue
|
||||
import java.text.DecimalFormat
|
||||
import java.util.Locale
|
||||
@@ -19,7 +20,7 @@ class CurrencyConverter(
|
||||
|
||||
override val dimension: Dimension = Dimension.Currency
|
||||
|
||||
suspend fun getAbbreviations() : List<String> {
|
||||
suspend fun getAbbreviations(): List<String> {
|
||||
return repository.getKnownUnits()
|
||||
}
|
||||
|
||||
@@ -35,7 +36,14 @@ class CurrencyConverter(
|
||||
val text = StringBuilder()
|
||||
val currency = Currency.getInstance(symbol) ?: return formatNameFallback(symbol)
|
||||
val pluralCount = PluralRules.forLocale(Locale.getDefault()).select(value)
|
||||
text.append(currency.getName(Locale.getDefault(), Currency.PLURAL_LONG_NAME, pluralCount, booleanArrayOf(false)))
|
||||
text.append(
|
||||
currency.getName(
|
||||
Locale.getDefault(),
|
||||
Currency.PLURAL_LONG_NAME,
|
||||
pluralCount,
|
||||
booleanArrayOf(false)
|
||||
)
|
||||
)
|
||||
|
||||
return text.toString()
|
||||
}
|
||||
@@ -62,12 +70,22 @@ class CurrencyConverter(
|
||||
}
|
||||
|
||||
|
||||
override suspend fun convert(context: Context, fromUnit: String, value: Double, toUnit: String?): UnitConverter {
|
||||
override suspend fun convert(
|
||||
context: Context,
|
||||
fromUnit: String,
|
||||
value: Double,
|
||||
toUnit: String?
|
||||
): UnitConverter {
|
||||
val fromIsoCode = repository.resolveAlias(fromUnit)
|
||||
val toIsoCode = toUnit?.let { repository.resolveAlias(it) }
|
||||
|
||||
val values = repository.convertCurrency(fromIsoCode, value, toIsoCode).map {
|
||||
UnitValue(it.second, it.first, formatName(it.first, it.second), formatValue(it.first, it.second))
|
||||
UnitValue(
|
||||
it.second,
|
||||
it.first,
|
||||
formatName(it.first, it.second),
|
||||
formatValue(it.first, it.second)
|
||||
)
|
||||
}.toMutableList()
|
||||
|
||||
val ownCurrencySymbol = JCurrency.getInstance(Locale.getDefault()).currencyCode ?: "USD"
|
||||
@@ -89,8 +107,46 @@ class CurrencyConverter(
|
||||
values.add(0, ownCurrency)
|
||||
}
|
||||
|
||||
val inputValue = UnitValue(value, fromIsoCode, formatName(fromIsoCode, value), formatValue(fromIsoCode, value))
|
||||
val inputValue = UnitValue(
|
||||
value,
|
||||
fromIsoCode,
|
||||
formatName(fromIsoCode, value),
|
||||
formatValue(fromIsoCode, value)
|
||||
)
|
||||
val lastUpdate = repository.getLastUpdate(fromIsoCode)
|
||||
return CurrencyUnitConverter(dimension, inputValue, values, lastUpdate)
|
||||
}
|
||||
|
||||
override suspend fun getSupportedUnits(): List<MeasureUnit> {
|
||||
val currencies = repository.getKnownUnits()
|
||||
return currencies.map {
|
||||
CurrencyUnit(
|
||||
symbol = it,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal data class CurrencyUnit(
|
||||
override val symbol: String,
|
||||
) : MeasureUnit {
|
||||
override fun formatName(context: Context, value: Double): String {
|
||||
val text = StringBuilder()
|
||||
val currency = try {
|
||||
Currency.getInstance(symbol)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
null
|
||||
} ?: return symbol
|
||||
val pluralCount = PluralRules.forLocale(Locale.getDefault()).select(value)
|
||||
text.append(
|
||||
currency.getName(
|
||||
Locale.getDefault(),
|
||||
Currency.LONG_NAME,
|
||||
pluralCount,
|
||||
booleanArrayOf(false)
|
||||
)
|
||||
)
|
||||
|
||||
return text.toString()
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -4,7 +4,7 @@ import android.content.Context
|
||||
import de.mm20.launcher2.unitconverter.Dimension
|
||||
import de.mm20.launcher2.unitconverter.R
|
||||
|
||||
class DataConverter(context: Context) : SimpleFactorConverter() {
|
||||
internal class DataConverter(context: Context) : SimpleFactorConverter() {
|
||||
override val dimension = Dimension.Data
|
||||
|
||||
override val standardUnits = listOf(
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import android.content.Context
|
||||
import de.mm20.launcher2.unitconverter.Dimension
|
||||
import de.mm20.launcher2.unitconverter.R
|
||||
|
||||
class LengthConverter(context: Context) : SimpleFactorConverter() {
|
||||
internal class LengthConverter(context: Context) : SimpleFactorConverter() {
|
||||
override val dimension = Dimension.Length
|
||||
|
||||
override val standardUnits = listOf(
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import android.content.Context
|
||||
import de.mm20.launcher2.unitconverter.Dimension
|
||||
import de.mm20.launcher2.unitconverter.R
|
||||
|
||||
class MassConverter(context: Context): SimpleFactorConverter() {
|
||||
internal class MassConverter(context: Context): SimpleFactorConverter() {
|
||||
override val dimension = Dimension.Mass
|
||||
|
||||
override val standardUnits = listOf(
|
||||
|
||||
+12
-3
@@ -5,11 +5,12 @@ 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
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/**
|
||||
* A converter for units that can converted into each other by simple multiplication with a constant factor
|
||||
*/
|
||||
abstract class SimpleFactorConverter: Converter {
|
||||
internal abstract class SimpleFactorConverter: Converter {
|
||||
open val standardUnits: List<MeasureUnitWithFactor> = emptyList()
|
||||
|
||||
/**
|
||||
@@ -36,11 +37,19 @@ abstract class SimpleFactorConverter: Converter {
|
||||
val inputValue = UnitValue(value, fromUnit, ConverterUtils.formatName(context, unit, value), ConverterUtils.formatValue(context, unit, value))
|
||||
return UnitConverter(dimension, inputValue, results)
|
||||
}
|
||||
|
||||
override suspend fun getSupportedUnits(): List<MeasureUnit> {
|
||||
return standardUnits
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
data class MeasureUnitWithFactor(
|
||||
val factor: Double,
|
||||
override val symbol: String,
|
||||
override val nameResource: Int
|
||||
): MeasureUnit
|
||||
val nameResource: Int
|
||||
): MeasureUnit {
|
||||
override fun formatName(context: Context, value: Double): String {
|
||||
return context.resources.getQuantityString(nameResource, value.roundToInt())
|
||||
}
|
||||
}
|
||||
+11
-4
@@ -4,7 +4,7 @@ import android.content.Context
|
||||
import de.mm20.launcher2.search.data.UnitConverter
|
||||
import de.mm20.launcher2.unitconverter.*
|
||||
|
||||
class TemperatureConverter(context: Context) : Converter {
|
||||
internal class TemperatureConverter(context: Context) : Converter {
|
||||
override val dimension = Dimension.Temperature
|
||||
|
||||
val units = listOf(
|
||||
@@ -100,14 +100,21 @@ class TemperatureConverter(context: Context) : Converter {
|
||||
}
|
||||
throw IllegalArgumentException()
|
||||
}
|
||||
|
||||
override suspend fun getSupportedUnits(): List<MeasureUnit> {
|
||||
return units
|
||||
}
|
||||
}
|
||||
|
||||
data class TemperatureMeasureUnit(
|
||||
override val symbol: String,
|
||||
override val nameResource: Int,
|
||||
val nameResource: Int,
|
||||
val unit: TemperatureUnit
|
||||
) :
|
||||
MeasureUnit
|
||||
) : MeasureUnit {
|
||||
override fun formatName(context: Context, value: Double): String {
|
||||
return context.resources.getQuantityString(nameResource, value.toInt())
|
||||
}
|
||||
}
|
||||
|
||||
enum class TemperatureUnit {
|
||||
DegreeCelsius,
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import android.content.Context
|
||||
import de.mm20.launcher2.unitconverter.Dimension
|
||||
import de.mm20.launcher2.unitconverter.R
|
||||
|
||||
class TimeConverter(context: Context) : SimpleFactorConverter() {
|
||||
internal class TimeConverter(context: Context) : SimpleFactorConverter() {
|
||||
override val dimension = Dimension.Time
|
||||
|
||||
override val standardUnits = listOf(
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import android.content.Context
|
||||
import de.mm20.launcher2.unitconverter.Dimension
|
||||
import de.mm20.launcher2.unitconverter.R
|
||||
|
||||
class VelocityConverter(context: Context) : SimpleFactorConverter() {
|
||||
internal class VelocityConverter(context: Context) : SimpleFactorConverter() {
|
||||
override val dimension = Dimension.Velocity
|
||||
|
||||
override val standardUnits = listOf(
|
||||
|
||||
Reference in New Issue
Block a user