From d8c1e14020dfead3ffa04d0eff744893ab049e4f Mon Sep 17 00:00:00 2001 From: Serhii Date: Wed, 8 May 2024 16:28:28 +0300 Subject: [PATCH] reduce API calls --- .../on_current_fiat_api_mode_change.dart | 2 +- lib/reactions/on_current_wallet_change.dart | 5 --- .../on_wallet_sync_status_change.dart | 44 ++++++++++--------- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/reactions/on_current_fiat_api_mode_change.dart b/lib/reactions/on_current_fiat_api_mode_change.dart index e13201121..968d3b6db 100644 --- a/lib/reactions/on_current_fiat_api_mode_change.dart +++ b/lib/reactions/on_current_fiat_api_mode_change.dart @@ -38,7 +38,7 @@ void startHistoricalRateUpdateReaction(AppStore appStore, SettingsStore settings _onHistoricalRateUpdateDisposer?.reaction.dispose(); _onHistoricalRateUpdateDisposer = reaction((_) => settingsStore.showHistoricalFiatAmount, (bool showHistoricalFiatAmount) async { - if (appStore.wallet == null || settingsStore.fiatApiMode == FiatApiMode.disabled) { + if (appStore.wallet == null || settingsStore.fiatApiMode == FiatApiMode.disabled || !showHistoricalFiatAmount) { return; } diff --git a/lib/reactions/on_current_wallet_change.dart b/lib/reactions/on_current_wallet_change.dart index 8825eb759..f5dd64de7 100644 --- a/lib/reactions/on_current_wallet_change.dart +++ b/lib/reactions/on_current_wallet_change.dart @@ -112,11 +112,6 @@ void startCurrentWalletChangeReaction( return; } - if (settingsStore.showHistoricalFiatAmount) { - await historicalRateUpdate( - appStore.wallet!, settingsStore, fiatConversionStore, transactionDescription); - } - fiatConversionStore.prices[wallet.currency] = 0; fiatConversionStore.prices[wallet.currency] = await FiatConversionService.fetchPrice( crypto: wallet.currency, diff --git a/lib/reactions/on_wallet_sync_status_change.dart b/lib/reactions/on_wallet_sync_status_change.dart index 67f887053..9ab65bfa3 100644 --- a/lib/reactions/on_wallet_sync_status_change.dart +++ b/lib/reactions/on_wallet_sync_status_change.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:cake_wallet/entities/fiat_api_mode.dart'; import 'package:cake_wallet/entities/transaction_description.dart'; import 'package:cake_wallet/entities/update_haven_rate.dart'; @@ -16,6 +18,7 @@ import 'package:wakelock_plus/wakelock_plus.dart'; import 'fiat_historical_rate_update.dart'; ReactionDisposer? _onWalletSyncStatusChangeReaction; +Timer? _debounceTimer; void startWalletSyncStatusChangeReaction( WalletBase, TransactionInfo> wallet, @@ -23,30 +26,29 @@ void startWalletSyncStatusChangeReaction( SettingsStore settingsStore, Box transactionDescription) { _onWalletSyncStatusChangeReaction?.reaction.dispose(); + _onWalletSyncStatusChangeReaction = reaction((_) => wallet.syncStatus, (SyncStatus status) async { - try { - if (status is ConnectedSyncStatus) { - await wallet.startSync(); + if (status is ConnectedSyncStatus) { + await wallet.startSync(); + if (wallet.type == WalletType.haven) { + await updateHavenRate(fiatConversionStore); + } + } + if (status is SyncingSyncStatus) { + await WakelockPlus.enable(); + } + if (status is SyncedSyncStatus || status is FailedSyncStatus) { + await WakelockPlus.disable(); - if (wallet.type == WalletType.haven) { - await updateHavenRate(fiatConversionStore); - } + if (status is SyncedSyncStatus && + (settingsStore.fiatApiMode != FiatApiMode.disabled || + settingsStore.showHistoricalFiatAmount)) { + _debounceTimer?.cancel(); + _debounceTimer = Timer(Duration(milliseconds: 200), () async { + await historicalRateUpdate( + wallet, settingsStore, fiatConversionStore, transactionDescription); + }); } - if (status is SyncingSyncStatus) { - await WakelockPlus.enable(); - } - if (status is SyncedSyncStatus || status is FailedSyncStatus) { - await WakelockPlus.disable(); - - if (settingsStore.fiatApiMode != FiatApiMode.disabled) { - if (settingsStore.showHistoricalFiatAmount) { - await historicalRateUpdate( - wallet, settingsStore, fiatConversionStore, transactionDescription); - } - } - } - } catch (e) { - print(e.toString()); } }); }