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';
|
2023-04-21 18:03:42 +00:00
|
|
|
import 'package:cw_core/currency.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
class AmountValidator extends TextValidator {
|
2023-04-20 01:13:37 +00:00
|
|
|
AmountValidator({
|
|
|
|
required CryptoCurrency currency,
|
|
|
|
bool isAutovalidate = false,
|
|
|
|
String? minValue,
|
|
|
|
String? maxValue,
|
|
|
|
}) {
|
2023-03-15 17:22:08 +00:00
|
|
|
symbolsAmountValidator =
|
|
|
|
SymbolsAmountValidator(isAutovalidate: isAutovalidate);
|
2023-03-17 11:33:45 +00:00
|
|
|
decimalAmountValidator = DecimalAmountValidator(currency: currency,isAutovalidate: isAutovalidate);
|
2023-04-20 01:13:37 +00:00
|
|
|
|
|
|
|
amountMinValidator = AmountMinValidator(
|
|
|
|
minValue: minValue,
|
|
|
|
isAutovalidate: isAutovalidate,
|
|
|
|
);
|
|
|
|
|
|
|
|
amountMaxValidator = AmountMaxValidator(
|
|
|
|
maxValue: maxValue,
|
|
|
|
isAutovalidate: isAutovalidate,
|
|
|
|
);
|
2023-03-15 17:22:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 01:13:37 +00:00
|
|
|
late final AmountMinValidator amountMinValidator;
|
|
|
|
|
|
|
|
late final AmountMaxValidator amountMaxValidator;
|
|
|
|
|
2023-03-15 17:22:08 +00:00
|
|
|
late final SymbolsAmountValidator symbolsAmountValidator;
|
|
|
|
|
|
|
|
late final DecimalAmountValidator decimalAmountValidator;
|
|
|
|
|
2023-04-20 01:13:37 +00:00
|
|
|
String? call(String? value) {
|
|
|
|
//* Validate for Text(length, symbols, decimals etc)
|
|
|
|
|
|
|
|
final textValidation = symbolsAmountValidator(value) ?? decimalAmountValidator(value);
|
|
|
|
|
|
|
|
//* Validate for Comparison(Value greater than min and less than )
|
|
|
|
final comparisonValidation = amountMinValidator(value) ?? amountMaxValidator(value);
|
|
|
|
|
|
|
|
return textValidation ?? comparisonValidation;
|
|
|
|
}
|
2023-03-15 17:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-04-21 18:03:42 +00:00
|
|
|
DecimalAmountValidator({required Currency currency, required bool isAutovalidate })
|
2023-03-15 17:22:08 +00:00
|
|
|
: 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-04-21 18:03:42 +00:00
|
|
|
static String _pattern(Currency currency) {
|
2023-03-15 17:22:08 +00:00
|
|
|
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
|
|
|
}
|
2023-04-20 01:13:37 +00:00
|
|
|
|
|
|
|
class AmountMinValidator extends Validator<String> {
|
|
|
|
final String? minValue;
|
|
|
|
final bool isAutovalidate;
|
|
|
|
|
|
|
|
AmountMinValidator({
|
|
|
|
this.minValue,
|
|
|
|
required this.isAutovalidate,
|
|
|
|
}) : super(errorMessage: S.current.error_text_input_below_minimum_limit);
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool isValid(String? value) {
|
|
|
|
if (value == null || value.isEmpty) {
|
|
|
|
return isAutovalidate ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minValue == null || minValue == "null") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
final valueInDouble = parseToDouble(value);
|
|
|
|
final minInDouble = parseToDouble(minValue ?? '');
|
|
|
|
|
|
|
|
if (valueInDouble == null || minInDouble == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueInDouble > minInDouble;
|
|
|
|
}
|
|
|
|
|
|
|
|
double? parseToDouble(String value) {
|
|
|
|
final data = double.tryParse(value.replaceAll(',', '.'));
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AmountMaxValidator extends Validator<String> {
|
|
|
|
final String? maxValue;
|
|
|
|
final bool isAutovalidate;
|
|
|
|
|
|
|
|
AmountMaxValidator({
|
|
|
|
this.maxValue,
|
|
|
|
required this.isAutovalidate,
|
|
|
|
}) : super(errorMessage: S.current.error_text_input_above_maximum_limit);
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool isValid(String? value) {
|
|
|
|
if (value == null || value.isEmpty) {
|
|
|
|
return isAutovalidate ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxValue == null || maxValue == "null") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
final valueInDouble = parseToDouble(value);
|
|
|
|
final maxInDouble = parseToDouble(maxValue ?? '');
|
|
|
|
|
|
|
|
if (valueInDouble == null || maxInDouble == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return valueInDouble < maxInDouble;
|
|
|
|
}
|
|
|
|
|
|
|
|
double? parseToDouble(String value) {
|
|
|
|
return double.tryParse(value.replaceAll(',', '.'));
|
|
|
|
}
|
|
|
|
}
|