| 1 | import 'package:cake_wallet/core/amount_parsing_proxy.dart'; |
| 2 | import 'package:cake_wallet/core/validator.dart'; |
| 3 | import 'package:cake_wallet/generated/i18n.dart'; |
| 4 | import 'package:cw_core/crypto_currency.dart'; |
| 5 | import 'package:cw_core/currency.dart'; |
| 6 | |
| 7 | class AmountValidator extends TextValidator { |
| 8 | AmountValidator({ |
| 9 | required CryptoCurrency currency, |
| 10 | required AmountParsingProxy amountParsingProxy, |
| 11 | bool isAutovalidate = false, |
| 12 | String? minValue, |
| 13 | String? maxValue, |
| 14 | }) { |
| 15 | symbolsAmountValidator = SymbolsAmountValidator(isAutovalidate: isAutovalidate); |
| 16 | decimalAmountValidator = |
| 17 | DecimalAmountValidator(currency: currency, isAutovalidate: isAutovalidate); |
| 18 | |
| 19 | amountMinValidator = AmountMinValidator( |
| 20 | minValue: minValue, |
| 21 | isAutovalidate: isAutovalidate, |
| 22 | amountParsingProxy: amountParsingProxy, |
| 23 | cryptoCurrency: currency, |
| 24 | ); |
| 25 | |
| 26 | amountMaxValidator = AmountMaxValidator( |
| 27 | maxValue: maxValue, |
| 28 | isAutovalidate: isAutovalidate, |
| 29 | amountParsingProxy: amountParsingProxy, |
| 30 | cryptoCurrency: currency, |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | late final AmountMinValidator amountMinValidator; |
| 35 | |
| 36 | late final AmountMaxValidator amountMaxValidator; |
| 37 | |
| 38 | late final SymbolsAmountValidator symbolsAmountValidator; |
| 39 | |
| 40 | late final DecimalAmountValidator decimalAmountValidator; |
| 41 | |
| 42 | String? call(String? value) { |
| 43 | if (value == null || value.isEmpty) { |
| 44 | return S.current.error_text_amount; |
| 45 | } |
| 46 | |
| 47 | //* Validate for Text(length, symbols, decimals etc) |
| 48 | |
| 49 | final textValidation = symbolsAmountValidator(value) ?? decimalAmountValidator(value); |
| 50 | |
| 51 | //* Validate for Comparison(Value greater than min and less than ) |
| 52 | final comparisonValidation = amountMinValidator(value) ?? amountMaxValidator(value); |
| 53 | |
| 54 | return textValidation ?? comparisonValidation; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | class SymbolsAmountValidator extends TextValidator { |
| 59 | SymbolsAmountValidator({required bool isAutovalidate}) |
| 60 | : super( |
| 61 | errorMessage: S.current.error_text_amount, |
| 62 | pattern: _pattern(), |
| 63 | isAutovalidate: isAutovalidate, |
| 64 | minLength: 0, |
| 65 | maxLength: 0, |
| 66 | ); |
| 67 | |
| 68 | static String _pattern() => r'^(?:\d+(?:[.\,]\d+)?|[.\,]\d+)$'; |
| 69 | } |
| 70 | |
| 71 | class DecimalAmountValidator extends TextValidator { |
| 72 | DecimalAmountValidator({required Currency currency, required bool isAutovalidate}) |
| 73 | : super( |
| 74 | errorMessage: S.current.decimal_places_error, |
| 75 | pattern: _pattern(currency), |
| 76 | isAutovalidate: isAutovalidate, |
| 77 | minLength: 0, |
| 78 | maxLength: 0, |
| 79 | ); |
| 80 | |
| 81 | static String _pattern(Currency currency) => |
| 82 | '^([0-9]+([.\,][0-9]{1,${currency.decimals}})?|[.\,][0-9]{1,${currency.decimals}})\$'; |
| 83 | } |
| 84 | |
| 85 | class AllAmountValidator extends TextValidator { |
| 86 | AllAmountValidator() |
| 87 | : super( |
| 88 | errorMessage: S.current.error_text_amount, |
| 89 | pattern: S.current.all, |
| 90 | minLength: 0, |
| 91 | maxLength: 0, |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | class AmountMinValidator extends Validator<String> { |
| 96 | final String? minValue; |
| 97 | final bool isAutovalidate; |
| 98 | final AmountParsingProxy amountParsingProxy; |
| 99 | final CryptoCurrency cryptoCurrency; |
| 100 | |
| 101 | AmountMinValidator({ |
| 102 | this.minValue, |
| 103 | required this.isAutovalidate, |
| 104 | required this.amountParsingProxy, |
| 105 | required this.cryptoCurrency, |
| 106 | }) : super(errorMessage: S.current.error_text_input_below_minimum_limit); |
| 107 | |
| 108 | @override |
| 109 | bool isValid(String? value) { |
| 110 | if (value == null || value.isEmpty) { |
| 111 | return isAutovalidate ? true : false; |
| 112 | } |
| 113 | |
| 114 | if (minValue == null || minValue == "null") { |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | value = amountParsingProxy.getCanonicalCryptoAmount(value, cryptoCurrency); |
| 119 | final valueInDouble = parseToDouble(value); |
| 120 | final minInDouble = parseToDouble(minValue ?? ''); |
| 121 | |
| 122 | if (valueInDouble == null || minInDouble == null) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | return valueInDouble >= minInDouble; |
| 127 | } |
| 128 | |
| 129 | double? parseToDouble(String value) { |
| 130 | final data = double.tryParse(value.replaceAll(',', '.')); |
| 131 | return data; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | class AmountMaxValidator extends Validator<String> { |
| 136 | final String? maxValue; |
| 137 | final bool isAutovalidate; |
| 138 | final AmountParsingProxy amountParsingProxy; |
| 139 | final CryptoCurrency cryptoCurrency; |
| 140 | |
| 141 | AmountMaxValidator({ |
| 142 | this.maxValue, |
| 143 | required this.isAutovalidate, |
| 144 | required this.amountParsingProxy, |
| 145 | required this.cryptoCurrency, |
| 146 | }) : super(errorMessage: S.current.error_text_input_above_maximum_limit); |
| 147 | |
| 148 | @override |
| 149 | bool isValid(String? value) { |
| 150 | if (value == null || value.isEmpty) { |
| 151 | return isAutovalidate ? true : false; |
| 152 | } |
| 153 | |
| 154 | if (maxValue == null || maxValue == "null") { |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | value = amountParsingProxy.getCanonicalCryptoAmount(value, cryptoCurrency); |
| 159 | final valueInDouble = parseToDouble(value); |
| 160 | final maxInDouble = parseToDouble(maxValue ?? ''); |
| 161 | |
| 162 | if (valueInDouble == null || maxInDouble == null) { |
| 163 | return false; |
| 164 | } |
| 165 | return valueInDouble <= maxInDouble; |
| 166 | } |
| 167 | |
| 168 | double? parseToDouble(String value) { |
| 169 | return double.tryParse(value.replaceAll(',', '.')); |
| 170 | } |
| 171 | } |