fix: swap currency updating bug and some updating optimizations

This commit is contained in:
julian 2023-05-03 14:40:39 -06:00
parent bea6cd366d
commit 51acd366b4

View file

@ -614,13 +614,15 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
? ref.read(efReceiveAmountProvider) ? ref.read(efReceiveAmountProvider)
: ref.read(efSendAmountProvider); : 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; ref.read(efRefreshingProvider.notifier).state = false;
return; return;
} }
final rateType = ref.read(efRateTypeProvider); final rateType = ref.read(efRateTypeProvider);
final pair = ref.read(efCurrencyPairProvider);
for (final exchange in exchanges) { for (final exchange in exchanges) {
final sendCurrency = pair.send?.forExchange(exchange.name); final sendCurrency = pair.send?.forExchange(exchange.name);
@ -657,7 +659,9 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
); );
} }
if (estimateResponse.value != null && 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!);
} }
@ -670,16 +674,12 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
} }
void updateSend(Estimate? estimate) { 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) { void updateReceive(Estimate? estimate) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ref.read(efReceiveAmountProvider.notifier).state = ref.read(efReceiveAmountProvider.notifier).state =
estimate?.estimatedAmount; estimate?.estimatedAmount;
});
} }
@override @override
@ -699,7 +699,8 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
} }
}); });
_receiveFocusNode.addListener(() { _receiveFocusNode.addListener(() {
if (_receiveFocusNode.hasFocus) { if (_receiveFocusNode.hasFocus &&
ref.read(efExchangeProvider).name != ChangeNowExchange.exchangeName) {
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
ref.read(efReversedProvider.notifier).state = true; ref.read(efReversedProvider.notifier).state = true;
}); });
@ -746,6 +747,8 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
void dispose() { void dispose() {
_receiveController.dispose(); _receiveController.dispose();
_sendController.dispose(); _sendController.dispose();
_receiveFocusNode.dispose();
_sendFocusNode.dispose();
super.dispose(); super.dispose();
} }
@ -778,15 +781,18 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
}); });
ref.listen(efEstimateProvider.notifier, (previous, next) { ref.listen(efEstimateProvider.notifier, (previous, next) {
final estimate = (next as StateController<Estimate?>).state;
if (ref.read(efReversedProvider)) { if (ref.read(efReversedProvider)) {
updateSend((next as StateController<Estimate?>).state); updateSend(estimate);
} else { } else {
updateReceive((next as StateController<Estimate?>).state); updateReceive(estimate);
} }
}); });
ref.listen(efCurrencyPairProvider, (previous, next) { ref.listen(efCurrencyPairProvider, (previous, next) {
if (!_swapLock) {
update(); update();
}
}); });
return Column( return Column(
@ -883,7 +889,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
), ),
), ),
), ),
) ),
), ),
], ],
), ),