2020-06-20 07:10:00 +00:00
|
|
|
import 'package:cake_wallet/core/validator.dart';
|
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
2023-03-15 17:22:08 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
class AmountValidator extends TextValidator {
|
2023-03-15 17:22:08 +00:00
|
|
|
AmountValidator({required CryptoCurrency currency, bool isAutovalidate = false}) {
|
|
|
|
symbolsAmountValidator =
|
|
|
|
SymbolsAmountValidator(isAutovalidate: isAutovalidate);
|
|
|
|
decimalAmountValidator = DecimalAmountValidator(currency: currency);
|
|
|
|
}
|
|
|
|
|
|
|
|
late final SymbolsAmountValidator symbolsAmountValidator;
|
|
|
|
|
|
|
|
late final DecimalAmountValidator decimalAmountValidator;
|
|
|
|
|
|
|
|
String? call(String? value) => symbolsAmountValidator(value) ?? decimalAmountValidator(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
class SymbolsAmountValidator extends TextValidator {
|
|
|
|
SymbolsAmountValidator({required bool isAutovalidate})
|
2020-06-20 07:10:00 +00:00
|
|
|
: super(
|
2023-03-15 17:22:08 +00:00
|
|
|
errorMessage: S.current.error_text_amount,
|
|
|
|
pattern: _pattern(),
|
|
|
|
isAutovalidate: isAutovalidate,
|
|
|
|
minLength: 0,
|
|
|
|
maxLength: 0);
|
|
|
|
|
|
|
|
static String _pattern() => '^([0-9]+([.\,][0-9]+)?|[.\,][0-9]+)\$';
|
|
|
|
}
|
|
|
|
|
|
|
|
class DecimalAmountValidator extends TextValidator {
|
|
|
|
DecimalAmountValidator({required CryptoCurrency currency, bool isAutovalidate = false})
|
|
|
|
: super(
|
|
|
|
errorMessage: S.current.decimal_places_error,
|
|
|
|
pattern: _pattern(currency),
|
2020-07-31 15:29:21 +00:00
|
|
|
isAutovalidate: isAutovalidate,
|
2020-06-20 07:10:00 +00:00
|
|
|
minLength: 0,
|
|
|
|
maxLength: 0);
|
|
|
|
|
2023-03-15 17:22:08 +00:00
|
|
|
static String _pattern(CryptoCurrency currency) {
|
|
|
|
switch (currency) {
|
|
|
|
case CryptoCurrency.xmr:
|
|
|
|
return '^([0-9]+([.\,][0-9]{1,12})?|[.\,][0-9]{1,12})\$';
|
|
|
|
case CryptoCurrency.btc:
|
|
|
|
return '^([0-9]+([.\,][0-9]{1,8})?|[.\,][0-9]{1,8})\$';
|
2020-06-20 07:10:00 +00:00
|
|
|
default:
|
2023-03-15 17:22:08 +00:00
|
|
|
return '^([0-9]+([.\,][0-9]{1,12})?|[.\,][0-9]{1,12})\$';
|
2020-06-20 07:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 07:41:29 +00:00
|
|
|
|
|
|
|
class AllAmountValidator extends TextValidator {
|
2023-03-15 17:22:08 +00:00
|
|
|
AllAmountValidator()
|
|
|
|
: super(
|
|
|
|
errorMessage: S.current.error_text_amount,
|
|
|
|
pattern: S.current.all,
|
|
|
|
minLength: 0,
|
|
|
|
maxLength: 0);
|
2020-10-09 07:41:29 +00:00
|
|
|
}
|