mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-01 12:04:21 +01:00
Cw 262 better handle user exchange amount below minimum or maximum trade size (#868)
* CW-262-Better-handle-user-exchange-amount-below-minimum-or-maximum-trade-size * fix: App should compute conversion even if it's not within the limits
This commit is contained in:
@@ -3,17 +3,45 @@ import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
|
||||
class AmountValidator extends TextValidator {
|
||||
AmountValidator({required CryptoCurrency currency, bool isAutovalidate = false}) {
|
||||
AmountValidator({
|
||||
required CryptoCurrency currency,
|
||||
bool isAutovalidate = false,
|
||||
String? minValue,
|
||||
String? maxValue,
|
||||
}) {
|
||||
symbolsAmountValidator =
|
||||
SymbolsAmountValidator(isAutovalidate: isAutovalidate);
|
||||
decimalAmountValidator = DecimalAmountValidator(currency: currency,isAutovalidate: isAutovalidate);
|
||||
|
||||
amountMinValidator = AmountMinValidator(
|
||||
minValue: minValue,
|
||||
isAutovalidate: isAutovalidate,
|
||||
);
|
||||
|
||||
amountMaxValidator = AmountMaxValidator(
|
||||
maxValue: maxValue,
|
||||
isAutovalidate: isAutovalidate,
|
||||
);
|
||||
}
|
||||
|
||||
late final AmountMinValidator amountMinValidator;
|
||||
|
||||
late final AmountMaxValidator amountMaxValidator;
|
||||
|
||||
late final SymbolsAmountValidator symbolsAmountValidator;
|
||||
|
||||
late final DecimalAmountValidator decimalAmountValidator;
|
||||
|
||||
String? call(String? value) => symbolsAmountValidator(value) ?? decimalAmountValidator(value);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
class SymbolsAmountValidator extends TextValidator {
|
||||
@@ -57,3 +85,71 @@ class AllAmountValidator extends TextValidator {
|
||||
minLength: 0,
|
||||
maxLength: 0);
|
||||
}
|
||||
|
||||
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(',', '.'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -456,8 +456,7 @@ class ExchangePage extends BasePage {
|
||||
depositAmountController.addListener(() {
|
||||
if (depositAmountController.text != exchangeViewModel.depositAmount) {
|
||||
_depositAmountDebounce.run(() {
|
||||
exchangeViewModel.changeDepositAmount(
|
||||
amount: depositAmountController.text);
|
||||
exchangeViewModel.changeDepositAmount(amount: depositAmountController.text);
|
||||
exchangeViewModel.isReceiveAmountEntered = false;
|
||||
});
|
||||
}
|
||||
@@ -469,8 +468,7 @@ class ExchangePage extends BasePage {
|
||||
receiveAmountController.addListener(() {
|
||||
if (receiveAmountController.text != exchangeViewModel.receiveAmount) {
|
||||
_receiveAmountDebounce.run(() {
|
||||
exchangeViewModel.changeReceiveAmount(
|
||||
amount: receiveAmountController.text);
|
||||
exchangeViewModel.changeReceiveAmount(amount: receiveAmountController.text);
|
||||
exchangeViewModel.isReceiveAmountEntered = true;
|
||||
});
|
||||
}
|
||||
@@ -626,8 +624,16 @@ class ExchangePage extends BasePage {
|
||||
currencyButtonColor: Colors.transparent,
|
||||
addressButtonsColor: Theme.of(context).focusColor!,
|
||||
borderColor: Theme.of(context).primaryTextTheme!.bodyText1!.color!,
|
||||
currencyValueValidator:
|
||||
AmountValidator(currency: exchangeViewModel.depositCurrency),
|
||||
currencyValueValidator: (value) {
|
||||
return !exchangeViewModel.isFixedRateMode
|
||||
? AmountValidator(
|
||||
isAutovalidate: true,
|
||||
currency: exchangeViewModel.depositCurrency,
|
||||
minValue: exchangeViewModel.limits.min.toString(),
|
||||
maxValue: exchangeViewModel.limits.max.toString(),
|
||||
).call(value)
|
||||
: null;
|
||||
},
|
||||
addressTextFieldValidator:
|
||||
AddressValidator(type: exchangeViewModel.depositCurrency),
|
||||
onPushPasteButton: (context) async {
|
||||
@@ -668,8 +674,16 @@ class ExchangePage extends BasePage {
|
||||
addressButtonsColor: Theme.of(context).focusColor!,
|
||||
borderColor:
|
||||
Theme.of(context).primaryTextTheme!.bodyText1!.decorationColor!,
|
||||
currencyValueValidator:
|
||||
AmountValidator(currency: exchangeViewModel.receiveCurrency),
|
||||
currencyValueValidator: (value) {
|
||||
return exchangeViewModel.isFixedRateMode
|
||||
? AmountValidator(
|
||||
isAutovalidate: true,
|
||||
currency: exchangeViewModel.receiveCurrency,
|
||||
minValue: exchangeViewModel.limits.min.toString(),
|
||||
maxValue: exchangeViewModel.limits.max.toString(),
|
||||
).call(value)
|
||||
: null;
|
||||
},
|
||||
addressTextFieldValidator:
|
||||
AddressValidator(type: exchangeViewModel.receiveCurrency),
|
||||
onPushPasteButton: (context) async {
|
||||
|
||||
@@ -198,6 +198,9 @@ abstract class ExchangeViewModelBase with Store {
|
||||
@observable
|
||||
bool isFixedRateMode;
|
||||
|
||||
@observable
|
||||
Limits limits;
|
||||
|
||||
@computed
|
||||
SyncStatus get status => wallet.syncStatus;
|
||||
|
||||
@@ -241,8 +244,6 @@ abstract class ExchangeViewModelBase with Store {
|
||||
|
||||
List<CryptoCurrency> depositCurrencies;
|
||||
|
||||
Limits limits;
|
||||
|
||||
NumberFormat _cryptoNumberFormat;
|
||||
|
||||
final SettingsStore _settingsStore;
|
||||
@@ -320,6 +321,22 @@ abstract class ExchangeViewModelBase with Store {
|
||||
.replaceAll(RegExp('\\,'), '');
|
||||
}
|
||||
|
||||
bool checkIfInputMeetsMinOrMaxCondition(String input) {
|
||||
final _enteredAmount = double.tryParse(input.replaceAll(',', '.')) ?? 0;
|
||||
double minLimit = limits.min ?? 0;
|
||||
double? maxLimit = limits.max;
|
||||
|
||||
if (_enteredAmount < minLimit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (maxLimit != null && _enteredAmount > maxLimit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> _calculateBestRate() async {
|
||||
final amount = double.tryParse(isFixedRateMode ? receiveAmount : depositAmount) ?? 1;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user