diff --git a/js/controllers/open.js b/js/controllers/open.js index a98ff6a5e..e14663319 100644 --- a/js/controllers/open.js +++ b/js/controllers/open.js @@ -23,17 +23,14 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc } else { $scope.retreiving = false; $scope.wallets = wallets.sort(cmp); - - walletFactory.storage.getLastOpened(function(ret) { - if (ret && _.indexOf(_.pluck($scope.wallets, 'id')) == -1) - ret = null; - - $scope.selectedWalletId = ret || ($scope.wallets[0] && $scope.wallets[0].id); - - setTimeout(function() { - $rootScope.$digest(); - }, 0); + var lastOpened = _.findWhere($scope.wallets, { + lastOpened: true }); + $scope.selectedWalletId = lastOpened ? lastOpened.id : ($scope.wallets[0] && $scope.wallets[0].id); + + setTimeout(function() { + $rootScope.$digest(); + }, 0); } }); diff --git a/js/models/WalletFactory.js b/js/models/WalletFactory.js index e4adc834f..83e1fdbde 100644 --- a/js/models/WalletFactory.js +++ b/js/models/WalletFactory.js @@ -368,11 +368,19 @@ WalletFactory.prototype.open = function(walletId, passphrase, cb) { }; WalletFactory.prototype.getWallets = function(cb) { - this.storage.getWallets(function(ret) { - ret.forEach(function(i) { + var self = this; + this.storage.getWallets(function(wallets) { + wallets.forEach(function(i) { i.show = i.name ? ((i.name + ' <' + i.id + '>')) : i.id; }); - return cb(null, ret); + self.storage.getLastOpened(function(lastId) { + var last = _.findWhere(wallets, { + id: lastId + }); + if (last) + last.lastOpened = true; + return cb(null, wallets); + }) }); }; diff --git a/test/test.WalletFactory.js b/test/test.WalletFactory.js index 406a8731c..5ecd1008c 100644 --- a/test/test.WalletFactory.js +++ b/test/test.WalletFactory.js @@ -189,6 +189,7 @@ describe('WalletFactory model', function() { describe('#getWallets', function() { it('should return empty array if no wallets', function(done) { wf.storage.getWallets = sinon.stub().yields([]); + wf.storage.getLastOpened = sinon.stub().yields(null); wf.getWallets(function(err, ws) { should.not.exist(err); @@ -205,6 +206,7 @@ describe('WalletFactory model', function() { name: 'w', id: 'id2', }]); + wf.storage.getLastOpened = sinon.stub().yields(null); wf.getWallets(function(err, ws) { should.not.exist(err); @@ -220,6 +222,31 @@ describe('WalletFactory model', function() { done(); }); }); + it('should include last used info', function(done) { + wf.storage.getWallets = sinon.stub().yields([{ + name: 'w1', + id: 'id1', + }, { + name: 'w', + id: 'id2', + }]); + wf.storage.getLastOpened = sinon.stub().yields('id2'); + + wf.getWallets(function(err, ws) { + should.not.exist(err); + ws.should.deep.equal([{ + name: 'w1', + id: 'id1', + show: 'w1 ' + }, { + name: 'w', + id: 'id2', + lastOpened: true, + show: 'w ' + }]); + done(); + }); + }); }); describe('#delete', function() {