Skip to content

Commit 35e9b33

Browse files
committed
refactor: Avoid unneeded currency conversion in money components
1 parent 3676fdd commit 35e9b33

File tree

1 file changed

+43
-39
lines changed
  • app/src/main/java/to/bitkit/ui/components

1 file changed

+43
-39
lines changed
Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package to.bitkit.ui.components
22

33
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.remember
45
import androidx.compose.ui.Modifier
56
import androidx.compose.ui.graphics.Color
67
import androidx.compose.ui.platform.LocalInspectionMode
@@ -17,74 +18,77 @@ fun MoneyDisplay(
1718
sats: Long,
1819
onClick: (() -> Unit)? = null,
1920
) {
20-
val isPreview = LocalInspectionMode.current
21-
if (isPreview) {
22-
val displayText = "<accent>₿</accent> ${sats.formatToModernDisplay()}"
23-
Display(text = displayText.withAccent(accentColor = Colors.White64))
24-
return
25-
}
26-
27-
val currency = currencyViewModel ?: return
28-
val currencies = LocalCurrencies.current
29-
30-
currency.convert(sats)?.let { converted ->
31-
val displayText = if (currencies.primaryDisplay == PrimaryDisplay.BITCOIN) {
32-
val btcComponents = converted.bitcoinDisplay(currencies.displayUnit)
33-
"<accent>${btcComponents.symbol}</accent> ${btcComponents.value}"
34-
} else {
35-
"<accent>${converted.symbol}</accent> ${converted.formatted}"
36-
}
37-
21+
rememberMoneyText(sats)?.let { text ->
3822
Display(
39-
text = displayText.withAccent(accentColor = Colors.White64),
23+
text = text.withAccent(accentColor = Colors.White64),
4024
modifier = Modifier.clickableAlpha(onClick = onClick)
4125
)
4226
}
4327
}
4428

4529
@Composable
4630
fun MoneySSB(sats: Long) {
31+
rememberMoneyText(sats)?.let { text ->
32+
BodySSB(text = text.withAccent(accentColor = Colors.White64))
33+
}
34+
}
35+
36+
@Composable
37+
fun MoneyCaptionB(
38+
sats: Long,
39+
color: Color,
40+
) {
4741
val isPreview = LocalInspectionMode.current
4842
if (isPreview) {
49-
val displayText = "<accent>₿</accent> ${sats.formatToModernDisplay()}"
50-
BodySSB(text = displayText.withAccent(accentColor = Colors.White64))
43+
CaptionB(text = sats.formatToModernDisplay(), color = color)
5144
return
5245
}
5346

5447
val currency = currencyViewModel ?: return
5548
val currencies = LocalCurrencies.current
5649

57-
currency.convert(sats)?.let { converted ->
58-
val displayText = if (currencies.primaryDisplay == PrimaryDisplay.BITCOIN) {
50+
val displayText = remember(currencies, sats) {
51+
currency.convert(sats)?.let { converted ->
5952
val btcComponents = converted.bitcoinDisplay(currencies.displayUnit)
60-
"<accent>${btcComponents.symbol}</accent> ${btcComponents.value}"
61-
} else {
62-
"<accent>${converted.symbol}</accent> ${converted.formatted}"
53+
btcComponents.value
6354
}
55+
}
6456

65-
BodySSB(text = displayText.withAccent(accentColor = Colors.White64))
57+
displayText?.let { text ->
58+
CaptionB(
59+
text = text,
60+
color = color,
61+
)
6662
}
6763
}
6864

65+
/**
66+
* Generates a formatted representation of a monetary value based on the provided amount in satoshis
67+
* and the current currency display settings. Can be either in bitcoin or fiat.
68+
*
69+
* @param sats The amount in satoshis to be formatted and displayed.
70+
* @return A formatted string representation of the monetary value, or null if it cannot be generated.
71+
*/
6972
@Composable
70-
fun MoneyCaptionB(
73+
fun rememberMoneyText(
7174
sats: Long,
72-
color: Color,
73-
) {
75+
): String? {
7476
val isPreview = LocalInspectionMode.current
7577
if (isPreview) {
76-
CaptionB(text = sats.formatToModernDisplay(), color = color)
77-
return
78+
return "<accent>₿</accent> ${sats.formatToModernDisplay()}"
7879
}
7980

80-
val currency = currencyViewModel ?: return
81+
val currency = currencyViewModel ?: return null
8182
val currencies = LocalCurrencies.current
8283

83-
currency.convert(sats)?.let { converted ->
84-
val btcComponents = converted.bitcoinDisplay(currencies.displayUnit)
85-
CaptionB(
86-
text = btcComponents.value,
87-
color = color,
88-
)
84+
return remember(currencies, sats) {
85+
val converted = currency.convert(sats) ?: return@remember null
86+
87+
if (currencies.primaryDisplay == PrimaryDisplay.BITCOIN) {
88+
val btcComponents = converted.bitcoinDisplay(currencies.displayUnit)
89+
"<accent>${btcComponents.symbol}</accent> ${btcComponents.value}"
90+
} else {
91+
"<accent>${converted.symbol}</accent> ${converted.formatted}"
92+
}
8993
}
9094
}

0 commit comments

Comments
 (0)