From 9b1708b88ef2e9c8b7f34d7d300dd5da4cb25bd3 Mon Sep 17 00:00:00 2001 From: Yemel Jardi Date: Mon, 4 Aug 2014 15:10:01 -0300 Subject: [PATCH] Save last opened wallet --- js/controllers/open.js | 2 +- js/models/core/WalletFactory.js | 4 ++++ js/models/storage/LocalEncrypted.js | 7 +++++++ test/mocks/FakeStorage.js | 7 +++++++ test/test.WalletFactory.js | 31 +++++++++++++++++++++++++++++ test/test.storage.LocalEncrypted.js | 12 +++++++++++ 6 files changed, 62 insertions(+), 1 deletion(-) diff --git a/js/controllers/open.js b/js/controllers/open.js index 02d47dd1f..917d1a281 100644 --- a/js/controllers/open.js +++ b/js/controllers/open.js @@ -9,7 +9,7 @@ angular.module('copayApp.controllers').controller('OpenController', }; $scope.loading = false; $scope.wallets = walletFactory.getWallets().sort(cmp); - $scope.selectedWalletId = $scope.wallets.length ? $scope.wallets[0].id : null; + $scope.selectedWalletId = walletFactory.storage.getLastOpened() || ($scope.wallets[0] && $scope.wallets[0].id); $scope.openPassword = ''; $scope.open = function(form) { diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index f689c1c6a..6f335ed0f 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -143,6 +143,7 @@ WalletFactory.prototype.create = function(opts) { opts.version = opts.version || this.version; var w = new Wallet(opts); w.store(); + this.storage.setLastOpened(w.id); return w; }; @@ -179,6 +180,8 @@ WalletFactory.prototype.open = function(walletId, opts) { if (w) { w.store(); } + + this.storage.setLastOpened(walletId); return w; }; @@ -194,6 +197,7 @@ WalletFactory.prototype.delete = function(walletId, cb) { var s = this.storage; this.log('## DELETING WALLET ID:' + walletId); //TODO s.deleteWallet(walletId); + s.setLastOpened(undefined); return cb(); }; diff --git a/js/models/storage/LocalEncrypted.js b/js/models/storage/LocalEncrypted.js index dd5a0f6d7..df9c9c703 100644 --- a/js/models/storage/LocalEncrypted.js +++ b/js/models/storage/LocalEncrypted.js @@ -172,6 +172,13 @@ Storage.prototype.deleteWallet = function(walletId) { } }; +Storage.prototype.setLastOpened = function(walletId) { + this.setGlobal('lastOpened', walletId); +} + +Storage.prototype.getLastOpened = function() { + return this.getGlobal('lastOpened'); +} //obj contains keys to be set Storage.prototype.setFromObj = function(walletId, obj) { diff --git a/test/mocks/FakeStorage.js b/test/mocks/FakeStorage.js index 9ea94c1b0..c7d86f3c1 100644 --- a/test/mocks/FakeStorage.js +++ b/test/mocks/FakeStorage.js @@ -19,6 +19,13 @@ FakeStorage.prototype.getGlobal = function(id) { return this.storage[id]; }; +FakeStorage.prototype.setLastOpened = function(val) { + this.storage['lastOpened'] = val; +}; + +FakeStorage.prototype.getLastOpened = function() { + return this.storage['lastOpened']; +}; FakeStorage.prototype.removeGlobal = function(id) { delete this.storage[id]; diff --git a/test/test.WalletFactory.js b/test/test.WalletFactory.js index 00cad01ce..c61787932 100644 --- a/test/test.WalletFactory.js +++ b/test/test.WalletFactory.js @@ -317,6 +317,20 @@ describe('WalletFactory model', function() { }); }); + it('should clean lastOpened on delete wallet', function(done) { + var wf = new WalletFactory(config, '0.0.1'); + var w = wf.create({ + name: 'test wallet' + }); + + wf.storage.setLastOpened(w.id); + wf.delete(w.id, function() { + var last = wf.storage.getLastOpened(); + should.equal(last, undefined); + done(); + }); + }); + it('should return false if wallet does not exist', function() { var opts = { 'requiredCopayers': 2, @@ -343,6 +357,23 @@ describe('WalletFactory model', function() { wf.read.calledWith(walletId).should.be.true; }); + it('should save lastOpened on create/open a wallet', function() { + var opts = { + 'requiredCopayers': 2, + 'totalCopayers': 3 + }; + var wf = new WalletFactory(config, '0.0.1'); + var w = wf.create(opts); + var last = wf.storage.getLastOpened(); + should.equal(last, w.id); + + wf.storage.setLastOpened('other_id'); + + var wo = wf.open(w.id, opts); + last = wf.storage.getLastOpened(); + should.equal(last, w.id); + }); + it('should return error if network are differents', function() { var opts = { 'requiredCopayers': 2, diff --git a/test/test.storage.LocalEncrypted.js b/test/test.storage.LocalEncrypted.js index 779f3542b..2212ab41f 100644 --- a/test/test.storage.LocalEncrypted.js +++ b/test/test.storage.LocalEncrypted.js @@ -148,6 +148,18 @@ describe('Storage/LocalEncrypted model', function() { s.getName(1).should.equal('hola'); }); }); + + describe('#getLastOpened #setLastOpened', function() { + it('should get/set names', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + password: 'password' + }); + s.setLastOpened('hey'); + s.getLastOpened().should.equal('hey'); + }); + }); + describe('#getWallets', function() { it('should retreive wallets from storage', function() { var s = new LocalEncrypted({