diff options
author | Iván Ávalos <avalos@disroot.org> | 2023-09-15 12:22:09 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2023-09-22 17:01:03 -0600 |
commit | cf086d5981867a510aa17032f2c97f6b8dae6e82 (patch) | |
tree | 7dfb553ee809718b0fca6ce1cbe12a8038c71d4c | |
parent | c3d7a6665d8efd74593fa4d753400f26ecbf5558 (diff) | |
download | taler-android-cf086d5981867a510aa17032f2c97f6b8dae6e82.tar.gz taler-android-cf086d5981867a510aa17032f2c97f6b8dae6e82.tar.bz2 taler-android-cf086d5981867a510aa17032f2c97f6b8dae6e82.zip |
[wallet] fix: AmountInputField reacts to external changes
-rw-r--r-- | wallet/src/main/java/net/taler/wallet/compose/AmountInputField.kt | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/compose/AmountInputField.kt b/wallet/src/main/java/net/taler/wallet/compose/AmountInputField.kt index a9503d7..9abc03d 100644 --- a/wallet/src/main/java/net/taler/wallet/compose/AmountInputField.kt +++ b/wallet/src/main/java/net/taler/wallet/compose/AmountInputField.kt @@ -17,11 +17,14 @@ package net.taler.wallet.compose import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.padding import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.LocalTextStyle import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Text import androidx.compose.material3.TextFieldColors import androidx.compose.material3.TextFieldDefaults import androidx.compose.runtime.Composable @@ -38,6 +41,8 @@ import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.OffsetMapping import androidx.compose.ui.text.input.TransformedText import androidx.compose.ui.text.input.VisualTransformation +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp import net.taler.common.Amount import java.text.DecimalFormat @@ -62,13 +67,22 @@ fun AmountInputField( colors: TextFieldColors = TextFieldDefaults.outlinedTextFieldColors() ) { val decimalSeparator = DecimalFormat().decimalFormatSymbols.decimalSeparator - var intermediate by remember { mutableStateOf(value) } + var tmpIn by remember { mutableStateOf(value) } + + // React to external changes + val tmpOut = remember(tmpIn, value) { + transformOutput(tmpIn, decimalSeparator, '.').let { + if (value != it) value else tmpIn + } + } + OutlinedTextField( - value = intermediate, + value = tmpOut, onValueChange = { input -> val filtered = transformOutput(input, decimalSeparator, '.') if (Amount.isValidAmountStr(filtered)) { - intermediate = transformInput(input, decimalSeparator, '.') + tmpIn = transformInput(input, decimalSeparator, '.') + // tmpIn = input onValueChange(filtered) } }, @@ -143,4 +157,19 @@ private fun transformOutput( it.endsWith(outputDecimalSeparator) -> "${it}0" else -> it } +} + +@Preview +@Composable +fun AmountInputFieldPreview() { + var value by remember { mutableStateOf("0") } + TalerSurface { + Column { + Text(modifier = Modifier.padding(16.dp), text = value) + AmountInputField( + value = value, + onValueChange = { value = it }, + ) + } + } }
\ No newline at end of file |