limit rate calls on amount entry

This commit is contained in:
julian 2023-02-06 13:08:24 -06:00
parent f578ce9c0c
commit b19a3dbbf8

View file

@ -66,38 +66,35 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
bool _swapLock = false; bool _swapLock = false;
// todo: check and adjust this value?
static const _valueCheckInterval = Duration(milliseconds: 500);
Timer? _sendFieldOnChangedTimer;
void sendFieldOnChanged(String value) async { void sendFieldOnChanged(String value) async {
if (_sendFocusNode.hasFocus) { if (_sendFocusNode.hasFocus) {
final newFromAmount = Decimal.tryParse(value); _sendFieldOnChangedTimer?.cancel();
await ref _sendFieldOnChangedTimer = Timer(_valueCheckInterval, () async {
.read(exchangeFormStateProvider) final newFromAmount = Decimal.tryParse(value);
.setSendAmountAndCalculateReceiveAmount(
newFromAmount ?? Decimal.zero, true);
if (newFromAmount == null) { await ref
_receiveController.text = "LOLOK"; .read(exchangeFormStateProvider)
// ref.read(prefsChangeNotifierProvider).exchangeRateType == .setSendAmountAndCalculateReceiveAmount(newFromAmount, true);
// ExchangeRateType.estimated });
// ? "-"
// : "";
}
} }
} }
Timer? _receiveFieldOnChangedTimer;
void receiveFieldOnChanged(String value) async { void receiveFieldOnChanged(String value) async {
final newToAmount = Decimal.tryParse(value); _receiveFieldOnChangedTimer?.cancel();
final isEstimated =
ref.read(prefsChangeNotifierProvider).exchangeRateType == _receiveFieldOnChangedTimer = Timer(_valueCheckInterval, () async {
ExchangeRateType.estimated; final newToAmount = Decimal.tryParse(value);
if (!(isEstimated &&
ref.read(currentExchangeNameStateProvider.state).state == await ref
ChangeNowExchange.exchangeName)) { .read(exchangeFormStateProvider)
ref.read(exchangeFormStateProvider).receiveAmount = newToAmount; .setReceivingAmountAndCalculateSendAmount(newToAmount, true);
} });
if (newToAmount == null) {
_sendController.text = "";
}
} }
void selectSendCurrency() async { void selectSendCurrency() async {
@ -110,6 +107,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
return; return;
} }
// TODO: return currency and await update while showing loading
await _showCurrencySelectionSheet( await _showCurrencySelectionSheet(
willChange: ref.read(exchangeFormStateProvider).sendCurrency, willChange: ref.read(exchangeFormStateProvider).sendCurrency,
willChangeIsSend: true, willChangeIsSend: true,
@ -140,7 +138,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
), ),
); );
await Future<void>.delayed(const Duration(milliseconds: 300)); // await Future<void>.delayed(const Duration(milliseconds: 300));
if (mounted) { if (mounted) {
Navigator.of(context, rootNavigator: true).pop(); Navigator.of(context, rootNavigator: true).pop();
@ -157,9 +155,10 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
return; return;
} }
// TODO: return currency and await update while showing loading
await _showCurrencySelectionSheet( await _showCurrencySelectionSheet(
willChange: ref.read(exchangeFormStateProvider).receiveCurrency, willChange: ref.read(exchangeFormStateProvider).receiveCurrency,
willChangeIsSend: false, willChangeIsSend: false,
paired: ref.read(exchangeFormStateProvider).sendCurrency, paired: ref.read(exchangeFormStateProvider).sendCurrency,
isFixedRate: type == ExchangeRateType.fixed, isFixedRate: type == ExchangeRateType.fixed,
onSelected: (selectedCurrency) => ref onSelected: (selectedCurrency) => ref
@ -187,7 +186,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
), ),
); );
await Future<void>.delayed(const Duration(milliseconds: 300)); // await Future<void>.delayed(const Duration(milliseconds: 300));
if (mounted) { if (mounted) {
Navigator.of(context, rootNavigator: true).pop(); Navigator.of(context, rootNavigator: true).pop();
@ -712,7 +711,6 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
final rateType = ref.watch( final rateType = ref.watch(
prefsChangeNotifierProvider.select((value) => value.exchangeRateType)); prefsChangeNotifierProvider.select((value) => value.exchangeRateType));
// provider for simpleswap; not called rn
ref.listen<String>(currentExchangeNameStateProvider, (previous, next) { ref.listen<String>(currentExchangeNameStateProvider, (previous, next) {
ref.read(exchangeFormStateProvider).updateExchange( ref.read(exchangeFormStateProvider).updateExchange(
exchange: ref.read(exchangeProvider), exchange: ref.read(exchangeProvider),
@ -729,11 +727,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
exchangeFormStateProvider.select((value) => value.toAmountString), exchangeFormStateProvider.select((value) => value.toAmountString),
(previous, String next) { (previous, String next) {
if (!_receiveFocusNode.hasFocus) { if (!_receiveFocusNode.hasFocus) {
// ref.watch(exchangeProvider).name ==
// SimpleSwapExchange.exchangeName &&
_receiveController.text = isEstimated && next.isEmpty ? "-" : next; _receiveController.text = isEstimated && next.isEmpty ? "-" : next;
//todo: check if print needed
// debugPrint("RECEIVE AMOUNT LISTENER ACTIVATED");
if (_swapLock) { if (_swapLock) {
_sendController.text = _sendController.text =
ref.read(exchangeFormStateProvider).fromAmountString; ref.read(exchangeFormStateProvider).fromAmountString;
@ -745,8 +739,6 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
(previous, String next) { (previous, String next) {
if (!_sendFocusNode.hasFocus) { if (!_sendFocusNode.hasFocus) {
_sendController.text = next; _sendController.text = next;
//todo: check if print needed
// debugPrint("SEND AMOUNT LISTENER ACTIVATED");
if (_swapLock) { if (_swapLock) {
_receiveController.text = isEstimated _receiveController.text = isEstimated
? ref.read(exchangeFormStateProvider).toAmountString.isEmpty ? ref.read(exchangeFormStateProvider).toAmountString.isEmpty
@ -884,10 +876,9 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
"", "",
ticker: ref.watch( ticker: ref.watch(
exchangeFormStateProvider.select((value) => value.toTicker)), exchangeFormStateProvider.select((value) => value.toTicker)),
readOnly: (rateType) == ExchangeRateType.estimated, readOnly: (rateType) == ExchangeRateType.estimated &&
// || ref.watch(exchangeProvider).name ==
// ref.watch(exchangeProvider).name == ChangeNowExchange.exchangeName,
// SimpleSwapExchange.exchangeName,
), ),
if (ref if (ref
.watch( .watch(