Skip to content

Commit

Permalink
Merge pull request #650 from breez/644-accept-comma
Browse files Browse the repository at this point in the history
644 accept comma
  • Loading branch information
ubbabeck authored Oct 5, 2023
2 parents 69bf46a + d25584b commit 42e9211
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/models/currency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ class BitcoinCurrency extends Object {
RegExp get whitelistedPattern {
switch (tickerSymbol) {
case "BTC":
return RegExp("^\\d+\\.?\\d{0,8}");
return RegExp("^\\d+[.,]?\\d{0,8}");
case "SAT":
return RegExp(r'\d+');
default:
return RegExp("^\\d+\\.?\\d{0,8}");
return RegExp("^\\d+[.,]?\\d{0,8}");
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/utils/fiat_conversion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class FiatConversion {
return (fiatAmount / exchangeRate * 100000000).round();
}

RegExp get whitelistedPattern => currencyData.info.fractionSize == 0
? RegExp(r'\d+')
: RegExp("^\\d+[.,]?\\d{0,${currencyData.info.fractionSize}}");

double satToFiat(int satoshies) {
return satoshies.toDouble() / 100000000 * exchangeRate;
}
Expand Down
9 changes: 8 additions & 1 deletion lib/widgets/amount_form_field/amount_form_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ class AmountFormField extends TextFormField {
enabled: enabled,
controller: controller,
inputFormatters: bitcoinCurrency != BitcoinCurrency.SAT
? [FilteringTextInputFormatter.allow(RegExp(r'\d+\.?\d*'))]
? [
FilteringTextInputFormatter.allow(bitcoinCurrency.whitelistedPattern),
TextInputFormatter.withFunction(
(_, newValue) => newValue.copyWith(
text: newValue.text.replaceAll(',', '.'),
),
),
]
: [SatAmountFormFieldFormatter()],
onFieldSubmitted: onFieldSubmitted,
onSaved: onSaved,
Expand Down
9 changes: 6 additions & 3 deletions lib/widgets/amount_form_field/currency_converter_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ class CurrencyConverterDialogState extends State<CurrencyConverterDialog>
}

final fiatConversion = FiatConversion(fiatCurrency, fiatExchangeRate);
final int fractionSize = fiatCurrency.info.fractionSize;
final borderColor = themeData.isLightTheme ? Colors.red : themeData.colorScheme.error;

return Column(
Expand Down Expand Up @@ -212,10 +211,14 @@ class CurrencyConverterDialogState extends State<CurrencyConverterDialog>
style: themeData.dialogTheme.contentTextStyle,
),
),
// Do not allow '.' when fractionSize is 0 and only allow fiat currencies fractionSize number of digits after decimal point
inputFormatters: [
FilteringTextInputFormatter.allow(
fractionSize == 0 ? RegExp(r'\d+') : RegExp("^\\d+\\.?\\d{0,$fractionSize}"),
fiatConversion.whitelistedPattern,
),
TextInputFormatter.withFunction(
(_, newValue) => newValue.copyWith(
text: newValue.text.replaceAll(',', '.'),
),
),
],
keyboardType: const TextInputType.numberWithOptions(decimal: true),
Expand Down

0 comments on commit 42e9211

Please sign in to comment.