refactor on local transactions methods

This commit is contained in:
Javier 2015-10-23 13:26:59 -03:00
commit 0d022523b0
2 changed files with 37 additions and 42 deletions

View file

@ -455,13 +455,8 @@
</div> </div>
</div> </div>
</div> </div>
<div class="row m20t text-center" ng-show="index.txHistoryPaging || index.updatingTxHistory"> <div class="row m20t text-center" ng-show="index.updatingTxHistory">
<div class="columns large-12 medium-12 small-12"> <div class="columns large-12 medium-12 small-12">
<button class="outline light-gray round small expand m0"
ng-click="index.updateTxHistory(index.skipHistory)"
ng-if="!index.updatingTxHistory">
<span translate>More</span> <i class="icon-arrow-down4 ng-scope"></i>
</button>
<div class="spinner" ng-if="index.updatingTxHistory"> <div class="spinner" ng-if="index.updatingTxHistory">
<div class="rect1"></div> <div class="rect1"></div>
<div class="rect2"></div> <div class="rect2"></div>

View file

@ -2,7 +2,7 @@
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, nodeWebkit, addonManager, feeService, isChromeApp, bwsError, txFormatService, uxLanguage, $state, glideraService, isMobile) { angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, nodeWebkit, addonManager, feeService, isChromeApp, bwsError, txFormatService, uxLanguage, $state, glideraService, isMobile) {
var self = this; var self = this;
var CONFIRMATIONS = 12; var SOFT_CONFIRMATION_LIMIT = 12;
self.isCordova = isCordova; self.isCordova = isCordova;
self.isChromeApp = isChromeApp; self.isChromeApp = isChromeApp;
self.isSafari = isMobile.Safari(); self.isSafari = isMobile.Safari();
@ -85,7 +85,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.txHistory = []; self.txHistory = [];
self.txHistoryUnique = {}; self.txHistoryUnique = {};
self.balanceByAddress = null; self.balanceByAddress = null;
self.txHistoryPaging = false;
self.pendingTxProposalsCountForUs = null; self.pendingTxProposalsCountForUs = null;
self.setSpendUnconfirmed(); self.setSpendUnconfirmed();
@ -647,18 +646,17 @@ angular.module('copayApp.controllers').controller('indexController', function($r
} }
var step = 6; var step = 6;
var unique = {}; var unique = {};
function getHistory(skip, cb) { function getHistory(cb) {
storageService.getTxHistory(c.walletId, function(err, txs) { storageService.getTxHistory(c.walletId, function(err, txs) {
if (err) return cb(err); if (err) return cb(err);
var txsFromLocal; var txsFromLocal = [];
try { try {
txsFromLocal = JSON.parse(txs); txsFromLocal = JSON.parse(txs);
} catch (ex) { } catch (ex) {
return cb(ex); $log.warn(ex);
} }
allTxs.push(txsFromLocal); allTxs.push(txsFromLocal);
@ -667,7 +665,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
} }
if (isCordova) { if (isCordova) {
$log.info('Not available on mobile'); $log.info('CSV generation not available in mobile');
return; return;
} }
var isNode = nodeWebkit.isDefined(); var isNode = nodeWebkit.isDefined();
@ -681,7 +679,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.setOngoingProcess('generatingCSV', true); self.setOngoingProcess('generatingCSV', true);
$timeout(function() { $timeout(function() {
getHistory(null, function(err, txs) { getHistory(function(err, txs) {
self.setOngoingProcess('generatingCSV', false); self.setOngoingProcess('generatingCSV', false);
if (err) { if (err) {
self.handleError(err); self.handleError(err);
@ -742,89 +740,91 @@ angular.module('copayApp.controllers').controller('indexController', function($r
return false; return false;
} }
self.removeLessThanSixConfirmations = function(txs) { self.removeSoftConfirmedTx = function(txs) {
return lodash.map(txs, function(tx) { return lodash.map(txs, function(tx) {
if (tx.confirmations >= CONFIRMATIONS) if (tx.confirmations >= SOFT_CONFIRMATION_LIMIT)
return tx; return tx;
}); });
} }
self.updateLocalTxHistory = function(cb) { self.getConfirmedTxs = function(cb) {
var fc = profileService.focusedClient; var fc = profileService.focusedClient;
var c = fc.credentials; var c = fc.credentials;
var txsToPush = [];
storageService.getTxHistory(c.walletId, function(err, txs) { storageService.getTxHistory(c.walletId, function(err, txs) {
if (err) return cb(err); if (err) return cb(err);
var localTxs; var localTxs = [];
try { try {
localTxs = JSON.parse(txs); localTxs = JSON.parse(txs);
} catch (ex) { } catch (ex) {
return cb(ex); $log.warn(ex);
} }
if (!localTxs) return cb(null, self.removeSoftConfirmedTx(localTxs));
localTxs = []; });
}
var txsFromLocal = self.removeLessThanSixConfirmations(localTxs); self.updateLocalTxHistory = function(cb) {
self.getConfirmedTxs(function(err, txsFromLocal) {
if (err) return cb(err);
var count = 0; var fc = profileService.focusedClient;
fillTxsObject(txsToPush); var c = fc.credentials;
fillTxsObject();
function fillTxsObject(txsToPush) { function fillTxsObject(txsResult, index) {
self.makeTxHistoryRequest(txsToPush, txsFromLocal, function(err, exitLoop, txsResult) { txsResult = txsResult || [];
index = index || 0;
self.makeTxHistoryRequest(txsResult, index, txsFromLocal[0], function(err, newIndex, exitLoop) {
if (err) return cb(err); if (err) return cb(err);
if (exitLoop) { if (exitLoop) {
self.txHistory = []; self.txHistory = [];
self.setTxHistory(lodash.compact(txsResult.concat(txsFromLocal))); self.setTxHistory(lodash.compact(txsResult.concat(txsFromLocal)));
storageService.setTxHistory(JSON.stringify(self.txHistory), c.walletId, function() { return storageService.setTxHistory(JSON.stringify(self.txHistory), c.walletId, function() {
return cb(null); return cb(null);
}); });
} else }
fillTxsObject(txsResult); fillTxsObject(txsResult, newIndex);
}); });
}; };
}); });
} }
self.makeTxHistoryRequest = function(txsToPush, localTx, cb) { self.makeTxHistoryRequest = function(txsResult, index, endingTx, cb) {
var fc = profileService.focusedClient; var fc = profileService.focusedClient;
var c = fc.credentials; var c = fc.credentials;
var exitLoop = false; var exitLoop = false;
fc.getTxHistory({ fc.getTxHistory({
skip: self.skipHistory, skip: index,
limit: self.limitHistory + 1 limit: self.limitHistory + 1
}, function(err, txsFromBWC) { }, function(err, txsFromBWC) {
if (err) return cb(err); if (err) return cb(err);
if (!txsFromBWC[0]) { if (!txsFromBWC[0])
exitLoop = true; exitLoop = true;
$log.debug('There is not transactions stored');
}
lodash.each(txsFromBWC, function(t) { lodash.each(txsFromBWC, function(t) {
if (!localTx[0]) txsToPush.push(t); if (!endingTx) txsResult.push(t);
else { else {
if (!self.stopSync(t, localTx[0]) && !exitLoop) { if (!self.stopSync(t, endingTx) && !exitLoop) {
txsToPush.push(t); txsResult.push(t);
} else { } else {
exitLoop = true; exitLoop = true;
} }
} }
}); });
self.skipHistory = self.skipHistory + self.limitHistory; index = index + self.limitHistory;
return cb(null, exitLoop, txsToPush); return cb(null, index, exitLoop);
}); });
} }
self.updateHistory = function() { self.updateHistory = function() {
$log.debug('Updating Transaction History'); $log.debug('Updating Transaction History');
self.skipHistory = 0;
self.txHistoryError = false; self.txHistoryError = false;
self.updatingTxHistory = true; self.updatingTxHistory = true;
self.txHistoryPaging = false;
$timeout(function() { $timeout(function() {
self.updateLocalTxHistory(function(err) { self.updateLocalTxHistory(function(err) {