Initial commit
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
<manifest package="de.mm20.launcher2.currencies">
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,19 @@
|
||||
package de.mm20.launcher2.currencies
|
||||
|
||||
import de.mm20.launcher2.database.entities.CurrencyEntity
|
||||
|
||||
data class Currency(
|
||||
val symbol: String,
|
||||
val value: Double,
|
||||
val lastUpdate: Long
|
||||
) {
|
||||
constructor(entity: CurrencyEntity) : this(
|
||||
symbol = entity.symbol,
|
||||
value = entity.value,
|
||||
lastUpdate = entity.lastUpdate
|
||||
)
|
||||
|
||||
fun toDatabaseEntity(): CurrencyEntity {
|
||||
return CurrencyEntity(symbol, value, lastUpdate)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package de.mm20.launcher2.currencies
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.work.*
|
||||
import de.mm20.launcher2.database.AppDatabase
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class CurrencyRepository(val context: Context) {
|
||||
|
||||
init {
|
||||
val currencyWorker = PeriodicWorkRequest.Builder(ExchangeRateWorker::class.java, 60, TimeUnit.MINUTES)
|
||||
.build()
|
||||
WorkManager.getInstance().enqueueUniquePeriodicWork("ExchangeRates",
|
||||
ExistingPeriodicWorkPolicy.REPLACE, currencyWorker)
|
||||
}
|
||||
|
||||
suspend fun convertCurrency(fromCurrency: String, value: Double, toCurrency: String? = null): List<Pair<String, Double>> {
|
||||
|
||||
return withContext<List<Pair<String, Double>>>(Dispatchers.IO) {
|
||||
val dao = AppDatabase.getInstance(context)
|
||||
.currencyDao()
|
||||
|
||||
val from = Currency(dao.getCurrency(fromCurrency) ?: return@withContext emptyList())
|
||||
|
||||
return@withContext if (toCurrency == null) {
|
||||
dao.getAllCurrencies().mapNotNull {
|
||||
val to = Currency(it)
|
||||
if (from.lastUpdate != to.lastUpdate) {
|
||||
Log.w("MM20", "Exchange rate update dates do not match: $fromCurrency, $it")
|
||||
return@mapNotNull null
|
||||
}
|
||||
if (from.symbol == to.symbol) return@mapNotNull null
|
||||
to.symbol to value * to.value / from.value
|
||||
}
|
||||
} else {
|
||||
val to = Currency(dao.getCurrency(toCurrency) ?: return@withContext emptyList())
|
||||
if (from.lastUpdate != to.lastUpdate) {
|
||||
Log.w("MM20", "Exchange rate update dates do not match: $fromCurrency, $toCurrency")
|
||||
return@withContext emptyList()
|
||||
}
|
||||
listOf(toCurrency to value * to.value / from.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getFlag(currencySymbol: String): String {
|
||||
return when (currencySymbol) {
|
||||
"EUR" -> "\uD83C\uDDEA\uD83C\uDDFA" // European Union
|
||||
"USD" -> "\uD83C\uDDFA\uD83C\uDDF8" // United States
|
||||
"JPY" -> "\uD83C\uDDEF\uD83C\uDDF5" // Japan
|
||||
"GBP" -> "\uD83C\uDDEC\uD83C\uDDE7" // United Kingdom
|
||||
"AUD" -> "\uD83C\uDDE6\uD83C\uDDFA" // Australia
|
||||
"CAD" -> "\uD83C\uDDE8\uD83C\uDDE6" // Canada
|
||||
"CHF" -> "\uD83C\uDDE8\uD83C\uDDED" // Switzerland
|
||||
"CNY" -> "\uD83C\uDDE8\uD83C\uDDF3" // China
|
||||
"SEK" -> "\uD83C\uDDF8\uD83C\uDDEA" // Sweden
|
||||
"NZD" -> "\uD83C\uDDF3\uD83C\uDDFF" // New Zealand
|
||||
|
||||
"HKD" -> "\uD83C\uDDED\uD83C\uDDF0" // Hong Kong
|
||||
"IDR" -> "\uD83C\uDDEE\uD83C\uDDE9" // Indonesia
|
||||
"ILS" -> "\uD83C\uDDEE\uD83C\uDDF1" // Israel
|
||||
"DKK" -> "\uD83C\uDDE9\uD83C\uDDF0" // Denmark
|
||||
"INR" -> "\uD83C\uDDEE\uD83C\uDDF3" // India
|
||||
"MXN" -> "\uD83C\uDDF2\uD83C\uDDFD" // Mexico
|
||||
"CZK" -> "\uD83C\uDDE8\uD83C\uDDFF" // Czechia
|
||||
"SGD" -> "\uD83C\uDDF8\uD83C\uDDEC" // Singapore
|
||||
"THB" -> "\uD83C\uDDF9\uD83C\uDDED" // Thailand
|
||||
"HRK" -> "\uD83C\uDDED\uD83C\uDDF7" // Croatia
|
||||
"MYR" -> "\uD83C\uDDF2\uD83C\uDDFE" // Malaysia
|
||||
"NOK" -> "\uD83C\uDDF3\uD83C\uDDF4" // Norway
|
||||
"BGN" -> "\uD83C\uDDE7\uD83C\uDDEC" // Bulgaria
|
||||
"PHP" -> "\uD83C\uDDF5\uD83C\uDDED" // Philippines
|
||||
"PLN" -> "\uD83C\uDDF5\uD83C\uDDF1" // Poland
|
||||
"ZAR" -> "\uD83C\uDDFF\uD83C\uDDE6" // South Africa
|
||||
"ISK" -> "\uD83C\uDDEE\uD83C\uDDF8" // Iceland
|
||||
"BRL" -> "\uD83C\uDDE7\uD83C\uDDF7" // Brazil
|
||||
"RON" -> "\uD83C\uDDF7\uD83C\uDDF4" // Romania
|
||||
"TRY" -> "\uD83C\uDDF9\uD83C\uDDF7" // Turkey
|
||||
"RUB" -> "\uD83C\uDDF7\uD83C\uDDFA" // Russia
|
||||
"KRW" -> "\uD83C\uDDF0\uD83C\uDDF7" // South Korea
|
||||
"HUF" -> "\uD83C\uDDED\uD83C\uDDFA" // Hungary
|
||||
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun isValidCurrency(symbol: String): Boolean {
|
||||
return withContext(Dispatchers.IO) {
|
||||
AppDatabase.getInstance(context).currencyDao().exists(symbol)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getLastUpdate(symbol: String): Long {
|
||||
return withContext(Dispatchers.IO) {
|
||||
AppDatabase.getInstance(context).currencyDao().getLastUpdate(symbol)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private lateinit var instance: CurrencyRepository
|
||||
fun getInstance(context: Context): CurrencyRepository {
|
||||
if (!::instance.isInitialized) instance = CurrencyRepository(context.applicationContext)
|
||||
return instance
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package de.mm20.launcher2.currencies
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.work.Worker
|
||||
import androidx.work.WorkerParameters
|
||||
import de.mm20.launcher2.crashreporter.CrashReporter
|
||||
import de.mm20.launcher2.database.AppDatabase
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import org.w3c.dom.Element
|
||||
import java.text.SimpleDateFormat
|
||||
import javax.xml.parsers.DocumentBuilderFactory
|
||||
|
||||
class ExchangeRateWorker(val context: Context, params: WorkerParameters) : Worker(context, params) {
|
||||
override fun doWork(): Result {
|
||||
Log.d("MM20", "Updating currency exchange rates")
|
||||
val httpClient = OkHttpClient()
|
||||
val request = Request.Builder()
|
||||
.url("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")
|
||||
.get()
|
||||
.build()
|
||||
try {
|
||||
val response = httpClient.newCall(request).execute()
|
||||
val document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(response.body?.byteStream()
|
||||
?: return Result.retry())
|
||||
val cubes = document.getElementsByTagName("Cube")
|
||||
val values = mutableListOf<Pair<String, Double>>()
|
||||
var timestamp = System.currentTimeMillis()
|
||||
values += "EUR" to 1.0
|
||||
for (i in 0 until cubes.length) {
|
||||
val cube = cubes.item(i) as? Element ?: continue
|
||||
if (cube.hasAttribute("currency")) {
|
||||
val symbol = cube.getAttribute("currency")
|
||||
val value = cube.getAttribute("rate").toDoubleOrNull() ?: continue
|
||||
values += symbol to value
|
||||
} else if (cube.hasAttribute("time")) {
|
||||
val date = cube.getAttribute("time")
|
||||
timestamp = SimpleDateFormat("yyyy-MM-dd").parse(date).time
|
||||
}
|
||||
}
|
||||
val currencies = values.map {
|
||||
Currency(
|
||||
symbol = it.first,
|
||||
value = it.second,
|
||||
lastUpdate = timestamp
|
||||
).toDatabaseEntity()
|
||||
}
|
||||
AppDatabase.getInstance(context).currencyDao().insertAll(currencies)
|
||||
return Result.success()
|
||||
} catch (e: Exception) {
|
||||
CrashReporter.logException(e)
|
||||
return Result.retry()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user