diff --git a/src/js/controllers/walletDetails.js b/src/js/controllers/walletDetails.js index fc45d70f6..d0afee8ca 100644 --- a/src/js/controllers/walletDetails.js +++ b/src/js/controllers/walletDetails.js @@ -13,6 +13,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun gettingCachedHistory: true, gettingInitialHistory: true, updatingTxHistory: false, + fetchedAllTxHistory: false, //updateTxHistoryError: false updateTxHistoryFailed: false }; @@ -108,6 +109,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun $scope.updatingStatus = true; $scope.updateStatusError = null; $scope.walletNotRegistered = false; + $scope.vm.fetchedAllTxHistory = false; walletService.getStatus($scope.wallet, { force: !!force, @@ -198,44 +200,6 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun }); }; - - var updateTxHistory = function(cb) { - if (!cb) cb = function() {}; - $scope.vm.updateTxHistoryFailed = false; - $scope.updatingTxHistoryProgress = 0; - - feeService.getFeeLevels($scope.wallet.coin, function(err, levels) { - walletService.getTxHistory($scope.wallet, { - feeLevels: levels - }, function(err, txHistory) { - $scope.vm.gettingInitialHistory = false; - if (err) { - $scope.txHistory = null; - $scope.vm.updateTxHistoryFailed = true; - return; - } - - applyCurrencyAliases(txHistory); - - var config = configService.getSync(); - var fiatCode = config.wallet.settings.alternativeIsoCode; - lodash.each(txHistory, function(t) { - var r = rateService.toFiat(t.amount, fiatCode, $scope.wallet.coin); - t.alternativeAmountStr = r.toFixed(2) + ' ' + fiatCode; - }); - console.log('pagination Got tx history old way'); - completeTxHistory = txHistory; - - //$scope.showHistory(); - $timeout(function() { - $scope.$apply(); - }); - return cb(); - }); - }); - }; - - function applyCurrencyAliases(txHistory) { var defaults = configService.getDefaults(); var configCache = configService.getSync(); @@ -290,7 +254,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun console.log('pagination fetchAndShowTxHistory() getLatest:', getLatest, ', flushCacheOnNew:', flushCacheOnNew); $scope.vm.updatingTxHistory = true; - walletHistoryService.updateLocalTxHistoryByPage($scope.wallet, getLatest, flushCacheOnNew, function onUpdateLocalTxHistoryByPage(err, txHistory) { + walletHistoryService.updateLocalTxHistoryByPage($scope.wallet, getLatest, flushCacheOnNew, function onUpdateLocalTxHistoryByPage(err, txHistory, fetchedAllTransactions) { console.log('pagination returned'); $scope.vm.gettingInitialHistory = false; $scope.vm.updatingTxHistory = false; @@ -301,6 +265,12 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun $scope.vm.updateTxHistoryFailed = true; return; } + + if (fetchedAllTransactions) { + console.log("All transactions seem to be fetched.."); + $scope.vm.fetchedAllTxHistory = true; + } + console.log('pagination txs returned in history: ' + txHistory.length); formatTxHistoryForDisplay(txHistory); @@ -314,7 +284,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun function showHistory(showAll) { if (completeTxHistory) { $scope.txHistory = showAll ? completeTxHistory : completeTxHistory.slice(0, (currentTxHistoryDisplayPage + 1) * DISPLAY_PAGE_SIZE); - $scope.vm.allowInfiniteScroll = completeTxHistory.length > $scope.txHistory.length || !$scope.vm.gettingInitialHistory; + $scope.vm.allowInfiniteScroll = !$scope.vm.fetchedAllTxHistory;//(completeTxHistory.length > $scope.txHistory.length || !$scope.vm.gettingInitialHistory) || (!$scope.vm.gettingInitialHistory && !$scope.vm.fetchedAllTxHistory); console.log('pagination Showing txs: ', $scope.txHistory.length); } else { $scope.vm.allowInfiniteScroll = false; @@ -534,7 +504,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun updateTxHistoryFromCachedData(); $scope.updateAll(false, true); refreshAmountSection(); - //refreshInterval = $interval($scope.onRefresh, 10 * 1000); + refreshInterval = $interval($scope.onRefresh, 10 * 1000); //refreshInterval = $interval($scope.onRefresh, 120 * 1000); // For testing }); diff --git a/src/js/services/wallet-history.service.js b/src/js/services/wallet-history.service.js index 8d6345e1d..1f9e03152 100644 --- a/src/js/services/wallet-history.service.js +++ b/src/js/services/wallet-history.service.js @@ -18,12 +18,46 @@ var SAFE_CONFIRMATIONS = 6; + var allTransactionsFetched = false; var service = { getCachedTxHistory: getCachedTxHistory, updateLocalTxHistoryByPage: updateLocalTxHistoryByPage, }; return service; +/* + function hasAllTransactionsFetched(walletId, cachedTxs, newTxs) { + var cachedTxIds = {}; + cachedTxs.forEach(function forCachedTx(tx){ + cachedTxIds[tx.txid] = true; + }); + + var someTransactionWereNew = false; + var overlappingTxsCount = 0; + + newTxs.forEach(function forNewTx(tx){ + if (cachedTxIds[tx.txid]) { + overlappingTxsCount++; + } else { + someTransactionWereNew = true; + } + }); + + console.log('pagination Overlapping transactions:', overlappingTxsCount); + if (overlappingTxsCount >= MIN_KNOWN_TX_OVERLAP) { // We are good + if (!someTransactionWereNew && overlappingTxsCount === newTxs.length) { + console.log("We probably have all of the transactions fetched!!") + return true; + } + } else { + console.log("Something went wrong") + return true; // Something went wrong, so stop fetching.. + } + return false; + + } +*/ + function addEarlyTransactions(walletId, cachedTxs, newTxs) { var cachedTxIds = {}; @@ -48,6 +82,9 @@ if (someTransactionsWereNew) { console.log('pagination someTransactionsWereNew'); saveTxHistory(walletId, cachedTxs); + } else if (overlappingTxsCount === newTxs.length) { + console.log('We probably have all transactions now'); + allTransactionsFetched = true; } return cachedTxs; } else { @@ -225,7 +262,7 @@ } if (fetchedTxs.length === 0) { - return cb(null, cachedTxs); + return cb(null, cachedTxs, true /*fetchedAllTransactions*/); } var txs = []; @@ -233,11 +270,12 @@ txs = addLatestTransactions(wallet.id, cachedTxs, fetchedTxs); } else { txs = addEarlyTransactions(wallet.id, cachedTxs, fetchedTxs); + return cb(null, txs, allTransactionsFetched/*, hasAllTransactionsFetched(wallet.id, cachedTxs, fetchedTxs)*/); } return cb(null, txs); }); - + }); } }