From 303b3aa354a993fa05f73850176023b305288c21 Mon Sep 17 00:00:00 2001 From: m2049r Date: Sat, 7 Aug 2021 11:58:54 +0200 Subject: [PATCH] dont update on every single block (#776) --- .../xmrwallet/service/WalletService.java | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/m2049r/xmrwallet/service/WalletService.java b/app/src/main/java/com/m2049r/xmrwallet/service/WalletService.java index b048755..876c944 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/service/WalletService.java +++ b/app/src/main/java/com/m2049r/xmrwallet/service/WalletService.java @@ -108,28 +108,33 @@ public class WalletService extends Service { Timber.d("unconfirmedMoneyReceived() %d @ %s", amount, txId); } - int lastTxCount = 0; + private long lastBlockTime = 0; + private int lastTxCount = 0; public void newBlock(long height) { final Wallet wallet = getWallet(); if (wallet == null) throw new IllegalStateException("No wallet!"); - Timber.d("newBlock() @ %d with observer %s", height, observer); - if (observer != null) { - boolean fullRefresh = false; - updateDaemonState(wallet, wallet.isSynchronized() ? height : 0); - if (!wallet.isSynchronized()) { - updated = true; - // we want to see our transactions as they come in - wallet.getHistory().refresh(); - int txCount = wallet.getHistory().getCount(); - if (txCount > lastTxCount) { - // update the transaction list only if we have more than before - lastTxCount = txCount; - fullRefresh = true; + // don't flood with an update for every block ... + if (lastBlockTime < System.currentTimeMillis() - 2000) { + lastBlockTime = System.currentTimeMillis(); + Timber.d("newBlock() @ %d with observer %s", height, observer); + if (observer != null) { + boolean fullRefresh = false; + updateDaemonState(wallet, wallet.isSynchronized() ? height : 0); + if (!wallet.isSynchronized()) { + updated = true; + // we want to see our transactions as they come in + wallet.getHistory().refresh(); + int txCount = wallet.getHistory().getCount(); + if (txCount > lastTxCount) { + // update the transaction list only if we have more than before + lastTxCount = txCount; + fullRefresh = true; + } } + if (observer != null) + observer.onRefreshed(wallet, fullRefresh); } - if (observer != null) - observer.onRefreshed(wallet, fullRefresh); } }