diff --git a/js/controllers/profile.js b/js/controllers/profile.js index b13a2565d..ee9bfc576 100644 --- a/js/controllers/profile.js +++ b/js/controllers/profile.js @@ -1,5 +1,5 @@ 'use strict'; -angular.module('copayApp.controllers').controller('ProfileController', function($scope, $rootScope, $location, $modal, $filter, backupService, identityService) { +angular.module('copayApp.controllers').controller('ProfileController', function($scope, $rootScope, $location, $modal, $filter, $timeout, backupService, identityService) { $scope.username = $rootScope.iden.getName(); $scope.isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0; @@ -26,11 +26,13 @@ angular.module('copayApp.controllers').controller('ProfileController', function( }); }; + $scope.init = function() { if ($rootScope.quotaPerItem) { $scope.perItem = $filter('noFractionNumber')($rootScope.quotaPerItem / 1000, 1); $scope.nrWallets = parseInt($rootScope.quotaItems) - 1; } + // no need to add event handlers here. Wallet deletion is handle by callback. }; $scope.setWallets = function() { @@ -46,8 +48,10 @@ angular.module('copayApp.controllers').controller('ProfileController', function( w.usage = $filter('noFractionNumber')(bits / max * 100, 0); } }); - $scope.wallets = wallets; + $timeout(function(){ + $scope.$digest(); + }) }; $scope.downloadWalletBackup = function(w) { diff --git a/js/controllers/sidebar.js b/js/controllers/sidebar.js index 5da6e83ab..a6f7533ae 100644 --- a/js/controllers/sidebar.js +++ b/js/controllers/sidebar.js @@ -63,7 +63,19 @@ angular.module('copayApp.controllers').controller('SidebarController', function( $scope.walletSelection = false; $scope.setWallets(); }); - iden.on('deleteWallet', function() { + iden.on('walletDeleted', function(wid) { + if (wid == $rootScope.wallet.id) { + copay.logger.debug('Deleted focused wallet:', wid); + + // new focus + var newWid = $rootScope.iden.getLastFocusedWalletId(); + if (newWid && $rootScope.iden.getWalletById(newWid)) { + identityService.setFocusedWallet(newWid); + } else { + copay.logger.debug('No wallets'); + identityService.noFocusedWallet(newWid); + } + } $scope.walletSelection = false; $scope.setWallets(); }); diff --git a/js/models/Identity.js b/js/models/Identity.js index f83c816c1..94c9a2ef2 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -645,7 +645,7 @@ Identity.prototype.deleteWallet = function(walletId, cb) { this.storage.removeItem(Wallet.getStorageKey(walletId), function(err) { if (err) return cb(err); - self.emitAndKeepAlive('deletedWallet', walletId); + self.emitAndKeepAlive('walletDeleted', walletId); self.store(null, cb); return cb(); }); diff --git a/js/services/identityService.js b/js/services/identityService.js index 53cf4331f..790e0a1c4 100644 --- a/js/services/identityService.js +++ b/js/services/identityService.js @@ -126,6 +126,13 @@ angular.module('copayApp.services') $location.path('/send'); }; + root.noFocusedWallet = function() { + $rootScope.wallet = null; + $timeout(function() { + $rootScope.$digest(); + }) + }; + root.setFocusedWallet = function(w, dontUpdateIt) { if (!_.isObject(w)) w = $rootScope.iden.getWalletById(w); @@ -280,13 +287,8 @@ angular.module('copayApp.services') $rootScope.$digest() }); - iden.on('deletedWallet', function(wid) { - notification.info('Wallet deleted', $filter('translate')('This wallet was deleted')); - if ($rootScope.wallet.id === wid) { - $rootScope.wallet = null; - var lastFocused = iden.getLastFocusedWalletId(); - root.setFocusedWallet(lastFocused); - } + iden.on('walletDeleted', function(wid) { + // do nothing. this is handled 'on sync' on controller. }); iden.on('closed', function() {