fix bug where wallet coin wouldn't be shown in exchange if changenow data failed to load before opening the exchange from within a wallet

This commit is contained in:
julian 2022-09-09 08:32:15 -06:00
parent bc669489a4
commit c18d456493
2 changed files with 32 additions and 12 deletions

View file

@ -351,7 +351,7 @@ class _WalletViewState extends ConsumerState<WalletView> {
void _loadCNData() { void _loadCNData() {
// unawaited future // unawaited future
_cnLoadingService.loadAll(ref); _cnLoadingService.loadAll(ref, coin: ref.read(managerProvider).coin);
} }
@override @override

View file

@ -6,14 +6,15 @@ import 'package:stackwallet/providers/exchange/changenow_initial_load_status.dar
import 'package:stackwallet/providers/exchange/estimate_rate_exchange_form_provider.dart'; import 'package:stackwallet/providers/exchange/estimate_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/fixed_rate_exchange_form_provider.dart'; import 'package:stackwallet/providers/exchange/fixed_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/fixed_rate_market_pairs_provider.dart'; import 'package:stackwallet/providers/exchange/fixed_rate_market_pairs_provider.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
class ChangeNowLoadingService { class ChangeNowLoadingService {
Future<void> loadAll(WidgetRef ref) async { Future<void> loadAll(WidgetRef ref, {Coin? coin}) async {
try { try {
await Future.wait([ await Future.wait([
_loadFixedRateMarkets(ref), _loadFixedRateMarkets(ref, coin: coin),
_loadChangeNowStandardCurrencies(ref), _loadChangeNowStandardCurrencies(ref, coin: coin),
]); ]);
} catch (e, s) { } catch (e, s) {
Logging.instance.log("ChangeNowLoadingService.loadAll failed: $e\n$s", Logging.instance.log("ChangeNowLoadingService.loadAll failed: $e\n$s",
@ -21,7 +22,7 @@ class ChangeNowLoadingService {
} }
} }
Future<void> _loadFixedRateMarkets(WidgetRef ref) async { Future<void> _loadFixedRateMarkets(WidgetRef ref, {Coin? coin}) async {
if (ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state == if (ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state ==
ChangeNowLoadStatus.loading) { ChangeNowLoadStatus.loading) {
// already in progress so just // already in progress so just
@ -38,8 +39,15 @@ class ChangeNowLoadingService {
response3.value!; response3.value!;
if (ref.read(fixedRateExchangeFormProvider).market == null) { if (ref.read(fixedRateExchangeFormProvider).market == null) {
final matchingMarkets = String fromTicker = "btc";
response3.value!.where((e) => e.to == "doge" && e.from == "btc"); String toTicker = "xmr";
if (coin != null) {
fromTicker = coin.ticker.toLowerCase();
}
final matchingMarkets = response3.value!
.where((e) => e.to == toTicker && e.from == fromTicker);
if (matchingMarkets.isNotEmpty) { if (matchingMarkets.isNotEmpty) {
await ref await ref
.read(fixedRateExchangeFormProvider) .read(fixedRateExchangeFormProvider)
@ -60,7 +68,8 @@ class ChangeNowLoadingService {
ChangeNowLoadStatus.success; ChangeNowLoadStatus.success;
} }
Future<void> _loadChangeNowStandardCurrencies(WidgetRef ref) async { Future<void> _loadChangeNowStandardCurrencies(WidgetRef ref,
{Coin? coin}) async {
if (ref if (ref
.read(changeNowEstimatedInitialLoadStatusStateProvider.state) .read(changeNowEstimatedInitialLoadStatusStateProvider.state)
.state == .state ==
@ -82,17 +91,28 @@ class ChangeNowLoadingService {
ref.read(availableFloatingRatePairsStateProvider.state).state = ref.read(availableFloatingRatePairsStateProvider.state).state =
response2.value!; response2.value!;
String fromTicker = "btc";
String toTicker = "xmr";
if (coin != null) {
fromTicker = coin.ticker.toLowerCase();
}
if (response.value!.length > 1) { if (response.value!.length > 1) {
if (ref.read(estimatedRateExchangeFormProvider).from == null) { if (ref.read(estimatedRateExchangeFormProvider).from == null) {
if (response.value!.where((e) => e.ticker == "btc").isNotEmpty) { if (response.value!
.where((e) => e.ticker == fromTicker)
.isNotEmpty) {
await ref.read(estimatedRateExchangeFormProvider).updateFrom( await ref.read(estimatedRateExchangeFormProvider).updateFrom(
response.value!.firstWhere((e) => e.ticker == "btc"), false); response.value!.firstWhere((e) => e.ticker == fromTicker),
false);
} }
} }
if (ref.read(estimatedRateExchangeFormProvider).to == null) { if (ref.read(estimatedRateExchangeFormProvider).to == null) {
if (response.value!.where((e) => e.ticker == "doge").isNotEmpty) { if (response.value!.where((e) => e.ticker == toTicker).isNotEmpty) {
await ref.read(estimatedRateExchangeFormProvider).updateTo( await ref.read(estimatedRateExchangeFormProvider).updateTo(
response.value!.firstWhere((e) => e.ticker == "doge"), false); response.value!.firstWhere((e) => e.ticker == toTicker),
false);
} }
} }
} }