Skip to content

Commit 967aa58

Browse files
committed
address review comments, improve logic [skip ci]
1 parent 1fe879b commit 967aa58

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

lib/view_model/buy/buy_sell_view_model.dart

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
148148

149149
@computed
150150
bool get isReadyToTrade {
151-
// final hasSelectedQuote = selectedQuote != null;
151+
final hasSelectedQuote = selectedQuote != null;
152152
final hasSelectedPaymentMethod = selectedPaymentMethod != null;
153153
final isPaymentMethodLoaded = paymentMethodState is PaymentMethodLoaded;
154154
final isBuySellQuotLoaded = buySellQuotState is BuySellQuotLoaded;
155155

156-
return hasSelectedPaymentMethod &&
156+
return hasSelectedQuote &&
157+
hasSelectedPaymentMethod &&
157158
isPaymentMethodLoaded &&
158159
isBuySellQuotLoaded;
159160
}
@@ -212,15 +213,17 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
212213
}
213214

214215
if (bestRateQuote != null) {
216+
if (bestRateQuote!.fiatCurrency != fiatCurrency) {
217+
cryptoAmount = '';
218+
return;
219+
}
215220
_cryptoNumberFormat.maximumFractionDigits = cryptoCurrency.decimals;
216221
cryptoAmount = _cryptoNumberFormat
217222
.format(enteredAmount / bestRateQuote!.rate)
218223
.toString()
219224
.replaceAll(RegExp('\\,'), '');
220225
} else {
221-
if (bestRateQuote != null || fiatCurrency == bestRateQuote?.fiatCurrency) {
222-
await calculateBestRate();
223-
}
226+
await calculateBestRate();
224227
}
225228
}
226229

@@ -245,14 +248,16 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
245248
}
246249

247250
if (bestRateQuote != null) {
251+
if (bestRateQuote!.cryptoCurrency != cryptoCurrency) {
252+
fiatAmount = '';
253+
return;
254+
}
248255
fiatAmount = _cryptoNumberFormat
249256
.format(enteredAmount * bestRateQuote!.rate)
250257
.toString()
251258
.replaceAll(RegExp('\\,'), '');
252259
} else {
253-
if (bestRateQuote != null || fiatCurrency == bestRateQuote?.fiatCurrency) {
254-
await calculateBestRate();
255-
}
260+
await calculateBestRate();
256261
}
257262
}
258263

@@ -442,7 +447,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
442447
return;
443448
}
444449

445-
final result = await Future.wait<List<Quote>?>(validProvidersNative.map((element) => element
450+
final resultNative = await Future.wait<List<Quote>?>(validProvidersNative.map((element) => element
446451
.fetchQuote(
447452
cryptoCurrency: cryptoCurrency,
448453
fiatCurrency: fiatCurrency,
@@ -456,6 +461,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
456461
Duration(seconds: 10),
457462
onTimeout: () => null,
458463
)));
464+
459465
final resultSepa = await Future.wait<List<Quote>?>(validProvidersSepa.map((element) => element
460466
.fetchQuote(
461467
cryptoCurrency: cryptoCurrency,
@@ -473,7 +479,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
473479
sortedRecommendedQuotes.clear();
474480
sortedQuotes.clear();
475481

476-
final validQuotesNative = result
482+
final validQuotesNative = resultNative
477483
.where((element) => element != null && element.isNotEmpty)
478484
.expand((element) => element!)
479485
.toList();
@@ -499,6 +505,12 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
499505
return true;
500506
}).toList();
501507

508+
final List<Quote> uniqueProviderQuotesSepa = validQuotesSepa.where((element) {
509+
if (addedProviders.contains(element.provider.title)) return false;
510+
addedProviders.add(element.provider.title);
511+
return true;
512+
}).toList();
513+
502514

503515
final List<Quote> successRateQuotes = [...validQuotesNative, ...validQuotesSepa].where((element) =>
504516
element.provider is OnRamperBuyProvider &&
@@ -508,7 +520,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
508520
final List<Quote> uniqueProviderQuotes = [];
509521

510522
for (final quote in successRateQuotes) {
511-
if (!uniqueProviderQuotesNative.contains(quote)) {
523+
if (![...uniqueProviderQuotesNative, ...uniqueProviderQuotesSepa].contains(quote)) {
512524
uniqueProviderQuotes.add(quote);
513525
}
514526
}
@@ -532,6 +544,10 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
532544

533545
selectedQuote = sortedRecommendedQuotes.first;
534546
sortedRecommendedQuotes.first.setIsSelected = true;
547+
} else if (sortedQuotes.isNotEmpty) {
548+
sortedQuotes.first.setIsSelected = true;
549+
bestRateQuote = sortedQuotes.first;
550+
selectedQuote = sortedQuotes.first;
535551
}
536552

537553
buySellQuotState = BuySellQuotLoaded();

0 commit comments

Comments
 (0)