Skip to content

Commit 3fe822c

Browse files
committed
refactor: currency model
1 parent 35f8504 commit 3fe822c

File tree

5 files changed

+35
-48
lines changed

5 files changed

+35
-48
lines changed

app/src/main/java/to/bitkit/models/Currency.kt

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import java.util.Locale
1111
const val BITCOIN_SYMBOL = ""
1212
const val SATS_IN_BTC = 100_000_000
1313
const val BTC_SCALE = 8
14-
const val BTC_PLACEHOLDER = "0.00000000"
15-
const val SATS_PLACEHOLDER = "0"
14+
const val GROUPING_SEPARATOR = ' '
1615

1716
@Serializable
1817
data class FxRateResponse(
@@ -42,24 +41,22 @@ data class FxRate(
4241
enum class PrimaryDisplay {
4342
BITCOIN, FIAT;
4443

45-
operator fun not(): PrimaryDisplay {
46-
return when (this) {
47-
BITCOIN -> FIAT
48-
FIAT -> BITCOIN
49-
}
44+
operator fun not() = when (this) {
45+
BITCOIN -> FIAT
46+
FIAT -> BITCOIN
5047
}
5148
}
5249

5350
/** aka. Denomination */
5451
enum class BitcoinDisplayUnit {
5552
MODERN, CLASSIC;
5653

57-
operator fun not(): BitcoinDisplayUnit {
58-
return when (this) {
59-
MODERN -> CLASSIC
60-
CLASSIC -> MODERN
61-
}
54+
operator fun not() = when (this) {
55+
MODERN -> CLASSIC
56+
CLASSIC -> MODERN
6257
}
58+
59+
fun isModern() = this == MODERN
6360
}
6461

6562
data class ConvertedAmount(
@@ -69,28 +66,17 @@ data class ConvertedAmount(
6966
val currency: String,
7067
val flag: String,
7168
val sats: Long,
69+
val locale: Locale = Locale.getDefault(),
7270
) {
73-
val btcValue: BigDecimal = sats.asBtc()
74-
7571
data class BitcoinDisplayComponents(
7672
val symbol: String,
7773
val value: String,
7874
)
7975

8076
fun bitcoinDisplay(unit: BitcoinDisplayUnit): BitcoinDisplayComponents {
81-
val spaceSeparator = ' '
8277
val formattedValue = when (unit) {
83-
BitcoinDisplayUnit.MODERN -> {
84-
sats.formatToModernDisplay()
85-
}
86-
87-
BitcoinDisplayUnit.CLASSIC -> {
88-
val formatSymbols = DecimalFormatSymbols(Locale.getDefault()).apply {
89-
groupingSeparator = spaceSeparator
90-
}
91-
val formatter = DecimalFormat("#,###.########", formatSymbols)
92-
formatter.format(btcValue)
93-
}
78+
BitcoinDisplayUnit.MODERN -> sats.formatToModernDisplay(locale)
79+
BitcoinDisplayUnit.CLASSIC -> sats.formatToClassicDisplay(locale)
9480
}
9581
return BitcoinDisplayComponents(
9682
symbol = BITCOIN_SYMBOL,
@@ -99,18 +85,25 @@ data class ConvertedAmount(
9985
}
10086
}
10187

102-
fun Long.formatToModernDisplay(): String {
88+
fun Long.formatToModernDisplay(locale: Locale = Locale.getDefault()): String {
10389
val sats = this
104-
val formatSymbols = DecimalFormatSymbols(Locale.getDefault()).apply {
105-
groupingSeparator = ' '
90+
val formatSymbols = DecimalFormatSymbols(locale).apply {
91+
groupingSeparator = GROUPING_SEPARATOR
10692
}
10793
val formatter = DecimalFormat("#,###", formatSymbols).apply {
10894
isGroupingUsed = true
10995
}
11096
return formatter.format(sats)
11197
}
11298

113-
fun ULong.formatToModernDisplay(): String = this.toLong().formatToModernDisplay()
99+
fun ULong.formatToModernDisplay(locale: Locale = Locale.getDefault()): String = toLong().formatToModernDisplay(locale)
100+
101+
fun Long.formatToClassicDisplay(locale: Locale = Locale.getDefault()): String {
102+
val sats = this
103+
val formatSymbols = DecimalFormatSymbols(locale)
104+
val formatter = DecimalFormat("###.########", formatSymbols)
105+
return formatter.format(sats.asBtc())
106+
}
114107

115108
/** Represent this sat value in Bitcoin BigDecimal. */
116109
fun Long.asBtc(): BigDecimal = BigDecimal(this).divide(BigDecimal(SATS_IN_BTC), BTC_SCALE, RoundingMode.HALF_UP)

app/src/main/java/to/bitkit/repositories/CurrencyRepo.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import to.bitkit.data.CacheStore
2323
import to.bitkit.data.SettingsStore
2424
import to.bitkit.di.BgDispatcher
2525
import to.bitkit.env.Env
26+
import to.bitkit.models.BITCOIN_SYMBOL
2627
import to.bitkit.models.BTC_SCALE
2728
import to.bitkit.models.BitcoinDisplayUnit
2829
import to.bitkit.models.ConvertedAmount
@@ -252,5 +253,7 @@ data class CurrencyState(
252253
val currencySymbol: String = "$",
253254
val displayUnit: BitcoinDisplayUnit = BitcoinDisplayUnit.MODERN,
254255
val primaryDisplay: PrimaryDisplay = PrimaryDisplay.BITCOIN,
255-
val lastSuccessfulRefresh: Long? = null
256-
)
256+
val lastSuccessfulRefresh: Long? = null,
257+
) {
258+
fun primarySymbol() = if (primaryDisplay == PrimaryDisplay.BITCOIN) BITCOIN_SYMBOL else currencySymbol
259+
}

app/src/main/java/to/bitkit/ui/components/Money.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ fun rememberMoneyText(
115115
): String? {
116116
val isPreview = LocalInspectionMode.current
117117
if (isPreview) {
118-
val symbol = if (unit == PrimaryDisplay.BITCOIN) BITCOIN_SYMBOL else "$"
119118
return buildString {
120-
if (showSymbol) append("<accent>$symbol</accent> ")
119+
if (showSymbol) append("<accent>${currencies.primarySymbol()}</accent> ")
121120
append(sats.formatToModernDisplay())
122121
}
123122
}

app/src/main/java/to/bitkit/ui/settings/general/DefaultUnitSettingsScreen.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,15 @@ fun DefaultUnitSettingsScreenContent(
9393
BitcoinDisplayUnit.entries.forEach { unit ->
9494
SettingsButtonRow(
9595
title = stringResource(
96-
if (unit == BitcoinDisplayUnit.MODERN) {
96+
if (unit.isModern()) {
9797
R.string.settings__general__denomination_modern
9898
} else {
9999
R.string.settings__general__denomination_classic
100100
}
101101
),
102102
value = SettingsButtonValue.BooleanValue(displayUnit == unit),
103103
onClick = { onBitcoinUnitClick(unit) },
104-
modifier = Modifier.testTag(
105-
if (unit == BitcoinDisplayUnit.MODERN) "DenominationModern" else "DenominationClassic"
106-
)
104+
modifier = Modifier.testTag(if (unit.isModern()) "DenominationModern" else "DenominationClassic")
107105
)
108106
}
109107

app/src/main/java/to/bitkit/ui/utils/visualTransformation/BitcoinVisualTransformation.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import androidx.compose.ui.text.input.OffsetMapping
55
import androidx.compose.ui.text.input.TransformedText
66
import androidx.compose.ui.text.input.VisualTransformation
77
import to.bitkit.models.BitcoinDisplayUnit
8+
import to.bitkit.models.GROUPING_SEPARATOR
9+
import to.bitkit.models.formatToModernDisplay
810
import java.text.DecimalFormat
911
import java.text.DecimalFormatSymbols
1012
import java.util.Locale
@@ -34,16 +36,8 @@ class BitcoinVisualTransformation(
3436
}
3537

3638
private fun formatModernDisplay(text: String): String {
37-
val cleanText = text.replace(" ", "")
38-
val longValue = cleanText.toLongOrNull() ?: return text
39-
40-
val formatSymbols = DecimalFormatSymbols(Locale.getDefault()).apply {
41-
groupingSeparator = ' '
42-
}
43-
val formatter = DecimalFormat("#,###", formatSymbols).apply {
44-
isGroupingUsed = true
45-
}
46-
return formatter.format(longValue)
39+
val longValue = text.replace("$GROUPING_SEPARATOR", "").toLongOrNull() ?: return text
40+
return longValue.formatToModernDisplay()
4741
}
4842

4943
private fun formatClassicDisplay(text: String): String {

0 commit comments

Comments
 (0)