address review comments, improve logic

This commit is contained in:
Czarek Nakamoto 2025-04-11 09:28:16 +02:00
parent 7c9f59e668
commit 39b04d7e73

View file

@ -149,12 +149,13 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
@computed @computed
bool get isReadyToTrade { bool get isReadyToTrade {
// final hasSelectedQuote = selectedQuote != null; final hasSelectedQuote = selectedQuote != null;
final hasSelectedPaymentMethod = selectedPaymentMethod != null; final hasSelectedPaymentMethod = selectedPaymentMethod != null;
final isPaymentMethodLoaded = paymentMethodState is PaymentMethodLoaded; final isPaymentMethodLoaded = paymentMethodState is PaymentMethodLoaded;
final isBuySellQuotLoaded = buySellQuotState is BuySellQuotLoaded; final isBuySellQuotLoaded = buySellQuotState is BuySellQuotLoaded;
return hasSelectedPaymentMethod && return hasSelectedQuote &&
hasSelectedPaymentMethod &&
isPaymentMethodLoaded && isPaymentMethodLoaded &&
isBuySellQuotLoaded; isBuySellQuotLoaded;
} }
@ -213,17 +214,19 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
} }
if (bestRateQuote != null) { if (bestRateQuote != null) {
if (bestRateQuote!.fiatCurrency != fiatCurrency) {
cryptoAmount = '';
return;
}
_cryptoNumberFormat.maximumFractionDigits = cryptoCurrency.decimals; _cryptoNumberFormat.maximumFractionDigits = cryptoCurrency.decimals;
cryptoAmount = _cryptoNumberFormat cryptoAmount = _cryptoNumberFormat
.format(enteredAmount / bestRateQuote!.rate) .format(enteredAmount / bestRateQuote!.rate)
.toString() .toString()
.replaceAll(RegExp('\\,'), ''); .replaceAll(RegExp('\\,'), '');
} else { } else {
if (bestRateQuote != null || fiatCurrency == bestRateQuote?.fiatCurrency) {
await calculateBestRate(); await calculateBestRate();
} }
} }
}
@action @action
Future<void> changeCryptoAmount({required String amount}) async { Future<void> changeCryptoAmount({required String amount}) async {
@ -246,16 +249,18 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
} }
if (bestRateQuote != null) { if (bestRateQuote != null) {
if (bestRateQuote!.cryptoCurrency != cryptoCurrency) {
fiatAmount = '';
return;
}
fiatAmount = _cryptoNumberFormat fiatAmount = _cryptoNumberFormat
.format(enteredAmount * bestRateQuote!.rate) .format(enteredAmount * bestRateQuote!.rate)
.toString() .toString()
.replaceAll(RegExp('\\,'), ''); .replaceAll(RegExp('\\,'), '');
} else { } else {
if (bestRateQuote != null || fiatCurrency == bestRateQuote?.fiatCurrency) {
await calculateBestRate(); await calculateBestRate();
} }
} }
}
@action @action
void changeOption(SelectableOption option) { void changeOption(SelectableOption option) {
@ -433,7 +438,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
return; return;
} }
final result = await Future.wait<List<Quote>?>(validProvidersNative.map((element) => element final resultNative = await Future.wait<List<Quote>?>(validProvidersNative.map((element) => element
.fetchQuote( .fetchQuote(
cryptoCurrency: cryptoCurrency, cryptoCurrency: cryptoCurrency,
fiatCurrency: fiatCurrency, fiatCurrency: fiatCurrency,
@ -446,6 +451,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
Duration(seconds: 10), Duration(seconds: 10),
onTimeout: () => null, onTimeout: () => null,
))); )));
final resultSepa = await Future.wait<List<Quote>?>(validProvidersSepa.map((element) => element final resultSepa = await Future.wait<List<Quote>?>(validProvidersSepa.map((element) => element
.fetchQuote( .fetchQuote(
cryptoCurrency: cryptoCurrency, cryptoCurrency: cryptoCurrency,
@ -463,7 +469,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
sortedRecommendedQuotes.clear(); sortedRecommendedQuotes.clear();
sortedQuotes.clear(); sortedQuotes.clear();
final validQuotesNative = result final validQuotesNative = resultNative
.where((element) => element != null && element.isNotEmpty) .where((element) => element != null && element.isNotEmpty)
.expand((element) => element!) .expand((element) => element!)
.toList(); .toList();
@ -489,6 +495,12 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
return true; return true;
}).toList(); }).toList();
final List<Quote> uniqueProviderQuotesSepa = validQuotesSepa.where((element) {
if (addedProviders.contains(element.provider.title)) return false;
addedProviders.add(element.provider.title);
return true;
}).toList();
final List<Quote> successRateQuotes = [...validQuotesNative, ...validQuotesSepa].where((element) => final List<Quote> successRateQuotes = [...validQuotesNative, ...validQuotesSepa].where((element) =>
element.provider is OnRamperBuyProvider && element.provider is OnRamperBuyProvider &&
@ -498,7 +510,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
final List<Quote> uniqueProviderQuotes = []; final List<Quote> uniqueProviderQuotes = [];
for (final quote in successRateQuotes) { for (final quote in successRateQuotes) {
if (!uniqueProviderQuotesNative.contains(quote)) { if (![...uniqueProviderQuotesNative, ...uniqueProviderQuotesSepa].contains(quote)) {
uniqueProviderQuotes.add(quote); uniqueProviderQuotes.add(quote);
} }
} }
@ -522,6 +534,10 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S
selectedQuote = sortedRecommendedQuotes.first; selectedQuote = sortedRecommendedQuotes.first;
sortedRecommendedQuotes.first.setIsSelected = true; sortedRecommendedQuotes.first.setIsSelected = true;
} else if (sortedQuotes.isNotEmpty) {
sortedQuotes.first.setIsSelected = true;
bestRateQuote = sortedQuotes.first;
selectedQuote = sortedQuotes.first;
} }
buySellQuotState = BuySellQuotLoaded(); buySellQuotState = BuySellQuotLoaded();