notifications cache and autorefresh

This commit is contained in:
Matias Alejo Garcia 2016-08-31 18:12:28 -03:00
commit e801fc2626
No known key found for this signature in database
GPG key ID: 02470DB551277AB3
5 changed files with 152 additions and 132 deletions

View file

@ -8,7 +8,7 @@
<div class="card">
<div class="item item-divider item-icon-right">
Recent Activity
<i class="icon ion-ios-arrow-right" ui-sref="activity" ng-show="notificationsMore"></i>
<i class="icon ion-ios-arrow-right" ui-sref="activity"></i>
</div>
<div ng-if="fetchingNotifications" class="item text-center">
<ion-spinner icon="lines"></ion-spinner>

View file

@ -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);
});
}
});

View file

@ -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);
})
});
};

View file

@ -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));
};
});
});

View file

@ -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); {