Skip to content

Commit 0b6e438

Browse files
committed
refactor: cleanup number formatting
1 parent b8c06fe commit 0b6e438

File tree

5 files changed

+51
-101
lines changed

5 files changed

+51
-101
lines changed
Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package to.bitkit.ext
22

3-
import java.text.DecimalFormat
4-
import java.text.DecimalFormatSymbols
53
import java.time.Instant
64

75
fun ULong.toActivityItemDate(): String {
@@ -11,20 +9,3 @@ fun ULong.toActivityItemDate(): String {
119
fun ULong.toActivityItemTime(): String {
1210
return Instant.ofEpochSecond(this.toLong()).formatted(DatePattern.ACTIVITY_TIME)
1311
}
14-
15-
fun Number.formatWithDotSeparator(): String {
16-
val symbols = DecimalFormatSymbols().apply {
17-
groupingSeparator = '.'
18-
decimalSeparator = ','
19-
}
20-
val decimalFormat = DecimalFormat("#,###", symbols)
21-
return decimalFormat.format(this)
22-
}
23-
24-
fun ULong.formatWithDotSeparator(): String {
25-
return this.toLong().formatWithDotSeparator()
26-
}
27-
28-
fun UInt.formatWithDotSeparator(): String {
29-
return this.toInt().formatWithDotSeparator()
30-
}

app/src/main/java/to/bitkit/ext/String.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fun String.truncate(length: Int): String {
2222

2323
fun String.removeSpaces() = this.filterNot { it.isWhitespace() }
2424

25+
fun String.toLongOrDefault(defaultValue: Long = 0): Long = toLongOrNull() ?: defaultValue
2526

2627
/**
2728
* Pluralizes this string using the ICU MessageFormat with the provided arguments map.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@ import androidx.compose.ui.semantics.contentDescription
1919
import androidx.compose.ui.semantics.semantics
2020
import androidx.compose.ui.tooling.preview.Preview
2121
import androidx.compose.ui.unit.dp
22-
import okhttp3.internal.toLongOrDefault
2322
import to.bitkit.ext.removeSpaces
23+
import to.bitkit.ext.toLongOrDefault
2424
import to.bitkit.models.BITCOIN_SYMBOL
2525
import to.bitkit.models.BitcoinDisplayUnit
2626
import to.bitkit.models.PrimaryDisplay
2727
import to.bitkit.models.SATS_IN_BTC
28-
import to.bitkit.models.formatToModernDisplay
2928
import to.bitkit.models.asBtc
29+
import to.bitkit.models.formatToModernDisplay
3030
import to.bitkit.ui.currencyViewModel
3131
import to.bitkit.ui.theme.AppThemeSurface
3232
import to.bitkit.ui.theme.Colors
3333
import to.bitkit.viewmodels.CurrencyViewModel
3434
import java.math.BigDecimal
3535

36-
3736
@Composable
3837
fun NumberPadTextField(
3938
input: String,
@@ -102,7 +101,7 @@ fun NumberPadTextField(
102101
}
103102

104103
displayUnit == BitcoinDisplayUnit.MODERN && primaryDisplay == PrimaryDisplay.BITCOIN -> {
105-
input.toLongOrDefault(0L).formatToModernDisplay()
104+
input.toLongOrDefault().formatToModernDisplay()
106105
}
107106

108107
else -> {
@@ -159,6 +158,7 @@ fun AmountInputHandler(
159158
sats.asBtc().toString()
160159
}
161160
}
161+
162162
PrimaryDisplay.FIAT -> {
163163
currencyVM.convert(sats)?.formatted ?: "0"
164164
}
@@ -171,13 +171,13 @@ fun AmountInputHandler(
171171
if (primaryDisplay == lastDisplay) return@LaunchedEffect
172172
lastDisplay = primaryDisplay
173173
val newInput = when (primaryDisplay) {
174-
PrimaryDisplay.BITCOIN -> { //Convert fiat to sats
174+
PrimaryDisplay.BITCOIN -> { // Convert fiat to sats
175175
val amountLong = currencyVM.convertFiatToSats(input.replace(",", "").toDoubleOrNull() ?: 0.0)
176176
if (amountLong > 0.0) amountLong.toString() else ""
177177
}
178178

179-
PrimaryDisplay.FIAT -> { //Convert sats to fiat
180-
val convertedAmount = currencyVM.convert(input.toLongOrDefault(0L))
179+
PrimaryDisplay.FIAT -> { // Convert sats to fiat
180+
val convertedAmount = currencyVM.convert(input.toLongOrDefault())
181181
if ((convertedAmount?.value
182182
?: BigDecimal(0)) > BigDecimal(0)
183183
) convertedAmount?.formatted.toString() else ""
@@ -189,7 +189,7 @@ fun AmountInputHandler(
189189
LaunchedEffect(input) {
190190
val sats = when (primaryDisplay) {
191191
PrimaryDisplay.BITCOIN -> {
192-
if (displayUnit == BitcoinDisplayUnit.MODERN) input else (input.toLongOrDefault(0L) * SATS_IN_BTC).toString()
192+
if (displayUnit == BitcoinDisplayUnit.MODERN) input else (input.toLongOrDefault() * SATS_IN_BTC).toString()
193193
}
194194

195195
PrimaryDisplay.FIAT -> {

app/src/main/java/to/bitkit/ui/settings/ChannelOrdersScreen.kt

Lines changed: 38 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import androidx.compose.runtime.mutableStateOf
2727
import androidx.compose.runtime.remember
2828
import androidx.compose.runtime.rememberCoroutineScope
2929
import androidx.compose.runtime.setValue
30-
import androidx.lifecycle.compose.collectAsStateWithLifecycle
3130
import androidx.compose.ui.Alignment
3231
import androidx.compose.ui.Modifier
3332
import androidx.compose.ui.draw.scale
@@ -40,6 +39,7 @@ import androidx.compose.ui.text.style.TextOverflow
4039
import androidx.compose.ui.tooling.preview.Preview
4140
import androidx.compose.ui.unit.dp
4241
import androidx.compose.ui.unit.sp
42+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
4343
import com.synonym.bitkitcore.BtBolt11InvoiceState
4444
import com.synonym.bitkitcore.BtOrderState
4545
import com.synonym.bitkitcore.BtOrderState2
@@ -56,12 +56,13 @@ import com.synonym.bitkitcore.ILspNode
5656
import com.synonym.bitkitcore.IcJitEntry
5757
import kotlinx.coroutines.delay
5858
import kotlinx.coroutines.launch
59-
import to.bitkit.ext.formatWithDotSeparator
59+
import to.bitkit.models.formatToModernDisplay
6060
import to.bitkit.ui.Routes
6161
import to.bitkit.ui.blocktankViewModel
6262
import to.bitkit.ui.components.BodyS
6363
import to.bitkit.ui.components.PrimaryButton
6464
import to.bitkit.ui.scaffold.AppTopBar
65+
import to.bitkit.ui.shared.util.clickableAlpha
6566
import to.bitkit.ui.theme.AppThemeSurface
6667
import to.bitkit.ui.theme.Colors
6768
import to.bitkit.utils.Logger
@@ -215,20 +216,17 @@ private fun OrderDetailView(
215216
.padding(16.dp)
216217
) {
217218
Column(modifier = Modifier.padding(16.dp)) {
218-
Text(
219-
text = "Order Details",
220-
style = MaterialTheme.typography.titleMedium
221-
)
219+
Text(text = "Order Details", style = MaterialTheme.typography.titleMedium)
222220
Spacer(modifier = Modifier.height(8.dp))
223221
DetailRow("ID", order.id)
224222
DetailRow("Onchain txs", order.payment.onchain.transactions.size.toString())
225223
DetailRow("State", order.state.toString())
226224
DetailRow("State 2", order.state2.toString())
227-
DetailRow("LSP Balance", "${order.lspBalanceSat} sats")
228-
DetailRow("Client Balance", "${order.clientBalanceSat} sats")
229-
DetailRow("Total Fee", "${order.feeSat} sats")
230-
DetailRow("Network Fee", "${order.networkFeeSat} sats")
231-
DetailRow("Service Fee", "${order.serviceFeeSat} sats")
225+
DetailRow("LSP Balance", order.lspBalanceSat.formatToModernDisplay())
226+
DetailRow("Client Balance", order.clientBalanceSat.formatToModernDisplay())
227+
DetailRow("Total Fee", order.feeSat.formatToModernDisplay())
228+
DetailRow("Network Fee", order.networkFeeSat.formatToModernDisplay())
229+
DetailRow("Service Fee", order.serviceFeeSat.formatToModernDisplay())
232230
}
233231
}
234232
}
@@ -242,10 +240,7 @@ private fun OrderDetailView(
242240
.padding(16.dp)
243241
) {
244242
Column(modifier = Modifier.padding(16.dp)) {
245-
Text(
246-
text = "Channel Settings",
247-
style = MaterialTheme.typography.titleMedium
248-
)
243+
Text(text = "Channel Settings", style = MaterialTheme.typography.titleMedium)
249244
Spacer(modifier = Modifier.height(8.dp))
250245
DetailRow("Zero Conf", if (order.zeroConf) "Yes" else "No")
251246
DetailRow("Zero Reserve", if (order.zeroReserve) "Yes" else "No")
@@ -268,10 +263,7 @@ private fun OrderDetailView(
268263
.padding(16.dp)
269264
) {
270265
Column(modifier = Modifier.padding(16.dp)) {
271-
Text(
272-
text = "LSP Information",
273-
style = MaterialTheme.typography.titleMedium
274-
)
266+
Text(text = "LSP Information", style = MaterialTheme.typography.titleMedium)
275267
Spacer(modifier = Modifier.height(8.dp))
276268
DetailRow("Alias", order.lspNode.alias)
277269
DetailRow("Node ID", order.lspNode.pubkey)
@@ -300,7 +292,7 @@ private fun OrderDetailView(
300292
DetailRow("Coupon Code", couponCode)
301293
order.discount?.let { discount ->
302294
DetailRow("Discount Type", discount.code)
303-
DetailRow("Value", "${discount.absoluteSat}")
295+
DetailRow("Value", discount.absoluteSat.formatToModernDisplay())
304296
}
305297
}
306298
}
@@ -316,10 +308,7 @@ private fun OrderDetailView(
316308
.padding(16.dp)
317309
) {
318310
Column(modifier = Modifier.padding(16.dp)) {
319-
Text(
320-
text = "Timestamps",
321-
style = MaterialTheme.typography.titleMedium
322-
)
311+
Text(text = "Timestamps", style = MaterialTheme.typography.titleMedium)
323312
Spacer(modifier = Modifier.height(8.dp))
324313
DetailRow("Created", order.createdAt)
325314
DetailRow("Updated", order.updatedAt)
@@ -391,14 +380,11 @@ private fun CJitDetailView(
391380
.padding(16.dp)
392381
) {
393382
Column(modifier = Modifier.padding(16.dp)) {
394-
Text(
395-
text = "Entry Details",
396-
style = MaterialTheme.typography.titleMedium
397-
)
383+
Text(text = "Entry Details", style = MaterialTheme.typography.titleMedium)
398384
Spacer(modifier = Modifier.height(8.dp))
399385
DetailRow(label = "ID", value = entry.id)
400386
DetailRow(label = "State", value = entry.state.toString())
401-
DetailRow(label = "Channel Size", value = "${entry.channelSizeSat} sats")
387+
DetailRow(label = "Channel Size", value = entry.channelSizeSat.formatToModernDisplay())
402388
entry.channelOpenError?.let { error ->
403389
DetailRow(label = "Error", value = error, isError = true)
404390
}
@@ -415,14 +401,11 @@ private fun CJitDetailView(
415401
.padding(16.dp)
416402
) {
417403
Column(modifier = Modifier.padding(16.dp)) {
418-
Text(
419-
text = "Fees",
420-
style = MaterialTheme.typography.titleMedium
421-
)
404+
Text(text = "Fees", style = MaterialTheme.typography.titleMedium)
422405
Spacer(modifier = Modifier.height(8.dp))
423-
DetailRow(label = "Total Fee", value = "${entry.feeSat} sats")
424-
DetailRow(label = "Network Fee", value = "${entry.networkFeeSat} sats")
425-
DetailRow(label = "Service Fee", value = "${entry.serviceFeeSat} sats")
406+
DetailRow(label = "Total Fee", value = entry.feeSat.formatToModernDisplay())
407+
DetailRow(label = "Network Fee", value = entry.networkFeeSat.formatToModernDisplay())
408+
DetailRow(label = "Service Fee", value = entry.serviceFeeSat.formatToModernDisplay())
426409
}
427410
}
428411
}
@@ -436,10 +419,7 @@ private fun CJitDetailView(
436419
.padding(16.dp)
437420
) {
438421
Column(modifier = Modifier.padding(16.dp)) {
439-
Text(
440-
text = "Channel Settings",
441-
style = MaterialTheme.typography.titleMedium
442-
)
422+
Text(text = "Channel Settings", style = MaterialTheme.typography.titleMedium)
443423
Spacer(modifier = Modifier.height(8.dp))
444424
DetailRow(label = "Node ID", value = entry.nodeId)
445425
DetailRow(label = "Expiry Weeks", value = "${entry.channelExpiryWeeks}")
@@ -456,10 +436,7 @@ private fun CJitDetailView(
456436
.padding(16.dp)
457437
) {
458438
Column(modifier = Modifier.padding(16.dp)) {
459-
Text(
460-
text = "LSP Information",
461-
style = MaterialTheme.typography.titleMedium
462-
)
439+
Text(text = "LSP Information", style = MaterialTheme.typography.titleMedium)
463440
Spacer(modifier = Modifier.height(8.dp))
464441
DetailRow(label = "Alias", value = entry.lspNode.alias)
465442
DetailRow(label = "Node ID", value = entry.lspNode.pubkey)
@@ -477,10 +454,7 @@ private fun CJitDetailView(
477454
.padding(16.dp)
478455
) {
479456
Column(modifier = Modifier.padding(16.dp)) {
480-
Text(
481-
text = "Discount",
482-
style = MaterialTheme.typography.titleMedium
483-
)
457+
Text(text = "Discount", style = MaterialTheme.typography.titleMedium)
484458
Spacer(modifier = Modifier.height(8.dp))
485459
DetailRow(label = "Coupon Code", value = entry.couponCode)
486460
entry.discount?.let { discount ->
@@ -501,10 +475,7 @@ private fun CJitDetailView(
501475
.padding(16.dp)
502476
) {
503477
Column(modifier = Modifier.padding(16.dp)) {
504-
Text(
505-
text = "Timestamps",
506-
style = MaterialTheme.typography.titleMedium
507-
)
478+
Text(text = "Timestamps", style = MaterialTheme.typography.titleMedium)
508479
Spacer(modifier = Modifier.height(8.dp))
509480
DetailRow(label = "Created", value = entry.createdAt)
510481
DetailRow(label = "Updated", value = entry.updatedAt)
@@ -543,7 +514,7 @@ private fun CopyableText(text: String) {
543514
overflow = TextOverflow.Ellipsis,
544515
modifier = Modifier
545516
.scale(scale)
546-
.clickable {
517+
.clickableAlpha {
547518
clipboardManager.setText(AnnotatedString(text))
548519
coroutineScope.launch {
549520
isPressed = true
@@ -557,10 +528,10 @@ private fun CopyableText(text: String) {
557528
@Composable
558529
private fun OrderRow(order: IBtOrder) {
559530
Column(
531+
verticalArrangement = Arrangement.spacedBy(8.dp),
560532
modifier = Modifier
561533
.fillMaxWidth()
562-
.padding(8.dp),
563-
verticalArrangement = Arrangement.spacedBy(8.dp)
534+
.padding(8.dp)
564535
) {
565536
Row(
566537
modifier = Modifier.fillMaxWidth(),
@@ -581,23 +552,23 @@ private fun OrderRow(order: IBtOrder) {
581552
}
582553

583554
Row(
555+
horizontalArrangement = Arrangement.SpaceBetween,
584556
modifier = Modifier.fillMaxWidth(),
585-
horizontalArrangement = Arrangement.SpaceBetween
586557
) {
587-
BalanceInfo(label = "LSP Balance", value = "${order.lspBalanceSat.formatWithDotSeparator()} sats")
588-
BalanceInfo(
558+
InfoCell(label = "LSP Balance", value = order.lspBalanceSat.formatToModernDisplay())
559+
InfoCell(
589560
label = "Client Balance",
590-
value = "${order.clientBalanceSat.formatWithDotSeparator()} sats",
561+
value = order.clientBalanceSat.formatToModernDisplay(),
591562
alignment = Alignment.End
592563
)
593564
}
594565

595566
Row(
567+
horizontalArrangement = Arrangement.SpaceBetween,
596568
modifier = Modifier.fillMaxWidth(),
597-
horizontalArrangement = Arrangement.SpaceBetween
598569
) {
599-
BalanceInfo(label = "Fees", value = "${order.feeSat.formatWithDotSeparator()} sats")
600-
BalanceInfo(
570+
InfoCell(label = "Fees", value = order.feeSat.formatToModernDisplay())
571+
InfoCell(
601572
label = "Expires",
602573
value = order.channelExpiresAt.take(10),
603574
alignment = Alignment.End
@@ -636,10 +607,10 @@ private fun CJitRow(entry: IcJitEntry) {
636607
modifier = Modifier.fillMaxWidth(),
637608
horizontalArrangement = Arrangement.SpaceBetween
638609
) {
639-
BalanceInfo(label = "Channel Size", value = "${entry.channelSizeSat.formatWithDotSeparator()} sats")
640-
BalanceInfo(
610+
InfoCell(label = "Channel Size", value = "${entry.channelSizeSat.formatToModernDisplay()} sats")
611+
InfoCell(
641612
label = "Fees",
642-
value = "${entry.feeSat.formatWithDotSeparator()} sats",
613+
value = "${entry.feeSat.formatToModernDisplay()} sats",
643614
alignment = Alignment.End
644615
)
645616
}
@@ -663,7 +634,7 @@ private fun CJitRow(entry: IcJitEntry) {
663634
}
664635

665636
@Composable
666-
private fun BalanceInfo(label: String, value: String, alignment: Alignment.Horizontal = Alignment.Start) {
637+
private fun InfoCell(label: String, value: String, alignment: Alignment.Horizontal = Alignment.Start) {
667638
Column(horizontalAlignment = alignment) {
668639
Text(
669640
text = label,
@@ -715,7 +686,7 @@ private fun DetailRow(label: String, value: String, isError: Boolean = false) {
715686
textAlign = TextAlign.End,
716687
modifier = Modifier
717688
.scale(scale)
718-
.clickable {
689+
.clickableAlpha {
719690
clipboardManager.setText(AnnotatedString(value))
720691
coroutineScope.launch {
721692
isPressed = true

0 commit comments

Comments
 (0)