Skip to content

Commit bc550b6

Browse files
committed
fix: placeholder glitching
1 parent d5e2e7e commit bc550b6

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

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

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import to.bitkit.models.formatToModernDisplay
2525
import to.bitkit.ui.currencyViewModel
2626
import to.bitkit.ui.theme.Colors
2727

28+
2829
@Composable
2930
fun NumberPadTextField(
3031
input: String,
@@ -35,7 +36,8 @@ fun NumberPadTextField(
3536
val currency = currencyViewModel ?: return
3637

3738
val satoshis = if (primaryDisplay == PrimaryDisplay.FIAT) {
38-
currency.convertFiatToSats(fiatAmount = input.replace(",", "").toDoubleOrNull() ?: 0.0).toString()
39+
currency.convertFiatToSats(fiatAmount = input.replace(",", "").toDoubleOrNull() ?: 0.0)
40+
.toString()
3941
} else {
4042
input.removeSpaces()
4143
}
@@ -44,44 +46,60 @@ fun NumberPadTextField(
4446
var placeholderFractional: String by remember { mutableStateOf("") }
4547
var value: String by remember { mutableStateOf("") }
4648

47-
LaunchedEffect (displayUnit, primaryDisplay) {
49+
LaunchedEffect(displayUnit, primaryDisplay) {
4850
placeholderFractional = when {
4951
displayUnit == BitcoinDisplayUnit.CLASSIC -> "00000000"
5052
primaryDisplay == PrimaryDisplay.FIAT -> "00"
5153
else -> ""
5254
}
5355

5456
placeholder = if (placeholderFractional.isNotEmpty()) {
55-
"0.$placeholderFractional"
57+
if (input.contains(".") || primaryDisplay == PrimaryDisplay.FIAT) {
58+
"0.$placeholderFractional"
59+
} else {
60+
".$placeholderFractional"
61+
}
5662
} else {
57-
""
63+
"0"
5864
}
65+
66+
value = ""
5967
}
6068

6169
if (input.isNotEmpty()) {
62-
val whole = input.split(".").firstOrNull().orEmpty().removeSpaces()
63-
val fraction = input.split(".").getOrNull(1).orEmpty().removeSpaces()
64-
65-
if (primaryDisplay == PrimaryDisplay.FIAT) {
66-
value = whole
67-
}
70+
val parts = input.split(".")
71+
val whole = parts.firstOrNull().orEmpty().removeSpaces()
72+
val fraction = parts.getOrNull(1).orEmpty().removeSpaces()
73+
74+
value = when {
75+
primaryDisplay == PrimaryDisplay.FIAT -> {
76+
if (input.contains(".")) {
77+
"$whole.$fraction"
78+
} else {
79+
whole
80+
}
81+
}
6882

69-
if (input.contains(".")) {
70-
placeholder = ""
71-
if (placeholderFractional.length >= fraction.length) {
72-
placeholder = placeholderFractional.drop(fraction.length)
83+
displayUnit == BitcoinDisplayUnit.MODERN && primaryDisplay == PrimaryDisplay.BITCOIN -> {
84+
input.toLongOrDefault(0L).formatToModernDisplay()
7385
}
7486

75-
if (primaryDisplay == PrimaryDisplay.FIAT) {
76-
value = "$whole.$fraction"
87+
else -> {
88+
whole
7789
}
78-
} else {
79-
if (displayUnit == BitcoinDisplayUnit.MODERN && primaryDisplay == PrimaryDisplay.BITCOIN) {
80-
value = input.toLongOrDefault(0L).formatToModernDisplay()
81-
placeholder = ""
82-
} else {
83-
placeholder = ".$placeholderFractional"
90+
}
91+
92+
placeholder = when {
93+
input.contains(".") -> {
94+
if (fraction.length < placeholderFractional.length) {
95+
placeholderFractional.drop(fraction.length)
96+
} else {
97+
""
98+
}
8499
}
100+
101+
displayUnit == BitcoinDisplayUnit.MODERN && primaryDisplay == PrimaryDisplay.BITCOIN -> ""
102+
else -> if (placeholderFractional.isNotEmpty()) ".$placeholderFractional" else ""
85103
}
86104
} else {
87105
value = ""

0 commit comments

Comments
 (0)