Repository navigation
feat: add offline currency conversion for period totals #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
shy-tangerine
wants to merge
2
commits into
isaacsa51:master
from
shy-tangerine:feat/currency-display
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...oidTest/java/com/serranoie/app/minus/presentation/ui/currency/CurrencyConversionUiTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.serranoie.app.minus.presentation.ui.currency | ||
|
|
||
| import androidx.activity.ComponentActivity | ||
| import androidx.compose.ui.test.assertIsDisplayed | ||
| import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
| import androidx.compose.ui.test.onNodeWithText | ||
| import com.serranoie.app.minus.domain.currency.ConversionPreferences | ||
| import com.serranoie.app.minus.domain.currency.ExchangeRate | ||
| import com.serranoie.app.minus.presentation.ui.theme.MinusTheme | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import java.math.BigDecimal | ||
|
|
||
| class CurrencyConversionUiTest { | ||
| @get:Rule val rule = createAndroidComposeRule<ComponentActivity>() | ||
| private val prefs = ConversionPreferences("BRL", referenceRates = listOf(ExchangeRate("USD", "BRL", BigDecimal("5"), "2026-10-08", false))) | ||
| @Test fun showsPairedTotals() { | ||
| rule.setContent { MinusTheme { CurrencyPeriodSummaryContent("USD", BigDecimal("100"), BigDecimal("12.50"), prefs, false) } } | ||
| rule.onNodeWithText("Budget: USD 100.00 ≈ BRL 500.00").assertIsDisplayed() | ||
| rule.onNodeWithText("Spent: USD 12.50 ≈ BRL 62.50").assertIsDisplayed() | ||
| } | ||
| @Test fun missingRateIsExplicit() { | ||
| rule.setContent { MinusTheme { CurrencyPeriodSummaryContent("EUR", BigDecimal("100"), BigDecimal("12.50"), prefs, false) } } | ||
| rule.onNodeWithText("No saved EUR/BRL rate. Set a rate in Settings.").assertIsDisplayed() | ||
| } | ||
| @Test fun privacyHidesConversion() { | ||
| rule.setContent { MinusTheme { CurrencyPeriodSummaryContent("USD", BigDecimal("100"), BigDecimal("12.50"), prefs, true) } } | ||
| rule.onNodeWithText("Budget: USD 100.00 ≈ BRL 500.00").assertDoesNotExist() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
app/src/main/java/com/serranoie/app/minus/data/currency/CurrencyConversionRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package com.serranoie.app.minus.data.currency | ||
|
|
||
| import androidx.datastore.core.DataStore | ||
| import androidx.datastore.preferences.core.Preferences | ||
| import androidx.datastore.preferences.core.edit | ||
| import androidx.datastore.preferences.core.stringPreferencesKey | ||
| import com.serranoie.app.minus.domain.currency.ConversionPreferences | ||
| import com.serranoie.app.minus.domain.currency.ExchangeRate | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.flow.map | ||
| import kotlinx.coroutines.withContext | ||
| import kotlinx.serialization.Serializable | ||
| import kotlinx.serialization.encodeToString | ||
| import kotlinx.serialization.json.Json | ||
| import java.math.BigDecimal | ||
| import java.net.URL | ||
| import java.time.LocalDate | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
| import javax.net.ssl.HttpsURLConnection | ||
|
|
||
| @Serializable | ||
| internal data class RateRecord(val base: String, val quote: String, val rate: String, val date: String, val manual: Boolean) { | ||
| fun domain() = ExchangeRate(base, quote, rate.toBigDecimal(), date, manual) | ||
| } | ||
| @Serializable | ||
| internal data class ConversionRecord(val displayCurrency: String? = null, val manual: List<RateRecord> = emptyList(), val reference: List<RateRecord> = emptyList()) { | ||
| fun domain() = ConversionPreferences(displayCurrency, manual.map { it.domain() }, reference.map { it.domain() }) | ||
| } | ||
| @Serializable | ||
| internal data class FrankfurterRate(val date: String, val base: String, val quote: String, val rate: kotlinx.serialization.json.JsonPrimitive) | ||
|
|
||
| // Parse decimal literals without a lossy Double intermediate. | ||
| internal fun parseReferenceRate(body: String, base: String, quote: String, today: LocalDate = LocalDate.now()): ExchangeRate { | ||
| val rows = Json { ignoreUnknownKeys = true }.decodeFromString<List<FrankfurterRate>>(body) | ||
| val row = rows.single { it.base == base && it.quote == quote } | ||
| require(!row.rate.isString) | ||
| return ExchangeRate(row.base, row.quote, row.rate.content.toBigDecimal(), row.date, false).also { | ||
| require(!LocalDate.parse(it.date).isAfter(today)) | ||
| } | ||
| } | ||
|
|
||
| @Singleton | ||
| class CurrencyConversionRepository @Inject constructor(private val dataStore: DataStore<Preferences>) { | ||
| private val key = stringPreferencesKey("currency_conversion_v1") | ||
| private val json = Json { ignoreUnknownKeys = true } | ||
| private fun read(raw: String?): ConversionRecord = runCatching { | ||
| json.decodeFromString<ConversionRecord>(raw ?: "{}").also { it.domain() } | ||
| }.getOrDefault(ConversionRecord()) | ||
| val preferences = dataStore.data.map { read(it[key]).domain() } | ||
|
|
||
| suspend fun setDisplayCurrency(code: String?) { | ||
| require(code == null || code.matches(Regex("[A-Z]{3}"))) | ||
| dataStore.edit { it[key] = json.encodeToString(read(it[key]).copy(displayCurrency = code)) } | ||
| } | ||
| suspend fun setManualRate(base: String, quote: String, rate: BigDecimal) { | ||
| require(rate.precision() <= 30 && rate.scale() in -18..24) | ||
| val checked = ExchangeRate(base, quote, rate, LocalDate.now().toString(), true) | ||
| val record = RateRecord(checked.base, checked.quote, checked.rate.toPlainString(), checked.date, true) | ||
| dataStore.edit { | ||
| val old = read(it[key]) | ||
| // Keep only one manual override for the pair, including its inverse. | ||
| val remaining = old.manual.filterNot { r -> setOf(r.base, r.quote) == setOf(base, quote) } | ||
| it[key] = json.encodeToString(old.copy(manual = remaining + record)) | ||
| } | ||
| } | ||
| suspend fun removeManualRate(base: String, quote: String) { | ||
| dataStore.edit { | ||
| val old = read(it[key]) | ||
| it[key] = json.encodeToString(old.copy(manual = old.manual.filterNot { r -> r.base == base && r.quote == quote })) | ||
| } | ||
| } | ||
| suspend fun fetchReferenceRate(base: String, quote: String) { | ||
| require(base.matches(Regex("[A-Z]{3}")) && quote.matches(Regex("[A-Z]{3}")) && base != quote) | ||
| val rate = withContext(Dispatchers.IO) { | ||
| val connection = URL("https://api.frankfurter.dev/v2/rates?base=$base"es=$quote").openConnection() as HttpsURLConnection | ||
| connection.instanceFollowRedirects = false | ||
| connection.connectTimeout = 15000 | ||
| connection.readTimeout = 15000 | ||
| connection.setRequestProperty("Accept", "application/json") | ||
| try { | ||
| check(connection.responseCode == 200) { "Reference rate request failed (${connection.responseCode})" } | ||
| val body = connection.inputStream.bufferedReader().use { reader -> | ||
| val chars = CharArray(65537) | ||
| var count = 0 | ||
| while (count < chars.size) { | ||
| val n = reader.read(chars, count, chars.size - count) | ||
| if (n < 0) break | ||
| count += n | ||
| } | ||
| check(count <= 65536) { "Reference rate response too large" } | ||
| String(chars, 0, count) | ||
| } | ||
| parseReferenceRate(body, base, quote) | ||
| } finally { connection.disconnect() } | ||
| } | ||
| val record = RateRecord(rate.base, rate.quote, rate.rate.toPlainString(), rate.date, false) | ||
| dataStore.edit { | ||
| val old = read(it[key]) | ||
| it[key] = json.encodeToString(old.copy(reference = old.reference.filterNot { r -> setOf(r.base, r.quote) == setOf(base, quote) } + record)) | ||
| } | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
app/src/main/java/com/serranoie/app/minus/domain/currency/CurrencyConversion.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.serranoie.app.minus.domain.currency | ||
|
|
||
| import java.math.BigDecimal | ||
| import java.math.MathContext | ||
| import java.time.LocalDate | ||
|
|
||
| /** Rates are display preferences, never transaction updates. */ | ||
| data class ExchangeRate( | ||
| val base: String, | ||
| val quote: String, | ||
| val rate: BigDecimal, | ||
| val date: String, | ||
| val manual: Boolean, | ||
| ) { | ||
| init { | ||
| require(base.matches(Regex("[A-Z]{3}")) && quote.matches(Regex("[A-Z]{3}"))) | ||
| require(base != quote && rate.signum() > 0 && rate.precision() <= 40) | ||
| require(rate.scale() in -40..100) | ||
| LocalDate.parse(date) | ||
| } | ||
| val key: String get() = "$base/$quote" | ||
| } | ||
|
|
||
| data class ConversionPreferences( | ||
| val displayCurrency: String? = null, | ||
| val manualRates: List<ExchangeRate> = emptyList(), | ||
| val referenceRates: List<ExchangeRate> = emptyList(), | ||
| ) { | ||
| fun rateFor(base: String, quote: String): ExchangeRate? { | ||
| fun find(rates: List<ExchangeRate>): ExchangeRate? { | ||
| rates.firstOrNull { it.base == base && it.quote == quote }?.let { return it } | ||
| return rates.firstOrNull { it.base == quote && it.quote == base }?.let { | ||
| it.copy(base = base, quote = quote, rate = BigDecimal.ONE.divide(it.rate, MathContext.DECIMAL128)) | ||
| } | ||
| } | ||
| // A manual rate in either direction always wins over a reference rate. | ||
| return find(manualRates) ?: find(referenceRates) | ||
| } | ||
| fun convert(amount: BigDecimal, base: String): BigDecimal? { | ||
| val quote = displayCurrency ?: return null | ||
| if (base == quote) return amount | ||
| return rateFor(base, quote)?.let { amount.multiply(it.rate, MathContext.DECIMAL128) } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.