From 51acd366b4da075ce856a0fd42c8f3ed8d6fec00 Mon Sep 17 00:00:00 2001 From: julian Date: Wed, 3 May 2023 14:40:39 -0600 Subject: [PATCH] fix: swap currency updating bug and some updating optimizations --- lib/pages/exchange_view/exchange_form.dart | 38 +++++++++++++--------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/pages/exchange_view/exchange_form.dart b/lib/pages/exchange_view/exchange_form.dart index e95151e9f..4ca1ba5fc 100644 --- a/lib/pages/exchange_view/exchange_form.dart +++ b/lib/pages/exchange_view/exchange_form.dart @@ -614,13 +614,15 @@ class _ExchangeFormState extends ConsumerState { ? ref.read(efReceiveAmountProvider) : ref.read(efSendAmountProvider); - if (amount == null || amount <= Decimal.zero) { + final pair = ref.read(efCurrencyPairProvider); + if (amount == null || + amount <= Decimal.zero || + pair.send == null || + pair.receive == null) { ref.read(efRefreshingProvider.notifier).state = false; return; } - final rateType = ref.read(efRateTypeProvider); - final pair = ref.read(efCurrencyPairProvider); for (final exchange in exchanges) { final sendCurrency = pair.send?.forExchange(exchange.name); @@ -657,7 +659,9 @@ class _ExchangeFormState extends ConsumerState { ); } - if (estimateResponse.value != null && rangeResponse.value != null) { + if (estimateResponse.value != null && + rangeResponse.value != null && + mounted) { ref.read(efEstimatesListProvider(exchange.name).notifier).state = Tuple2(estimateResponse.value!, rangeResponse.value!); } @@ -670,16 +674,12 @@ class _ExchangeFormState extends ConsumerState { } void updateSend(Estimate? estimate) { - WidgetsBinding.instance.addPostFrameCallback((_) { - ref.read(efSendAmountProvider.notifier).state = estimate?.estimatedAmount; - }); + ref.read(efSendAmountProvider.notifier).state = estimate?.estimatedAmount; } void updateReceive(Estimate? estimate) { - WidgetsBinding.instance.addPostFrameCallback((_) { - ref.read(efReceiveAmountProvider.notifier).state = - estimate?.estimatedAmount; - }); + ref.read(efReceiveAmountProvider.notifier).state = + estimate?.estimatedAmount; } @override @@ -699,7 +699,8 @@ class _ExchangeFormState extends ConsumerState { } }); _receiveFocusNode.addListener(() { - if (_receiveFocusNode.hasFocus) { + if (_receiveFocusNode.hasFocus && + ref.read(efExchangeProvider).name != ChangeNowExchange.exchangeName) { WidgetsBinding.instance.addPostFrameCallback((_) { ref.read(efReversedProvider.notifier).state = true; }); @@ -746,6 +747,8 @@ class _ExchangeFormState extends ConsumerState { void dispose() { _receiveController.dispose(); _sendController.dispose(); + _receiveFocusNode.dispose(); + _sendFocusNode.dispose(); super.dispose(); } @@ -778,15 +781,18 @@ class _ExchangeFormState extends ConsumerState { }); ref.listen(efEstimateProvider.notifier, (previous, next) { + final estimate = (next as StateController).state; if (ref.read(efReversedProvider)) { - updateSend((next as StateController).state); + updateSend(estimate); } else { - updateReceive((next as StateController).state); + updateReceive(estimate); } }); ref.listen(efCurrencyPairProvider, (previous, next) { - update(); + if (!_swapLock) { + update(); + } }); return Column( @@ -883,7 +889,7 @@ class _ExchangeFormState extends ConsumerState { ), ), ), - ) + ), ), ], ),