From a1b7c9248ba4406d6e1a08ae3040328eb1887ce2 Mon Sep 17 00:00:00 2001 From: julian-CStack Date: Thu, 4 May 2023 08:58:50 -0600 Subject: [PATCH] tighten swap estimate refreshing --- lib/pages/exchange_view/exchange_form.dart | 36 ++++++++++++++----- .../exchange_form_state_provider.dart | 17 +++++++-- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/lib/pages/exchange_view/exchange_form.dart b/lib/pages/exchange_view/exchange_form.dart index 6e7deaa5a..65a83a3cd 100644 --- a/lib/pages/exchange_view/exchange_form.dart +++ b/lib/pages/exchange_view/exchange_form.dart @@ -604,7 +604,7 @@ class _ExchangeFormState extends ConsumerState { } Future update() async { - ref.read(efRefreshingProvider.notifier).state = true; + _addUpdate(); for (final exchange in exchanges) { ref.read(efEstimatesListProvider(exchange.name).notifier).state = null; } @@ -619,7 +619,7 @@ class _ExchangeFormState extends ConsumerState { amount <= Decimal.zero || pair.send == null || pair.receive == null) { - ref.read(efRefreshingProvider.notifier).state = false; + _removeUpdate(); return; } final rateType = ref.read(efRateTypeProvider); @@ -659,18 +659,36 @@ class _ExchangeFormState extends ConsumerState { ); } - if (estimateResponse.value != null && - rangeResponse.value != null && - mounted) { - ref.read(efEstimatesListProvider(exchange.name).notifier).state = - Tuple2(estimateResponse.value!, rangeResponse.value!); + if (mounted) { + if (estimateResponse.value != null && rangeResponse.value != null) { + ref.read(efEstimatesListProvider(exchange.name).notifier).state = + Tuple2(estimateResponse.value!, rangeResponse.value!); + } else { + ref.read(efEstimatesListProvider(exchange.name).notifier).state = + null; + } } } } - WidgetsBinding.instance.addPostFrameCallback((_) { + // WidgetsBinding.instance.addPostFrameCallback((_) { + _removeUpdate(); + // }); + } + + int _updateCount = 0; + + void _addUpdate() { + _updateCount++; + ref.read(efRefreshingProvider.notifier).state = true; + } + + void _removeUpdate() { + _updateCount--; + if (_updateCount <= 0) { + _updateCount = 0; ref.read(efRefreshingProvider.notifier).state = false; - }); + } } void updateSend(Estimate? estimate) { diff --git a/lib/providers/exchange/exchange_form_state_provider.dart b/lib/providers/exchange/exchange_form_state_provider.dart index 1148c97f6..937fcc7db 100644 --- a/lib/providers/exchange/exchange_form_state_provider.dart +++ b/lib/providers/exchange/exchange_form_state_provider.dart @@ -28,10 +28,23 @@ final efSendAmountProvider = StateProvider((ref) => null); final efReceiveAmountProvider = StateProvider((ref) => null); final efSendAmountStringProvider = StateProvider((ref) { - return ref.watch(efSendAmountProvider)?.toStringAsFixed(8) ?? ""; + final refreshing = ref.watch(efRefreshingProvider); + final reversed = ref.watch(efReversedProvider); + if (refreshing && reversed) { + return "-"; + } else { + return ref.watch(efSendAmountProvider)?.toStringAsFixed(8) ?? ""; + } }); final efReceiveAmountStringProvider = StateProvider((ref) { - return ref.watch(efReceiveAmountProvider)?.toStringAsFixed(8) ?? ""; + final refreshing = ref.watch(efRefreshingProvider); + final reversed = ref.watch(efReversedProvider); + + if (refreshing && reversed == false) { + return "-"; + } else { + return ref.watch(efReceiveAmountProvider)?.toStringAsFixed(8) ?? ""; + } }); final efReversedProvider = StateProvider((ref) => false);