Reorganize and group modules

This commit is contained in:
MM20
2022-12-13 17:37:26 +01:00
parent bac24baad2
commit 3f8880a90a
995 changed files with 501 additions and 298 deletions
+1
View File
@@ -0,0 +1 @@
/build
+50
View File
@@ -0,0 +1,50 @@
plugins {
id("com.android.library")
id("kotlin-android")
}
android {
compileSdk = sdk.versions.compileSdk.get().toInt()
defaultConfig {
minSdk = sdk.versions.minSdk.get().toInt()
targetSdk = sdk.versions.targetSdk.get().toInt()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
buildTypes {
release {
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
namespace = "de.mm20.launcher2.currencies"
}
dependencies {
implementation(libs.bundles.kotlin)
implementation(libs.androidx.core)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.work)
implementation(libs.okhttp)
implementation(project(":core:ktx"))
implementation(project(":core:i18n"))
implementation(project(":core:database"))
implementation(project(":core:crashreporter"))
}
View File
+21
View File
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.kts.kts.kts.kts.kts.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
@@ -0,0 +1,3 @@
<manifest>
</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,75 @@
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.*
import java.util.concurrent.TimeUnit
class CurrencyRepository(
private val context: Context,
) {
fun enableCurrencyUpdateWorker() {
val currencyWorker =
PeriodicWorkRequest.Builder(ExchangeRateWorker::class.java, 60, TimeUnit.MINUTES)
.build()
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
"ExchangeRates",
ExistingPeriodicWorkPolicy.KEEP, currencyWorker
)
}
fun disableCurrencyUpdateWorker() {
WorkManager.getInstance(context).cancelUniqueWork("ExchangeRates")
}
suspend fun convertCurrency(
fromCurrency: String,
value: Double,
toCurrency: String? = null
): List<Pair<String, Double>> {
return withContext(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)
}
}
}
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)
}
}
}
@@ -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()
}
}
}