diff --git a/BlueApp.js b/BlueApp.js index 5a9a03aa7..a534d8904 100644 --- a/BlueApp.js +++ b/BlueApp.js @@ -26,6 +26,31 @@ async function startAndDecrypt(retry) { console.log('loaded from disk'); EV(EV.enum.WALLETS_COUNT_CHANGED); EV(EV.enum.TRANSACTIONS_COUNT_CHANGED); + // now, lets try to fetch balance and txs for first wallet if it is time for it + let hadToRefresh = false; + let noErr = true; + try { + let wallets = BlueApp.getWallets(); + if (wallets && wallets[0] && wallets[0].timeToRefresh()) { + console.log('time to refresh wallet #0'); + let oldBalance = wallets[0].getBalance(); + await wallets[0].fetchBalance(); + if (oldBalance !== wallets.getBalance()) { + // balance changed, thus txs too + await wallets[0].fetchTransactions(); + hadToRefresh = true; + EV(EV.enum.WALLETS_COUNT_CHANGED); + EV(EV.enum.TRANSACTIONS_COUNT_CHANGED); + } + } // end of timeToRefresh + } catch (Err) { + noErr = false; + console.warn(Err); + } + + if (hadToRefresh && noErr) { + await BlueApp.saveToDisk(); // caching + } } if (!success && password) { diff --git a/class/legacy-wallet.js b/class/legacy-wallet.js index aa85b673b..91b358c41 100644 --- a/class/legacy-wallet.js +++ b/class/legacy-wallet.js @@ -20,6 +20,12 @@ export class LegacyWallet extends AbstractWallet { this._lastBalanceFetch = 0; } + timeToRefresh() { + if (+new Date() - this._lastBalanceFetch >= 60 * 1000) { + return true; + } + } + generate() { function myRng(c) { let buf = Buffer.alloc(c); diff --git a/screen/wallets/list.js b/screen/wallets/list.js index 4f2020d7e..65fcb1081 100644 --- a/screen/wallets/list.js +++ b/screen/wallets/list.js @@ -133,6 +133,42 @@ export default class WalletsList extends Component { 50, ); // just to animate it, no real function } + + // now, lets try to fetch balance and txs for this wallet in case it has changed + this.lazyRefreshWallet(index); + } + + /** + * Decides whether wallet with such index shoud be refreshed, + * refreshes if yes and redraws the screen + * @param index {Integer} Index of the wallet. + * @return {Promise.} + */ + async lazyRefreshWallet(index) { + /** @type {Array.} wallets */ + let wallets = BlueApp.getWallets(); + let oldBalance = wallets[index].getBalance(); + let noErr = true; + + try { + if (wallets && wallets[index] && wallets[index].timeToRefresh()) { + console.log('snapped to, and now its time to refresh wallet #', index); + await wallets[index].fetchBalance(); + if (oldBalance !== wallets[index].getBalance()) { + // balance changed, thus txs too + await wallets[index].fetchTransactions(); + this.refreshFunction(); + } + } + } catch (Err) { + noErr = false; + console.warn(Err); + } + + if (noErr && oldBalance !== wallets[index].getBalance()) { + // so we DID refresh + await BlueApp.saveToDisk(); // caching + } } render() {