From adb57c1a5e1895f4ffaa9caae58bd2f1deec2e6c Mon Sep 17 00:00:00 2001 From: julian Date: Thu, 9 Mar 2023 09:46:48 -0600 Subject: [PATCH] move to timer base fee updates instead of focus listener activated --- lib/pages/send_view/send_view.dart | 113 +++++++++++++++++++---------- 1 file changed, 73 insertions(+), 40 deletions(-) diff --git a/lib/pages/send_view/send_view.dart b/lib/pages/send_view/send_view.dart index d6dd4b0c1..d7f23f252 100644 --- a/lib/pages/send_view/send_view.dart +++ b/lib/pages/send_view/send_view.dart @@ -146,19 +146,47 @@ class _SendViewState extends ConsumerState { _updatePreviewButtonState(_address, _amountToSend); - // if (_amountToSend == null) { - // setState(() { - // _calculateFeesFuture = calculateFees(0); - // }); - // } else { - // setState(() { - // _calculateFeesFuture = - // calculateFees(Format.decimalAmountToSatoshis(_amountToSend!)); - // }); - // } + _cryptoAmountChangedFeeUpdateTimer?.cancel(); + _cryptoAmountChangedFeeUpdateTimer = Timer(updateFeesTimerDuration, () { + if (coin != Coin.epicCash && !_baseFocus.hasFocus) { + setState(() { + _calculateFeesFuture = calculateFees( + _amountToSend == null + ? 0 + : Format.decimalAmountToSatoshis( + _amountToSend!, + coin, + ), + ); + }); + } + }); } } + final updateFeesTimerDuration = const Duration(milliseconds: 500); + + Timer? _cryptoAmountChangedFeeUpdateTimer; + Timer? _baseAmountChangedFeeUpdateTimer; + + void _baseAmountChanged() { + _baseAmountChangedFeeUpdateTimer?.cancel(); + _baseAmountChangedFeeUpdateTimer = Timer(updateFeesTimerDuration, () { + if (coin != Coin.epicCash && !_cryptoFocus.hasFocus) { + setState(() { + _calculateFeesFuture = calculateFees( + _amountToSend == null + ? 0 + : Format.decimalAmountToSatoshis( + _amountToSend!, + coin, + ), + ); + }); + } + }); + } + int _currentFee = 0; void _setCurrentFee(String fee, bool shouldSetState) { @@ -538,6 +566,7 @@ class _SendViewState extends ConsumerState { onCryptoAmountChanged = _cryptoAmountChanged; cryptoAmountController.addListener(onCryptoAmountChanged); + baseAmountController.addListener(_baseAmountChanged); if (_data != null) { if (_data!.amount != null) { @@ -553,43 +582,47 @@ class _SendViewState extends ConsumerState { noteController.text = "PayNym send"; } - if (coin != Coin.epicCash) { - _cryptoFocus.addListener(() { - if (!_cryptoFocus.hasFocus && !_baseFocus.hasFocus) { - if (_amountToSend == null) { - setState(() { - _calculateFeesFuture = calculateFees(0); - }); - } else { - setState(() { - _calculateFeesFuture = calculateFees( - Format.decimalAmountToSatoshis(_amountToSend!, coin)); - }); - } - } - }); + // if (coin != Coin.epicCash) { + // _cryptoFocus.addListener(() { + // if (!_cryptoFocus.hasFocus && !_baseFocus.hasFocus) { + // if (_amountToSend == null) { + // setState(() { + // _calculateFeesFuture = calculateFees(0); + // }); + // } else { + // setState(() { + // _calculateFeesFuture = calculateFees( + // Format.decimalAmountToSatoshis(_amountToSend!, coin)); + // }); + // } + // } + // }); - _baseFocus.addListener(() { - if (!_cryptoFocus.hasFocus && !_baseFocus.hasFocus) { - if (_amountToSend == null) { - setState(() { - _calculateFeesFuture = calculateFees(0); - }); - } else { - setState(() { - _calculateFeesFuture = calculateFees( - Format.decimalAmountToSatoshis(_amountToSend!, coin)); - }); - } - } - }); - } + // _baseFocus.addListener(() { + // if (!_cryptoFocus.hasFocus && !_baseFocus.hasFocus) { + // if (_amountToSend == null) { + // setState(() { + // _calculateFeesFuture = calculateFees(0); + // }); + // } else { + // setState(() { + // _calculateFeesFuture = calculateFees( + // Format.decimalAmountToSatoshis(_amountToSend!, coin)); + // }); + // } + // } + // }); + // } super.initState(); } @override void dispose() { + _cryptoAmountChangedFeeUpdateTimer?.cancel(); + _baseAmountChangedFeeUpdateTimer?.cancel(); + cryptoAmountController.removeListener(onCryptoAmountChanged); + baseAmountController.removeListener(_baseAmountChanged); sendToController.dispose(); cryptoAmountController.dispose();