Unit converter: list preferred currencies first

Close #1837
This commit is contained in:
MM20
2026-06-29 19:13:39 +02:00
parent 8b50c8349d
commit 58772e7195
2 changed files with 8 additions and 14 deletions

View File

@@ -20,6 +20,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
@@ -67,7 +68,7 @@ internal class UnitConverterRepositoryImpl(
TemperatureConverter(context),
VolumeConverter(context),
)
if (includeCurrencies) converters.add(CurrencyConverter(currencyRepository))
if (includeCurrencies) converters.add(CurrencyConverter(currencyRepository, settings.preferredCurrencies.first()))
return converters
}

View File

@@ -16,6 +16,7 @@ import java.util.Currency as JCurrency
class CurrencyConverter(
private val repository: CurrencyRepository,
private val preferredCurrencies: List<String>,
) : Converter {
override val dimension: Dimension = Dimension.Currency
@@ -88,23 +89,15 @@ class CurrencyConverter(
)
}.toMutableList()
val ownCurrencySymbol = JCurrency.getInstance(Locale.getDefault()).currencyCode ?: "USD"
val index = values.indexOfFirst { it.symbol == ownCurrencySymbol }
val ownCurrency = if (index != -1) {
values.removeAt(index)
} else {
null
val ownCurrencySymbols = preferredCurrencies.ifEmpty {
listOf(JCurrency.getInstance(Locale.getDefault()).currencyCode ?: "USD")
}
val topCurrencies = ownCurrencySymbols + topCurrencies
values.sortBy {
val i = topCurrencies.indexOf(it.symbol)
if (i != -1) i.toString()
else it.formattedName
}
if (ownCurrency != null) {
values.add(0, ownCurrency)
if (i != -1) return@sortBy ("000$i").takeLast(4)
it.formattedName
}
val inputValue = UnitValue(