tighten swap estimate refreshing

This commit is contained in:
julian-CStack 2023-05-04 08:58:50 -06:00
parent 87ed2af7ce
commit a1b7c9248b
2 changed files with 42 additions and 11 deletions

View file

@ -604,7 +604,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
} }
Future<void> update() async { Future<void> update() async {
ref.read(efRefreshingProvider.notifier).state = true; _addUpdate();
for (final exchange in exchanges) { for (final exchange in exchanges) {
ref.read(efEstimatesListProvider(exchange.name).notifier).state = null; ref.read(efEstimatesListProvider(exchange.name).notifier).state = null;
} }
@ -619,7 +619,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
amount <= Decimal.zero || amount <= Decimal.zero ||
pair.send == null || pair.send == null ||
pair.receive == null) { pair.receive == null) {
ref.read(efRefreshingProvider.notifier).state = false; _removeUpdate();
return; return;
} }
final rateType = ref.read(efRateTypeProvider); final rateType = ref.read(efRateTypeProvider);
@ -659,18 +659,36 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
); );
} }
if (estimateResponse.value != null && if (mounted) {
rangeResponse.value != null && if (estimateResponse.value != null && rangeResponse.value != null) {
mounted) { ref.read(efEstimatesListProvider(exchange.name).notifier).state =
ref.read(efEstimatesListProvider(exchange.name).notifier).state = Tuple2(estimateResponse.value!, rangeResponse.value!);
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; ref.read(efRefreshingProvider.notifier).state = false;
}); }
} }
void updateSend(Estimate? estimate) { void updateSend(Estimate? estimate) {

View file

@ -28,10 +28,23 @@ final efSendAmountProvider = StateProvider<Decimal?>((ref) => null);
final efReceiveAmountProvider = StateProvider<Decimal?>((ref) => null); final efReceiveAmountProvider = StateProvider<Decimal?>((ref) => null);
final efSendAmountStringProvider = StateProvider<String>((ref) { final efSendAmountStringProvider = StateProvider<String>((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<String>((ref) { final efReceiveAmountStringProvider = StateProvider<String>((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<bool>((ref) => false); final efReversedProvider = StateProvider<bool>((ref) => false);