From e801fc26268ac28166a5bea4c61b30d7059fa058 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 31 Aug 2016 18:12:28 -0300 Subject: [PATCH] notifications cache and autorefresh --- public/views/tab-home.html | 2 +- src/js/controllers/activity.js | 18 +--- src/js/controllers/tab-home.js | 64 +++++++------ src/js/services/profileService.js | 146 +++++++++++++++++++++++------- src/js/services/walletService.js | 58 ------------ 5 files changed, 154 insertions(+), 134 deletions(-) diff --git a/public/views/tab-home.html b/public/views/tab-home.html index 402f9d6f3..ef9582374 100644 --- a/public/views/tab-home.html +++ b/public/views/tab-home.html @@ -8,7 +8,7 @@
Recent Activity - +
diff --git a/src/js/controllers/activity.js b/src/js/controllers/activity.js index 89f06fafe..6205357c5 100644 --- a/src/js/controllers/activity.js +++ b/src/js/controllers/activity.js @@ -4,17 +4,6 @@ angular.module('copayApp.controllers').controller('activityController', function($rootScope, $timeout, $scope, $state, lodash, profileService, walletService, configService, txFormatService, $ionicModal, $log, platformInfo) { var self = this; - var setNotifications = function(notifications) { - var n = walletService.processNotifications(notifications); - - $scope.notifications = n; - $timeout(function() { - $scope.$apply(); - }, 1); - }; - - - $scope.init = function() { $scope.fetchingNotifications = true; @@ -24,7 +13,10 @@ angular.module('copayApp.controllers').controller('activityController', return; } $scope.fetchingNotifications = false; - setNotifications(n); - }) + $scope.notifications = n; + $timeout(function() { + $scope.$apply(); + }, 1); + }); } }); diff --git a/src/js/controllers/tab-home.js b/src/js/controllers/tab-home.js index bcaeb1d75..1421b60f1 100644 --- a/src/js/controllers/tab-home.js +++ b/src/js/controllers/tab-home.js @@ -4,9 +4,7 @@ angular.module('copayApp.controllers').controller('tabHomeController', function($rootScope, $timeout, $scope, $state, lodash, profileService, walletService, configService, txFormatService, $ionicModal, $log, platformInfo, storageService) { var setNotifications = function(notifications) { - var n = walletService.processNotifications(notifications, 3); - $scope.notifications = n; - $scope.notificationsMore = notifications.length > 3 ? notifications.length - 3 : null; + $scope.notifications = notifications; $timeout(function() { $scope.$apply(); }, 1); @@ -16,37 +14,35 @@ angular.module('copayApp.controllers').controller('tabHomeController', $scope.wallets = profileService.getWallets(); if (lodash.isEmpty($scope.wallets)) return; - $timeout(function() { - var i = $scope.wallets.length; - var j = 0; - var timeSpan = 60 * 60 * 24 * 7; - var notifications = []; + var i = $scope.wallets.length; + var j = 0; + var timeSpan = 60 * 60 * 24 * 7; + var notifications = []; - lodash.each($scope.wallets, function(wallet) { + lodash.each($scope.wallets, function(wallet) { - walletService.getStatus(wallet, {}, function(err, status) { - if (err) { - console.log('[tab-home.js.35:err:]', $log.error(err)); //TODO - return; - } - wallet.status = status; - }); - - - }); - - $scope.fetchingNotifications = true; - profileService.getNotifications(3, function(err, n) { + walletService.getStatus(wallet, {}, function(err, status) { if (err) { console.log('[tab-home.js.35:err:]', $log.error(err)); //TODO return; } - $scope.fetchingNotifications = false; - setNotifications(n); - }) + wallet.status = status; + }); - $scope.$digest(); - }, 100); + + }); + + $scope.fetchingNotifications = true; + profileService.getNotifications({ + limit: 3 + }, function(err, n) { + if (err) { + console.log('[tab-home.js.35:err:]', $log.error(err)); //TODO + return; + } + $scope.fetchingNotifications = false; + setNotifications(n); + }) }; $scope.updateWallet = function(wallet) { @@ -57,9 +53,17 @@ angular.module('copayApp.controllers').controller('tabHomeController', return; } wallet.status = status; - $timeout(function() { - $scope.$apply(); - }, 1); + + profileService.getNotifications({ + limit: 3 + }, function(err, n) { + console.log('[tab-home.js.57]', n); //TODO + if (err) { + console.log('[tab-home.js.35:err:]', $log.error(err)); //TODO + return; + } + setNotifications(n); + }) }); }; diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index 44eff25a9..0efb95b6a 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -1,6 +1,6 @@ 'use strict'; angular.module('copayApp.services') - .factory('profileService', function profileServiceFactory($rootScope, $timeout, $filter, $log, sjcl, lodash, storageService, bwcService, configService, pushNotificationsService, gettext, gettextCatalog, bwcError, uxLanguage, bitcore, platformInfo, $ionicHistory) { + .factory('profileService', function profileServiceFactory($rootScope, $timeout, $filter, $log, sjcl, lodash, storageService, bwcService, configService, pushNotificationsService, gettext, gettextCatalog, bwcError, uxLanguage, platformInfo, $ionicHistory, txFormatService, $state) { var isChromeApp = platformInfo.isChromeApp; @@ -119,6 +119,9 @@ angular.module('copayApp.services') if (wallet.completeHistory) wallet.completeHistory.isValid = false; + if (wallet.cachedActivity) + wallet.cachedActivity.isValid = false; + $rootScope.$emit('bwsEvent', wallet.id, n.type, n); }); @@ -736,6 +739,7 @@ angular.module('copayApp.services') } else {} return lodash.sortBy(ret, [ + function(x) { return x.isComplete(); }, 'createdOn' @@ -747,14 +751,11 @@ angular.module('copayApp.services') storageService.setHideBalanceFlag(walletId, root.wallet[walletId].balanceHidden.toString(), cb); }; - root.getNotifications = function(limit, cb) { + root.getNotifications = function(opts, cb) { + opts = opts || {}; var TIME_STAMP = 60 * 60 * 24 * 7; - - var opts = { - timeSpan: TIME_STAMP, - includeOwn: true, - }; + var MAX = 100; var ignored = { 'NewBlock': 1, @@ -771,36 +772,117 @@ angular.module('copayApp.services') var l = w.length, j = 0, notifications = []; + + + function isActivityCached(wallet) { + return wallet.cachedActivity && wallet.cachedActivity.isValid; + }; + + + function getNotifications(wallet, cb2) { + if (isActivityCached(wallet) && !opts.force) return cb2(); + + wallet.getNotifications({ + timeSpan: TIME_STAMP, + includeOwn: true, + }, function(err, n) { + if (err) return cb2(err); + + wallet.cachedActivity = { + n: n.slice(-MAX), + isValid: true, + }; + + return cb2(); + }); + }; + + function process(notifications) { + if (!notifications) return []; + + var shown = lodash.sortBy(notifications, 'createdOn').reverse(); + + shown = shown.splice(0, opts.limit || MAX); + + lodash.each(shown, function(x) { + x.txpId = x.data ? x.data.txProposalId : null; + x.txid = x.data ? x.data.txid : null; + x.types = [x.type]; + + if (x.data && x.data.amount) + x.amountStr = txFormatService.formatAmountStr(x.data.amount); + + x.action = function() { + // TODO? + $state.go('wallet.details', { + walletId: x.walletId, + txpId: x.txpId, + txid: x.txid, + }); + }; + }); + + // condense + var finale = [], + prev; + + + lodash.each(shown, function(x) { + if (prev && prev.walletId === x.walletId && prev.txpId && prev.txpId === x.txpId && prev.creatorId && prev.creatorId === x.creatorId) { + prev.types.push(x.type); + prev.data = lodash.assign(prev.data, x.data); + prev.txid = prev.txid || x.txid; + prev.amountStr = prev.amountStr || x.amountStr; + prev.creatorName = prev.creatorName || x.creatorName; + } else { + finale.push(x); + prev = x; + } + }); + + // messages... + + var u = bwcService.getUtils(); + lodash.each(finale, function(x) { + if (x.data && x.data.message && x.wallet && x.wallet.credentials.sharedEncryptingKey) { + // TODO TODO TODO => BWC + x.message = u.decryptMessage(x.data.message, x.wallet.credentials.sharedEncryptingKey); + } + }); + + return finale; + }; + lodash.each(w, function(wallet) { - wallet.getNotifications(opts, function(err, n) { + getNotifications(wallet, function(err) { j++; if (err) { - console.log('[tab-home.js.35:err:]', $log.error(err)); //TODO - return; - } - - n = lodash.filter(n, function(x) { - return !ignored[x.type]; - }); - - var idToName = {}; - if (wallet.cachedStatus) { - lodash.each(wallet.cachedStatus.wallet.copayers, function(c) { - idToName[c.id] = c.name; + $log.warn('Error updating notifications:' + err); + } else { + var n = lodash.filter(wallet.cachedActivity.n, function(x) { + return !ignored[x.type]; }); + + var idToName = {}; + if (wallet.cachedStatus) { + lodash.each(wallet.cachedStatus.wallet.copayers, function(c) { + idToName[c.id] = c.name; + }); + } + + lodash.each(n, function(x) { + x.wallet = wallet; + if (x.creatorId && wallet.cachedStatus) { + x.creatorName = idToName[x.creatorId]; + }; + }); + + notifications.push(n); } - - lodash.each(n, function(x) { - x.wallet = wallet; - if (x.creatorId && wallet.cachedStatus) { - x.creatorName = idToName[x.creatorId]; - }; - }); - - notifications.push(n); - if (++j == l) { - notifications = lodash.sortBy(notifications,'createdOn'); - return cb(null, lodash.compact(lodash.flatten(notifications))); + if (j == l) { + notifications = lodash.sortBy(notifications, 'createdOn'); + notifications = lodash.compact(lodash.flatten(notifications)).slice(0,MAX); + return cb(null, process(notifications)); }; }); }); diff --git a/src/js/services/walletService.js b/src/js/services/walletService.js index 435ead627..fd5a5bfa2 100644 --- a/src/js/services/walletService.js +++ b/src/js/services/walletService.js @@ -1009,64 +1009,6 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim }); }; - root.processNotifications = function(notifications, limit) { - if (!notifications) return []; - - var shown = lodash.sortBy(notifications, 'createdOn').reverse(); - - if (limit) - shown = shown.splice(0, limit); - - lodash.each(shown, function(x) { - x.txpId = x.data ? x.data.txProposalId : null; - x.txid = x.data ? x.data.txid : null; - x.types = [x.type]; - - if (x.data && x.data.amount) - x.amountStr = txFormatService.formatAmountStr(x.data.amount); - - x.action = function() { - // TODO? - $state.go('wallet.details', { - walletId: x.walletId, - txpId: x.txpId, - txid: x.txid, - }); - }; - }); - - // condense - var finale = [], - prev; - - - lodash.each(shown, function(x) { - if (prev && prev.walletId === x.walletId && prev.txpId && prev.txpId === x.txpId && prev.creatorId && prev.creatorId === x.creatorId) { - prev.types.push(x.type); - prev.data = lodash.assign(prev.data, x.data); - prev.txid = prev.txid || x.txid; - prev.amountStr = prev.amountStr || x.amountStr; - prev.creatorName = prev.creatorName || x.creatorName; - } else { - finale.push(x); - prev = x; - } - }); - - // messages... - - var u = bwcService.getUtils(); - lodash.each(finale, function(x) { - if (x.data && x.data.message && x.wallet && x.wallet.credentials.sharedEncryptingKey) { - // TODO TODO TODO => BWC - x.message = u.decryptMessage(x.data.message, x.wallet.credentials.sharedEncryptingKey); - } - }); - - return finale; - }; - - root.setTouchId = function(wallet, enabled, cb) { fingerprintService.check(wallet, function(err) { if (err) return cb(err); {