- Recalculate best rate if isFixedRate changed - check limits against the receive amount if isFixedRate

- Recalculate best rate if isFixedRate changed - check limits against the receive amount if isFixedRate

OmarHatem committed Dec 6, 2022 at 13:30 UTC 36c27026881c4bbbd957fd65cfbfb64a9c76d301
1 file changed +13 -14
lib/view_model/exchange/exchange_view_model.dart
+13 -14
@@ -44,7 +44,6 @@ abstract class ExchangeViewModelBase with Store {
44 ExchangeViewModelBase(this.wallet, this.trades, this._exchangeTemplateStore,
45 this.tradesStore, this._settingsStore, this.sharedPreferences)
46 : _cryptoNumberFormat = NumberFormat(),
47 - isReverse = false,
47 isFixedRateMode = false,
48 isReceiveAmountEntered = false,
49 depositAmount = '',
@@ -112,7 +111,11 @@ abstract class ExchangeViewModelBase with Store {
111 loadLimits();
112 reaction(
113 (_) => isFixedRateMode,
115 - (Object _) => loadLimits());
114 + (Object _) {
115 + loadLimits();
116 + _bestRate = 0;
117 + _calculateBestRate();
118 + });
119 }
120
121 final WalletBase wallet;
@@ -227,8 +230,6 @@ abstract class ExchangeViewModelBase with Store {
230
231 Limits limits;
232
230 - bool isReverse;
231 -
233 NumberFormat _cryptoNumberFormat;
234
235 final SettingsStore _settingsStore;
@@ -258,7 +259,6 @@ abstract class ExchangeViewModelBase with Store {
259 @action
260 Future<void> changeReceiveAmount({required String amount}) async {
261 receiveAmount = amount;
261 - isReverse = true;
262
263 if (amount.isEmpty) {
264 depositAmount = '';
@@ -283,7 +283,6 @@ abstract class ExchangeViewModelBase with Store {
283 @action
284 Future<void> changeDepositAmount({required String amount}) async {
285 depositAmount = amount;
286 - isReverse = false;
286
287 if (amount.isEmpty) {
288 depositAmount = '';
@@ -307,7 +306,7 @@ abstract class ExchangeViewModelBase with Store {
306 }
307
308 Future<void> _calculateBestRate() async {
310 - final amount = double.tryParse(isFixedRateMode ? receiveAmount : depositAmount) ?? 1;
309 + final amount = double.tryParse(depositAmount) ?? 1;
310
311 final result = await Future.wait<double>(
312 _tradeAvailableProviders
@@ -401,7 +400,7 @@ abstract class ExchangeViewModelBase with Store {
400 settleAddress: receiveAddress,
401 refundAddress: depositAddress,
402 );
404 - amount = depositAmount;
403 + amount = isFixedRateMode ? receiveAmount : depositAmount;
404 }
405
406 if (provider is SimpleSwapExchangeProvider) {
@@ -412,7 +411,7 @@ abstract class ExchangeViewModelBase with Store {
411 address: receiveAddress,
412 refundAddress: depositAddress,
413 );
415 - amount = depositAmount;
414 + amount = isFixedRateMode ? receiveAmount : depositAmount;
415 }
416
417 if (provider is XMRTOExchangeProvider) {
@@ -424,7 +423,7 @@ abstract class ExchangeViewModelBase with Store {
423 address: receiveAddress,
424 refundAddress: depositAddress,
425 isBTCRequest: isReceiveAmountEntered);
427 - amount = depositAmount;
426 + amount = isFixedRateMode ? receiveAmount : depositAmount;
427 }
428
429 if (provider is ChangeNowExchangeProvider) {
@@ -435,8 +434,8 @@ abstract class ExchangeViewModelBase with Store {
434 toAmount: receiveAmount.replaceAll(',', '.'),
435 refundAddress: depositAddress,
436 address: receiveAddress,
438 - isReverse: isReverse);
439 - amount = isReverse ? receiveAmount : depositAmount;
437 + isReverse: isFixedRateMode);
438 + amount = isFixedRateMode ? receiveAmount : depositAmount;
439 }
440
441 if (provider is MorphTokenExchangeProvider) {
@@ -446,13 +445,13 @@ abstract class ExchangeViewModelBase with Store {
445 amount: depositAmount.replaceAll(',', '.'),
446 refundAddress: depositAddress,
447 address: receiveAddress);
449 - amount = depositAmount;
448 + amount = isFixedRateMode ? receiveAmount : depositAmount;
449 }
450
451 amount = amount.replaceAll(',', '.');
452
453 if (limitsState is LimitsLoadedSuccessfully) {
455 - if (double.parse(amount) < limits.min!) {
454 + if (limits.max != null && double.parse(amount) < limits.min!) {
455 continue;
456 } else if (limits.max != null && double.parse(amount) > limits.max!) {
457 continue;