reduce API calls

This commit is contained in:
Serhii 2024-05-08 16:28:28 +03:00
parent 97877c4c7f
commit d8c1e14020
3 changed files with 24 additions and 27 deletions

View file

@ -38,7 +38,7 @@ void startHistoricalRateUpdateReaction(AppStore appStore, SettingsStore settings
_onHistoricalRateUpdateDisposer?.reaction.dispose(); _onHistoricalRateUpdateDisposer?.reaction.dispose();
_onHistoricalRateUpdateDisposer = reaction((_) => settingsStore.showHistoricalFiatAmount, _onHistoricalRateUpdateDisposer = reaction((_) => settingsStore.showHistoricalFiatAmount,
(bool showHistoricalFiatAmount) async { (bool showHistoricalFiatAmount) async {
if (appStore.wallet == null || settingsStore.fiatApiMode == FiatApiMode.disabled) { if (appStore.wallet == null || settingsStore.fiatApiMode == FiatApiMode.disabled || !showHistoricalFiatAmount) {
return; return;
} }

View file

@ -112,11 +112,6 @@ void startCurrentWalletChangeReaction(
return; return;
} }
if (settingsStore.showHistoricalFiatAmount) {
await historicalRateUpdate(
appStore.wallet!, settingsStore, fiatConversionStore, transactionDescription);
}
fiatConversionStore.prices[wallet.currency] = 0; fiatConversionStore.prices[wallet.currency] = 0;
fiatConversionStore.prices[wallet.currency] = await FiatConversionService.fetchPrice( fiatConversionStore.prices[wallet.currency] = await FiatConversionService.fetchPrice(
crypto: wallet.currency, crypto: wallet.currency,

View file

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:cake_wallet/entities/fiat_api_mode.dart'; import 'package:cake_wallet/entities/fiat_api_mode.dart';
import 'package:cake_wallet/entities/transaction_description.dart'; import 'package:cake_wallet/entities/transaction_description.dart';
import 'package:cake_wallet/entities/update_haven_rate.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'; import 'fiat_historical_rate_update.dart';
ReactionDisposer? _onWalletSyncStatusChangeReaction; ReactionDisposer? _onWalletSyncStatusChangeReaction;
Timer? _debounceTimer;
void startWalletSyncStatusChangeReaction( void startWalletSyncStatusChangeReaction(
WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo> wallet, WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo> wallet,
@ -23,30 +26,29 @@ void startWalletSyncStatusChangeReaction(
SettingsStore settingsStore, SettingsStore settingsStore,
Box<TransactionDescription> transactionDescription) { Box<TransactionDescription> transactionDescription) {
_onWalletSyncStatusChangeReaction?.reaction.dispose(); _onWalletSyncStatusChangeReaction?.reaction.dispose();
_onWalletSyncStatusChangeReaction = reaction((_) => wallet.syncStatus, (SyncStatus status) async { _onWalletSyncStatusChangeReaction = reaction((_) => wallet.syncStatus, (SyncStatus status) async {
try { if (status is ConnectedSyncStatus) {
if (status is ConnectedSyncStatus) { await wallet.startSync();
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) { if (status is SyncedSyncStatus &&
await updateHavenRate(fiatConversionStore); (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());
} }
}); });
} }