use static min and max amounts from BuyFormState class

This commit is contained in:
sneurlax 2023-01-26 15:39:57 -06:00
parent d7eb25aa9c
commit f64a2518ad

View file

@ -76,8 +76,8 @@ class _BuyFormState extends ConsumerState<BuyForm> {
List<Fiat>? fiats; List<Fiat>? fiats;
String? _address; String? _address;
Fiat? selectedFiat; static Fiat? selectedFiat;
Crypto? selectedCrypto; static Crypto? selectedCrypto;
SimplexQuote quote = SimplexQuote( SimplexQuote quote = SimplexQuote(
crypto: Crypto.fromJson({'ticker': 'BTC', 'name': 'Bitcoin'}), crypto: Crypto.fromJson({'ticker': 'BTC', 'name': 'Bitcoin'}),
fiat: Fiat.fromJson({'ticker': 'USD', 'name': 'United States Dollar'}), fiat: Fiat.fromJson({'ticker': 'USD', 'name': 'United States Dollar'}),
@ -88,18 +88,18 @@ class _BuyFormState extends ConsumerState<BuyForm> {
buyWithFiat: true, buyWithFiat: true,
); // TODO enum this or something ); // TODO enum this or something
bool buyWithFiat = true; static bool buyWithFiat = true;
bool _addressToggleFlag = false; bool _addressToggleFlag = false;
bool _hovering1 = false; bool _hovering1 = false;
bool _hovering2 = false; bool _hovering2 = false;
Decimal minFiat = Decimal.fromInt(50); static Decimal minFiat = Decimal.fromInt(50);
Decimal maxFiat = Decimal.fromInt(20000); static Decimal maxFiat = Decimal.fromInt(20000);
Decimal minCrypto = Decimal.parse((0.00000001) static Decimal minCrypto = Decimal.parse((0.00000001)
.toString()); // lol how to go from double->Decimal more easily? .toString()); // lol how to go from double->Decimal more easily?
Decimal maxCrypto = Decimal.parse((10000.00000000).toString()); static Decimal maxCrypto = Decimal.parse((10000.00000000).toString());
String boundedCryptoTicker = 'BTC'; static String boundedCryptoTicker = 'BTC';
void fiatFieldOnChanged(String value) async {} void fiatFieldOnChanged(String value) async {}
@ -492,13 +492,13 @@ class _BuyFormState extends ConsumerState<BuyForm> {
(errorMessage.indexOf('getQuote exception: ') ?? 19) + 20, (errorMessage.indexOf('getQuote exception: ') ?? 19) + 20,
errorMessage.indexOf(", value: null")); errorMessage.indexOf(", value: null"));
if (errorMessage.contains('must be between')) { if (errorMessage.contains('must be between')) {
boundedCryptoTicker = errorMessage.substring( _BuyFormState.boundedCryptoTicker = errorMessage.substring(
errorMessage.indexOf('The ') + 4, errorMessage.indexOf('The ') + 4,
errorMessage.indexOf(' must be between')); errorMessage.indexOf(' amount must be between'));
minCrypto = Decimal.parse(errorMessage.substring( _BuyFormState.minCrypto = Decimal.parse(errorMessage.substring(
errorMessage.indexOf('must be between ') + 16, errorMessage.indexOf('must be between ') + 16,
errorMessage.indexOf(' and '))); errorMessage.indexOf(' and ')));
maxCrypto = Decimal.parse(errorMessage.substring( _BuyFormState.maxCrypto = Decimal.parse(errorMessage.substring(
errorMessage.indexOf("$minCrypto and ") + "$minCrypto and ".length, errorMessage.indexOf("$minCrypto and ") + "$minCrypto and ".length,
errorMessage.length)); errorMessage.length));
} }
@ -925,15 +925,7 @@ class _BuyFormState extends ConsumerState<BuyForm> {
decimal: true, decimal: true,
), ),
textAlign: TextAlign.left, textAlign: TextAlign.left,
inputFormatters: [ inputFormatters: [NumericalRangeFormatter()],
NumericalRangeFormatter(
min: buyWithFiat ? minFiat : minCrypto,
max: buyWithFiat ? maxFiat : maxCrypto,
buyWithFiat: buyWithFiat,
cryptoTicker: selectedCrypto?.ticker ?? 'BTC',
boundedCryptoTicker: boundedCryptoTicker,
)
],
decoration: InputDecoration( decoration: InputDecoration(
contentPadding: const EdgeInsets.only( contentPadding: const EdgeInsets.only(
// top: 22, // top: 22,
@ -1347,18 +1339,7 @@ class _BuyFormState extends ConsumerState<BuyForm> {
// See https://stackoverflow.com/a/68072967 // See https://stackoverflow.com/a/68072967
class NumericalRangeFormatter extends TextInputFormatter { class NumericalRangeFormatter extends TextInputFormatter {
final Decimal min; NumericalRangeFormatter();
final Decimal max;
final bool buyWithFiat;
final String cryptoTicker;
final String boundedCryptoTicker;
NumericalRangeFormatter(
{required this.min,
required this.max,
required this.buyWithFiat,
required this.cryptoTicker,
required this.boundedCryptoTicker});
@override @override
TextEditingValue formatEditUpdate( TextEditingValue formatEditUpdate(
@ -1368,28 +1349,32 @@ class NumericalRangeFormatter extends TextInputFormatter {
if (newValue.text == '') { if (newValue.text == '') {
return newValue; return newValue;
} else { } else {
if (buyWithFiat) { if (_BuyFormState.buyWithFiat) {
if (Decimal.parse(newValue.text) < min) { if (Decimal.parse(newValue.text) < _BuyFormState.minFiat) {
newValue = newValue = const TextEditingValue()
const TextEditingValue().copyWith(text: min.toStringAsFixed(2)); .copyWith(text: _BuyFormState.minFiat.toStringAsFixed(2));
} else { } else {
newValue = Decimal.parse(newValue.text) > max newValue = Decimal.parse(newValue.text) > _BuyFormState.maxFiat
? const TextEditingValue().copyWith(text: max.toStringAsFixed(2)) ? const TextEditingValue()
.copyWith(text: _BuyFormState.maxFiat.toStringAsFixed(2))
: newValue; : newValue;
} }
} else if (cryptoTicker == boundedCryptoTicker) { } else if (!_BuyFormState.buyWithFiat &&
if (Decimal.parse(newValue.text) < min) { _BuyFormState.selectedCrypto?.ticker ==
newValue = _BuyFormState.boundedCryptoTicker) {
const TextEditingValue().copyWith(text: min.toStringAsFixed(8)); if (Decimal.parse(newValue.text) < _BuyFormState.maxCrypto) {
newValue = const TextEditingValue()
.copyWith(text: _BuyFormState.minCrypto.toStringAsFixed(8));
} else { } else {
newValue = Decimal.parse(newValue.text) > max newValue = Decimal.parse(newValue.text) > _BuyFormState.maxCrypto
? const TextEditingValue().copyWith(text: max.toStringAsFixed(8)) ? const TextEditingValue()
.copyWith(text: _BuyFormState.maxCrypto.toStringAsFixed(8))
: newValue; : newValue;
} }
} }
} }
final regexString = buyWithFiat final regexString = _BuyFormState.buyWithFiat
? r'^([0-9]*[,.]?[0-9]{0,2}|[,.][0-9]{0,2})$' ? r'^([0-9]*[,.]?[0-9]{0,2}|[,.][0-9]{0,2})$'
: r'^([0-9]*[,.]?[0-9]{0,8}|[,.][0-9]{0,8})$'; : r'^([0-9]*[,.]?[0-9]{0,8}|[,.][0-9]{0,8})$';