11package to.bitkit.ui.components
22
33import androidx.compose.runtime.Composable
4+ import androidx.compose.runtime.remember
45import androidx.compose.ui.Modifier
56import androidx.compose.ui.graphics.Color
67import 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
4630fun 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