Skip to content

Commit 92997c4

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

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

lib/view_model/buy/buy_sell_view_model.dart

Lines changed: 22 additions & 12 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();
@@ -493,7 +499,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
493499

494500
final Set<String> addedProviders = {};
495501

496-
final List<Quote> uniqueProviderQuotesNative = validQuotesNative.where((element) {
502+
final List<Quote> uniqueProviderQuotesSepa = validQuotesSepa.where((element) {
497503
if (addedProviders.contains(element.provider.title)) return false;
498504
addedProviders.add(element.provider.title);
499505
return true;
@@ -508,7 +514,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
508514
final List<Quote> uniqueProviderQuotes = [];
509515

510516
for (final quote in successRateQuotes) {
511-
if (!uniqueProviderQuotesNative.contains(quote)) {
517+
if (!uniqueProviderQuotesSepa.contains(quote)) {
512518
uniqueProviderQuotes.add(quote);
513519
}
514520
}
@@ -532,6 +538,10 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
532538

533539
selectedQuote = sortedRecommendedQuotes.first;
534540
sortedRecommendedQuotes.first.setIsSelected = true;
541+
} else if (sortedQuotes.isNotEmpty) {
542+
sortedQuotes.first.setIsSelected = true;
543+
bestRateQuote = sortedQuotes.first;
544+
selectedQuote = sortedQuotes.first;
535545
}
536546

537547
buySellQuotState = BuySellQuotLoaded();

0 commit comments

Comments
 (0)