Skip to content
Snippets Groups Projects
Commit 832bee2b authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

[fix] Optimize refresh of secondary wallets

parent a75ecbf5
No related branches found
No related tags found
No related merge requests found
...@@ -84,10 +84,12 @@ function WalletListController($scope, $controller, $state, $timeout, $q, $transl ...@@ -84,10 +84,12 @@ function WalletListController($scope, $controller, $state, $timeout, $q, $transl
// Override defaults // Override defaults
$scope.formData.name = undefined; $scope.formData.name = undefined;
$scope.motion = UIUtils.motion.default; $scope.motion = UIUtils.motion.default;
$scope.entered = false;
$scope.enter = function(e, state) { $scope.enter = function(e, state) {
// First enter // First enter
if ($scope.loading) { if (!$scope.entered) {
$scope.entered = true;
$scope.setParameters({ $scope.setParameters({
showDefault: true, showDefault: true,
showBalance: true showBalance: true
...@@ -105,11 +107,20 @@ function WalletListController($scope, $controller, $state, $timeout, $q, $transl ...@@ -105,11 +107,20 @@ function WalletListController($scope, $controller, $state, $timeout, $q, $transl
else { else {
// Re-add listeners // Re-add listeners
$scope.addListeners(); $scope.addListeners();
if ($scope.formData.stopped) {
$scope.loading = false;
$scope.formData.stopped = false;
$scope.formData.updatingWalletId = undefined;
$scope.updateView();
}
} }
}; };
$scope.$on('$ionicView.enter', $scope.enter); $scope.$on('$ionicView.enter', $scope.enter);
$scope.leave = function() { $scope.leave = function() {
$scope.formData.stopped = true;
$scope.formData.updatingWalletId = undefined;
$scope.loading = false;
$scope.removeListeners(); $scope.removeListeners();
}; };
$scope.$on('$ionicView.leave', $scope.leave); $scope.$on('$ionicView.leave', $scope.leave);
...@@ -120,7 +131,12 @@ function WalletListController($scope, $controller, $state, $timeout, $q, $transl ...@@ -120,7 +131,12 @@ function WalletListController($scope, $controller, $state, $timeout, $q, $transl
$scope.select = function(event, wallet) { $scope.select = function(event, wallet) {
if (event.isDefaultPrevented()) return; if (event.isDefaultPrevented()) return;
if (wallet.isDefault()) {
$state.go('app.view_wallet');
}
else {
$state.go('app.view_wallet_by_id', {id: wallet.id}); $state.go('app.view_wallet_by_id', {id: wallet.id});
}
}; };
$scope.editWallet = function(event, wallet) { $scope.editWallet = function(event, wallet) {
...@@ -449,14 +465,15 @@ function WalletListController($scope, $controller, $state, $timeout, $q, $transl ...@@ -449,14 +465,15 @@ function WalletListController($scope, $controller, $state, $timeout, $q, $transl
function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTranslations, csSettings, csCurrency, csWallet, parameters){ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTranslations, csSettings, csCurrency, csWallet, parameters){
'ngInject'; 'ngInject';
var loadWalletWaitTime = 100;
$scope.loading = true; $scope.loading = true;
$scope.wallets = null; $scope.wallets = null;
$scope.formData = { $scope.formData = {
useRelative: csSettings.data.useRelative, useRelative: csSettings.data.useRelative,
showDefault: true, showDefault: true,
showBalance: false, showBalance: false,
balance: undefined balance: undefined,
updatingWalletId: undefined,
stopped: false
}; };
$scope.motion = null; // no animation $scope.motion = null; // no animation
...@@ -473,23 +490,30 @@ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTransl ...@@ -473,23 +490,30 @@ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTransl
$scope.load = function(options) { $scope.load = function(options) {
options = options || {}; options = options || {};
$scope.loading = !options.silent; $scope.loading = angular.isUndefined(options.silent) || !options.silent;
$scope.formData.balance = undefined;
$scope.formData.updatingWalletId = undefined;
$scope.formData.stopped = false;
// Load currency, and filter translations (need by 'formatAmount' filter) // Load currency, and filter translations (need by 'formatAmount' filter)
var jobs = [ var jobs = [];
csCurrency.name()
.then(function(name) { jobs.push(csCurrency.name()
$scope.currency = name; .then(function(currency) {
$scope.currency = currency;
return filterTranslations.ready(); return filterTranslations.ready();
}) }));
];
// Get children wallets // Get children wallets
var tempWallets;
if (!$scope.wallets) { if (!$scope.wallets) {
jobs.push( jobs.push(
csWallet.children.all() csWallet.children.all()
.then(function(children) { .then(function(children) {
$scope.wallets = $scope.formData.showDefault ? [csWallet].concat(children) : children; $scope.wallets = [];
tempWallets = $scope.formData.showDefault ? [csWallet].concat(children) : children;
UIUtils.loading.hide();
$scope.updateView();
}) })
); );
} }
...@@ -497,7 +521,7 @@ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTransl ...@@ -497,7 +521,7 @@ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTransl
// Prepare load options // Prepare load options
var walletLoadOptions = { var walletLoadOptions = {
silent: true, silent: true,
minData: true, minData: !$scope.formData.showBalance,
sources: $scope.formData.showBalance, sources: $scope.formData.showBalance,
tx: { tx: {
enable: false enable: false
...@@ -508,34 +532,43 @@ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTransl ...@@ -508,34 +532,43 @@ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTransl
var loadCounter = 0; var loadCounter = 0;
var now = Date.now(); var now = Date.now();
var balance = 0; var balance = 0;
return $q.all(jobs) return (jobs.length ? $q.all(jobs) : $q.when())
// Load wallet data (apply a timeout between each wallet) // Load wallet data (apply a timeout between each wallet)
.then(function() { .then(function() {
console.debug("[wallets] Loading {0} wallets...".format($scope.wallets.length)); console.debug("[wallets] Loading {0} wallets...".format($scope.wallets.length));
return $scope.wallets.reduce(function(res, wallet) { return (tempWallets || $scope.wallets).reduce(function(res, wallet) {
var skip= !options.refresh && wallet.isDataLoaded(walletLoadOptions); var skip= !options.refresh && wallet.isDataLoaded(walletLoadOptions);
if (skip) { if (skip) {
console.debug("[wallets] Wallet #{0} already loaded. Skipping".format(wallet.id), walletLoadOptions); console.debug("[wallets] Wallet #{0} already loaded. Skipping".format(wallet.id), walletLoadOptions);
return res.then(function(){
balance += wallet.data.balance; balance += wallet.data.balance;
return res; if (tempWallets) {
$scope.wallets.push(wallet);
$scope.updateView();
}
});
} }
loadCounter++; loadCounter++;
return res.then(function() { return res.then(function() {
if ($scope.formData.stopped) return; // skip if stopped
// Loading next wallet, after waiting some time // Loading next wallet, after waiting some time
return $timeout(function() { $scope.formData.updatingWalletId = wallet.id;
return (!options.refresh ? return (!options.refresh ?
wallet.loadData(walletLoadOptions) : wallet.loadData(walletLoadOptions) :
wallet.refreshData(angular.merge({ wallet.refreshData(angular.merge({
requirements: wallet.requirements && (wallet.requirements.isMember || wallet.requirements.wasMember || wallet.requirements.pendingMembership) requirements: wallet.requirements && (wallet.requirements.isMember || wallet.requirements.wasMember || wallet.requirements.pendingMembership)
}, walletLoadOptions)) }, walletLoadOptions))
).then(function(walletData) { ).then(function(walletData) {
if (tempWallets) {
$scope.wallets.push(wallet);
$scope.updateView();
}
balance += walletData.balance; balance += walletData.balance;
}) })
.catch(function(err) { .catch(function(err) {
console.error("[wallets] Error while loading data of wallet #{0}".format(wallet.id), err); console.error("[wallets] Error while loading data of wallet #{0}".format(wallet.id), err);
hasLoadError = true; hasLoadError = true;
}); });
}, loadWalletWaitTime);
}); });
}, $q.when()); }, $q.when());
}) })
...@@ -547,10 +580,16 @@ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTransl ...@@ -547,10 +580,16 @@ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTransl
$scope.cancel(); $scope.cancel();
}); });
} }
if ($scope.formData.stopped) {
// Make sure to keep all wallets
if (tempWallets) $scope.wallets = tempWallets;
return;
}
if (loadCounter) { if (loadCounter) {
console.debug("[wallets] Loaded data of {0} wallet(s) in {1}ms".format(loadCounter, (Date.now() - now))); console.debug("[wallets] Loaded data of {0} wallet(s) in {1}ms".format(loadCounter, (Date.now() - now)));
} }
$scope.formData.balance = balance; $scope.formData.balance = balance;
$scope.formData.updatingWalletId = undefined;
$scope.loading = false; $scope.loading = false;
UIUtils.loading.hide(); UIUtils.loading.hide();
$scope.updateView(); $scope.updateView();
...@@ -579,6 +618,7 @@ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTransl ...@@ -579,6 +618,7 @@ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTransl
$scope.wallets = null; $scope.wallets = null;
$scope.loading = true; $scope.loading = true;
$scope.formData.balance = undefined; $scope.formData.balance = undefined;
$scope.formData.updatingWalletId = undefined;
}; };
$scope.updateView = function() { $scope.updateView = function() {
...@@ -592,8 +632,8 @@ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTransl ...@@ -592,8 +632,8 @@ function WalletSelectModalController($scope, $q, $timeout, UIUtils, filterTransl
} }
}; };
$scope.doUpdate = function(silent) { $scope.doUpdate = function(silent, event) {
if ($scope.loading || !$scope.wallets || !$scope.wallets.length) return $q.when(); if ($scope.loading || !$scope.wallets || !$scope.wallets.length || $scope.formData.updatingWalletId) return $q.when();
return $scope.load({silent: silent, refresh: true}) return $scope.load({silent: silent, refresh: true})
.then(function() { .then(function() {
......
...@@ -28,8 +28,10 @@ ...@@ -28,8 +28,10 @@
<span class="badge" ng-if="formData.showBalance" <span class="badge" ng-if="formData.showBalance"
ng-class=":rebind:{'badge-assertive': (walletData.balance <= 0), 'badge-balanced': (walletData.balance > 0) }"> ng-class=":rebind:{'badge-assertive': (walletData.balance <= 0), 'badge-balanced': (walletData.balance > 0) }">
<ion-spinner class="ion-spinner-small" ng-if="formData.updatingWalletId==wallet.id" icon="android"></ion-spinner>
<span ng-bind-html=":rebind:walletData.balance|formatAmount:{useRelative: formData.useRelative, currency: currency}"></span> <span ng-bind-html=":rebind:walletData.balance|formatAmount:{useRelative: formData.useRelative, currency: currency}"></span>
</span> </span>
<i class="icon ion-ios-arrow-right "></i> <i class="icon ion-ios-arrow-right "></i>
</div> </div>
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
<div class="hidden-xs hidden-sm padding text-center"> <div class="hidden-xs hidden-sm padding text-center">
<button class="button button-stable button-small-padding icon ion-loop ink" <button class="button button-stable button-small-padding icon ion-loop ink"
ng-click="doUpdate()" ng-click="doUpdate(true)"
title="{{:locale:'COMMON.BTN_REFRESH' | translate}}"> title="{{:locale:'COMMON.BTN_REFRESH' | translate}}">
</button> </button>
...@@ -42,32 +42,34 @@ ...@@ -42,32 +42,34 @@
</div> </div>
<div class="padding-top padding-xs" style="display: block; height: 60px;" ng-if="formData.showBalance"> <div class="padding-top padding-xs hidden-xs hidden-sm" style="display: block; height: 60px;">
<div class="pull-left" ng-if="!loading && formData.balance"> <div class="pull-left">
<h4> <h4>
<span translate>ACCOUNT.WALLET_LIST.TITLE</span> <span translate>ACCOUNT.WALLET_LIST.TITLE</span>
<small class="gray" ng-if="formData.balance">(<span translate>ACCOUNT.WALLET_LIST.TOTAL_DOTS</span>&nbsp; <small class="gray" ng-if="formData.balance">(<span translate>ACCOUNT.WALLET_LIST.TOTAL_DOTS</span>&nbsp;
<span ng-bind-html="formData.balance|formatAmount:{useRelative: formData.useRelative, currency: currency}"></span>)</small> <span ng-bind-html="formData.balance|formatAmount:{useRelative: formData.useRelative, currency: currency}"></span>)
</span> </small>
<ion-spinner class="ion-spinner-small" icon="android" ng-if="loading || formData.updatingWalletId"></ion-spinner>
</h4> </h4>
</div> </div>
</div> </div>
<div class="center padding" ng-if="loading">
<ion-spinner icon="android"></ion-spinner>
</div>
<div class="center padding gray" ng-if="!loading && wallets && !wallets.length"> <div class="center padding gray" ng-if="!loading && wallets && !wallets.length">
{{:locale:'ACCOUNT.WALLET_LIST.NO_WALLET'|translate}} {{:locale:'ACCOUNT.WALLET_LIST.NO_WALLET'|translate}}
</div> </div>
<ion-list ng-if="!loading" class="{{::motion.ionListClass}}"> <ion-list ng-if="wallets" class="{{::motion.ionListClass}}">
<ng-include ng-repeat="wallet in wallets track by wallet.id" <ng-include ng-repeat="wallet in wallets track by wallet.id"
src="'templates/wallet/list/item_wallet.html'"> src="'templates/wallet/list/item_wallet.html'">
</ng-include> </ng-include>
</ion-list> </ion-list>
<div class="center padding visible-xs visible-sm" ng-if="loading">
<ion-spinner icon="android"></ion-spinner>
</div>
</ion-content> </ion-content>
<button id="fab-add-wallet" <button id="fab-add-wallet"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment