Skip to content

Commit 39b04d7

Browse files
committed
address review comments, improve logic
1 parent 7c9f59e commit 39b04d7

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
@@ -149,12 +149,13 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
149149

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

157-
return hasSelectedPaymentMethod &&
157+
return hasSelectedQuote &&
158+
hasSelectedPaymentMethod &&
158159
isPaymentMethodLoaded &&
159160
isBuySellQuotLoaded;
160161
}
@@ -213,15 +214,17 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
213214
}
214215

215216
if (bestRateQuote != null) {
217+
if (bestRateQuote!.fiatCurrency != fiatCurrency) {
218+
cryptoAmount = '';
219+
return;
220+
}
216221
_cryptoNumberFormat.maximumFractionDigits = cryptoCurrency.decimals;
217222
cryptoAmount = _cryptoNumberFormat
218223
.format(enteredAmount / bestRateQuote!.rate)
219224
.toString()
220225
.replaceAll(RegExp('\\,'), '');
221226
} else {
222-
if (bestRateQuote != null || fiatCurrency == bestRateQuote?.fiatCurrency) {
223-
await calculateBestRate();
224-
}
227+
await calculateBestRate();
225228
}
226229
}
227230

@@ -246,14 +249,16 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
246249
}
247250

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

@@ -433,7 +438,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
433438
return;
434439
}
435440

436-
final result = await Future.wait<List<Quote>?>(validProvidersNative.map((element) => element
441+
final resultNative = await Future.wait<List<Quote>?>(validProvidersNative.map((element) => element
437442
.fetchQuote(
438443
cryptoCurrency: cryptoCurrency,
439444
fiatCurrency: fiatCurrency,
@@ -446,6 +451,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
446451
Duration(seconds: 10),
447452
onTimeout: () => null,
448453
)));
454+
449455
final resultSepa = await Future.wait<List<Quote>?>(validProvidersSepa.map((element) => element
450456
.fetchQuote(
451457
cryptoCurrency: cryptoCurrency,
@@ -463,7 +469,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
463469
sortedRecommendedQuotes.clear();
464470
sortedQuotes.clear();
465471

466-
final validQuotesNative = result
472+
final validQuotesNative = resultNative
467473
.where((element) => element != null && element.isNotEmpty)
468474
.expand((element) => element!)
469475
.toList();
@@ -489,6 +495,12 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
489495
return true;
490496
}).toList();
491497

498+
final List<Quote> uniqueProviderQuotesSepa = validQuotesSepa.where((element) {
499+
if (addedProviders.contains(element.provider.title)) return false;
500+
addedProviders.add(element.provider.title);
501+
return true;
502+
}).toList();
503+
492504

493505
final List<Quote> successRateQuotes = [...validQuotesNative, ...validQuotesSepa].where((element) =>
494506
element.provider is OnRamperBuyProvider &&
@@ -498,7 +510,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
498510
final List<Quote> uniqueProviderQuotes = [];
499511

500512
for (final quote in successRateQuotes) {
501-
if (!uniqueProviderQuotesNative.contains(quote)) {
513+
if (![...uniqueProviderQuotesNative, ...uniqueProviderQuotesSepa].contains(quote)) {
502514
uniqueProviderQuotes.add(quote);
503515
}
504516
}
@@ -522,6 +534,10 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
522534

523535
selectedQuote = sortedRecommendedQuotes.first;
524536
sortedRecommendedQuotes.first.setIsSelected = true;
537+
} else if (sortedQuotes.isNotEmpty) {
538+
sortedQuotes.first.setIsSelected = true;
539+
bestRateQuote = sortedQuotes.first;
540+
selectedQuote = sortedQuotes.first;
525541
}
526542

527543
buySellQuotState = BuySellQuotLoaded();

0 commit comments

Comments
 (0)