Add currency converter symbol aliases (i.e. € for EUR)
This commit is contained in:
parent
8054f6e5c4
commit
32276934bb
@ -2,15 +2,97 @@ package de.mm20.launcher2.currencies
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.work.*
|
import androidx.work.ExistingPeriodicWorkPolicy
|
||||||
|
import androidx.work.PeriodicWorkRequest
|
||||||
|
import androidx.work.WorkManager
|
||||||
import de.mm20.launcher2.database.AppDatabase
|
import de.mm20.launcher2.database.AppDatabase
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import java.util.Locale
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
class CurrencyRepository(
|
class CurrencyRepository(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
private val currencySymbolAliases = buildMap {
|
||||||
|
val ownCurrency = java.util.Currency.getInstance(Locale.getDefault()).symbol ?: "USD"
|
||||||
|
|
||||||
|
put("€", "EUR")
|
||||||
|
|
||||||
|
val dollarSymbolCurrencies = listOf(
|
||||||
|
// dollar
|
||||||
|
"AUD",
|
||||||
|
"BBD",
|
||||||
|
"BMD",
|
||||||
|
"BND",
|
||||||
|
"BSD",
|
||||||
|
"BZD",
|
||||||
|
"CAD",
|
||||||
|
"FJD",
|
||||||
|
"GYD",
|
||||||
|
"HKD",
|
||||||
|
"JMD",
|
||||||
|
"KID",
|
||||||
|
"KYD",
|
||||||
|
"LRD",
|
||||||
|
"NAD",
|
||||||
|
"NZD",
|
||||||
|
"SBD",
|
||||||
|
"SGD",
|
||||||
|
"SRD",
|
||||||
|
"TTD",
|
||||||
|
"TVD",
|
||||||
|
"TWD",
|
||||||
|
"USD",
|
||||||
|
"XCD",
|
||||||
|
// peso
|
||||||
|
"ARS",
|
||||||
|
"CLP",
|
||||||
|
"COP",
|
||||||
|
"DOP",
|
||||||
|
"MXN",
|
||||||
|
"UYU",
|
||||||
|
)
|
||||||
|
|
||||||
|
if (ownCurrency in dollarSymbolCurrencies) {
|
||||||
|
put("$", ownCurrency)
|
||||||
|
} else {
|
||||||
|
put("$", "USD")
|
||||||
|
}
|
||||||
|
|
||||||
|
val poundSymbolCurrencies = listOf("EGP", "FKP", "GBP", "GIP", "SHP", "SDG", "SYP")
|
||||||
|
if (ownCurrency in poundSymbolCurrencies) {
|
||||||
|
put("£", ownCurrency)
|
||||||
|
} else {
|
||||||
|
put("£", "GBP")
|
||||||
|
}
|
||||||
|
|
||||||
|
put("¥", if (ownCurrency == "CNY") "CNY" else "JPY")
|
||||||
|
put("₩", if (ownCurrency == "KPW") "KPW" else "KRW")
|
||||||
|
|
||||||
|
put("kr", if (ownCurrency == "NOK") "NOK" else "SEK")
|
||||||
|
put("kr.", "DKK")
|
||||||
|
put("Kr", "ISK")
|
||||||
|
|
||||||
|
put("zł", "PLN")
|
||||||
|
put("Kč", "CZK")
|
||||||
|
put("₴", "UAH")
|
||||||
|
put("₽", "RUB")
|
||||||
|
put("Ft", "HUF")
|
||||||
|
|
||||||
|
put("₪", "ILS")
|
||||||
|
put("TL", "TRY")
|
||||||
|
|
||||||
|
put("R$", "BRL")
|
||||||
|
|
||||||
|
put("₱", if (ownCurrency == "CUP") "CUP" else "PHP")
|
||||||
|
put("฿", "THB")
|
||||||
|
put("₹", "INR")
|
||||||
|
|
||||||
|
return@buildMap
|
||||||
|
}
|
||||||
|
|
||||||
fun enableCurrencyUpdateWorker() {
|
fun enableCurrencyUpdateWorker() {
|
||||||
val currencyWorker =
|
val currencyWorker =
|
||||||
PeriodicWorkRequest.Builder(ExchangeRateWorker::class.java, 60, TimeUnit.MINUTES)
|
PeriodicWorkRequest.Builder(ExchangeRateWorker::class.java, 60, TimeUnit.MINUTES)
|
||||||
@ -25,6 +107,16 @@ class CurrencyRepository(
|
|||||||
WorkManager.getInstance(context).cancelUniqueWork("ExchangeRates")
|
WorkManager.getInstance(context).cancelUniqueWork("ExchangeRates")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves currency symbol aliases to their full currency symbol.
|
||||||
|
* (e.g. € -> EUR)
|
||||||
|
* @param symbol The currency symbol to resolve
|
||||||
|
* @return The resolved currency symbol or the original symbol if no alias was found
|
||||||
|
*/
|
||||||
|
fun resolveAlias(symbol: String): String {
|
||||||
|
return currencySymbolAliases[symbol] ?: symbol
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun convertCurrency(
|
suspend fun convertCurrency(
|
||||||
fromCurrency: String,
|
fromCurrency: String,
|
||||||
value: Double,
|
value: Double,
|
||||||
@ -62,8 +154,9 @@ class CurrencyRepository(
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun isValidCurrency(symbol: String): Boolean {
|
suspend fun isValidCurrency(symbol: String): Boolean {
|
||||||
|
val isoSymbol = currencySymbolAliases[symbol] ?: symbol
|
||||||
return withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.IO) {
|
||||||
AppDatabase.getInstance(context).currencyDao().exists(symbol)
|
AppDatabase.getInstance(context).currencyDao().exists(isoSymbol)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -59,7 +59,10 @@ 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 values = repository.convertCurrency(fromUnit, value, toUnit).map {
|
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()
|
}.toMutableList()
|
||||||
|
|
||||||
@ -82,8 +85,8 @@ class CurrencyConverter(
|
|||||||
values.add(0, ownCurrency)
|
values.add(0, ownCurrency)
|
||||||
}
|
}
|
||||||
|
|
||||||
val inputValue = UnitValue(value, fromUnit, formatName(fromUnit, value), formatValue(fromUnit, value))
|
val inputValue = UnitValue(value, fromIsoCode, formatName(fromIsoCode, value), formatValue(fromIsoCode, value))
|
||||||
val lastUpdate = repository.getLastUpdate(fromUnit)
|
val lastUpdate = repository.getLastUpdate(fromIsoCode)
|
||||||
return CurrencyUnitConverter(dimension, inputValue, values, lastUpdate)
|
return CurrencyUnitConverter(dimension, inputValue, values, lastUpdate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user