Show additional info for unit and currency converter (#720)
* Now for conversion patterns such as "TO > FROM" and "TO - FROM" are accepted Signed-off-by: Guillermo Villafuerte <gvillafu@comunidad.unam.mx> * Added help URL for unit converter Signed-off-by: Guillermo Villafuerte <gvillafu@comunidad.unam.mx> * Localized supported units are now shown on a screen that can be invoked from unit converter settings Signed-off-by: Guillermo Villafuerte <gvillafu@comunidad.unam.mx> * Made localizable titles for dimensions and preferences Signed-off-by: Guillermo Villafuerte <gvillafu@comunidad.unam.mx> * Show currency units on list if enabled. Signed-off-by: Guillermo Villafuerte <gvillafu@comunidad.unam.mx> * Supported units are now listed inline instead of a separate screen. Signed-off-by: Guillermo Villafuerte <gvillafu@comunidad.unam.mx> --------- Signed-off-by: Guillermo Villafuerte <gvillafu@comunidad.unam.mx>
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
package de.mm20.launcher2.unitconverter
|
||||
|
||||
enum class Dimension {
|
||||
Length,
|
||||
Mass,
|
||||
Velocity,
|
||||
Volume,
|
||||
Area,
|
||||
Currency,
|
||||
Data,
|
||||
Bitrate,
|
||||
Pressure,
|
||||
Energy,
|
||||
Frequency,
|
||||
Temperature,
|
||||
Time
|
||||
import androidx.annotation.StringRes
|
||||
|
||||
enum class Dimension(@StringRes val resource: Int) {
|
||||
Length(R.string.dimension_length),
|
||||
Mass(R.string.dimension_mass),
|
||||
Velocity(R.string.dimension_velocity),
|
||||
Volume(R.string.dimension_volume),
|
||||
Area(R.string.dimension_area),
|
||||
Currency(R.string.dimension_currency),
|
||||
Data(R.string.dimension_data),
|
||||
Bitrate(R.string.dimension_bitrate),
|
||||
Pressure(R.string.dimension_pressure),
|
||||
Energy(R.string.dimension_energy),
|
||||
Frequency(R.string.dimension_frequency),
|
||||
Temperature(R.string.dimension_temperature),
|
||||
Time(R.string.dimension_time),
|
||||
}
|
||||
+22
-12
@@ -5,6 +5,7 @@ import de.mm20.launcher2.currencies.CurrencyRepository
|
||||
import de.mm20.launcher2.preferences.search.UnitConverterSettings
|
||||
import de.mm20.launcher2.search.data.UnitConverter
|
||||
import de.mm20.launcher2.unitconverter.converters.AreaConverter
|
||||
import de.mm20.launcher2.unitconverter.converters.Converter
|
||||
import de.mm20.launcher2.unitconverter.converters.CurrencyConverter
|
||||
import de.mm20.launcher2.unitconverter.converters.DataConverter
|
||||
import de.mm20.launcher2.unitconverter.converters.LengthConverter
|
||||
@@ -25,6 +26,7 @@ import org.koin.core.component.KoinComponent
|
||||
|
||||
interface UnitConverterRepository {
|
||||
fun search(query: String): Flow<UnitConverter?>
|
||||
fun availableConverters(includeCurrencies: Boolean) : List<Converter>
|
||||
}
|
||||
|
||||
internal class UnitConverterRepositoryImpl(
|
||||
@@ -53,11 +55,29 @@ internal class UnitConverterRepositoryImpl(
|
||||
}
|
||||
}
|
||||
|
||||
override fun availableConverters(includeCurrencies: Boolean) : List<Converter> {
|
||||
val converters = mutableListOf(
|
||||
MassConverter(context),
|
||||
LengthConverter(context),
|
||||
DataConverter(context),
|
||||
TimeConverter(context),
|
||||
VelocityConverter(context),
|
||||
AreaConverter(context),
|
||||
TemperatureConverter(context)
|
||||
)
|
||||
if (includeCurrencies) converters.add(CurrencyConverter(currencyRepository))
|
||||
|
||||
return converters
|
||||
}
|
||||
|
||||
private suspend fun queryUnitConverter(
|
||||
query: String,
|
||||
includeCurrencies: Boolean
|
||||
): UnitConverter? {
|
||||
if (!query.matches(Regex("[0-9,.:]+ [^\\s]+")) && !query.matches(Regex("[0-9,.:]+ [^\\s]+ >> [^\\s]+"))) return null
|
||||
if (!query.matches(Regex("[0-9,.:]+ [^\\s]+")) &&
|
||||
!query.matches(Regex("[0-9,.:]+ [^\\s]+ >> [^\\s]+")) &&
|
||||
!query.matches(Regex("[0-9,.:]+ [^\\s]+ > [^\\s]+")) &&
|
||||
!query.matches(Regex("[0-9,.:]+ [^\\s]+ - [^\\s]+"))) return null
|
||||
val valueStr: String
|
||||
val unitStr: String
|
||||
val targetUnitStr: String?
|
||||
@@ -70,17 +90,7 @@ internal class UnitConverterRepositoryImpl(
|
||||
val value = valueStr.toDoubleOrNull() ?: valueStr.replace(',', '.').toDoubleOrNull()
|
||||
?: return null
|
||||
|
||||
val converters = mutableListOf(
|
||||
MassConverter(context),
|
||||
LengthConverter(context),
|
||||
DataConverter(context),
|
||||
TimeConverter(context),
|
||||
VelocityConverter(context),
|
||||
AreaConverter(context),
|
||||
TemperatureConverter(context)
|
||||
)
|
||||
|
||||
if (includeCurrencies) converters.add(CurrencyConverter(currencyRepository))
|
||||
val converters = availableConverters(includeCurrencies)
|
||||
|
||||
for (converter in converters) {
|
||||
if (!converter.isValidUnit(unitStr)) continue
|
||||
|
||||
+5
-1
@@ -10,8 +10,8 @@ import de.mm20.launcher2.unitconverter.Dimension
|
||||
import de.mm20.launcher2.unitconverter.UnitValue
|
||||
import java.text.DecimalFormat
|
||||
import java.util.Locale
|
||||
import java.util.Currency as JCurrency
|
||||
import kotlin.math.abs
|
||||
import java.util.Currency as JCurrency
|
||||
|
||||
class CurrencyConverter(
|
||||
private val repository: CurrencyRepository,
|
||||
@@ -19,6 +19,10 @@ class CurrencyConverter(
|
||||
|
||||
override val dimension: Dimension = Dimension.Currency
|
||||
|
||||
suspend fun getAbbreviations() : List<String> {
|
||||
return repository.getKnownUnits()
|
||||
}
|
||||
|
||||
|
||||
private val topCurrencies = arrayOf("USD", "EUR", "JPY", "GBP", "AUD")
|
||||
|
||||
|
||||
+3
-3
@@ -7,7 +7,7 @@ import de.mm20.launcher2.unitconverter.*
|
||||
class TemperatureConverter(context: Context) : Converter {
|
||||
override val dimension = Dimension.Temperature
|
||||
|
||||
private val units = listOf(
|
||||
val units = listOf(
|
||||
TemperatureMeasureUnit(
|
||||
context.getString(R.string.unit_degree_celsius_symbol),
|
||||
R.plurals.unit_degree_celsius,
|
||||
@@ -102,14 +102,14 @@ class TemperatureConverter(context: Context) : Converter {
|
||||
}
|
||||
}
|
||||
|
||||
private data class TemperatureMeasureUnit(
|
||||
data class TemperatureMeasureUnit(
|
||||
override val symbol: String,
|
||||
override val nameResource: Int,
|
||||
val unit: TemperatureUnit
|
||||
) :
|
||||
MeasureUnit
|
||||
|
||||
private enum class TemperatureUnit {
|
||||
enum class TemperatureUnit {
|
||||
DegreeCelsius,
|
||||
DegreeFahrenheit,
|
||||
Kelvin,
|
||||
|
||||
Reference in New Issue
Block a user