Skip to content

Commit 9189700

Browse files
committed
Fix price lazy issue
1 parent e6b15ca commit 9189700

File tree

1 file changed

+11
-11
lines changed
  • app/src/main/java/dev/shorthouse/coinwatch/model

1 file changed

+11
-11
lines changed

app/src/main/java/dev/shorthouse/coinwatch/model/Price.kt

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@ import java.util.Currency as CurrencyCode
1212
data class Price(val price: String?, val currency: Currency = Currency.USD) : Comparable<Price> {
1313
val amount: BigDecimal = price.toSanitisedBigDecimalOrZero()
1414

15-
private val currencyFormat by lazy {
16-
val currencyFormat = DecimalFormat.getCurrencyInstance(Locale.US) as DecimalFormat
15+
private val currencyFormat: DecimalFormat = getCurrencyFormat()
1716

17+
val formattedAmount: String = when {
18+
price.isNullOrBlank() -> "${currency.symbol}--"
19+
amount in belowOneThreshold -> currencyFormat.format(amount)
20+
amount in smallThreshold -> currencyFormat.format(amount)
21+
else -> formatLargeAmount()
22+
}
23+
24+
private fun getCurrencyFormat(): DecimalFormat {
25+
val currencyFormat = DecimalFormat.getCurrencyInstance(Locale.US) as DecimalFormat
1826
val decimalPlaces = if (amount in belowOneThreshold) 6 else 2
1927
val currencyCode = try {
2028
CurrencyCode.getInstance(currency.name)
2129
} catch (e: IllegalArgumentException) {
2230
CurrencyCode.getInstance(Currency.USD.name)
2331
}
24-
2532
currencyFormat.minimumFractionDigits = decimalPlaces
2633
currencyFormat.maximumFractionDigits = decimalPlaces
2734
currencyFormat.currency = currencyCode
2835

29-
currencyFormat
30-
}
31-
32-
val formattedAmount: String = when {
33-
price.isNullOrBlank() -> "${currency.symbol}--"
34-
amount in belowOneThreshold -> currencyFormat.format(amount)
35-
amount in smallThreshold -> currencyFormat.format(amount)
36-
else -> formatLargeAmount()
36+
return currencyFormat
3737
}
3838

3939
private fun formatLargeAmount(): String {

0 commit comments

Comments
 (0)