replaced last opened with last focused wallet

This commit is contained in:
Ivan Socolsky 2014-10-20 18:33:21 -03:00 committed by Matias Alejo Garcia
commit 37526b554e
7 changed files with 46 additions and 38 deletions

View file

@ -25,13 +25,13 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc
networkName: config.networkName, networkName: config.networkName,
walletDefaults: config.wallet, walletDefaults: config.wallet,
passphraseConfig: config.passphraseConfig, passphraseConfig: config.passphraseConfig,
}, function(err, iden, firstWallet) { }, function(err, iden, lastFocusedWallet) {
if (err && !iden) { if (err && !iden) {
console.log('Error:' + err) console.log('Error:' + err)
controllerUtils.onErrorDigest( controllerUtils.onErrorDigest(
$scope, (err.toString() || '').match('PNOTFOUND') ? 'Profile not found' : 'Unknown error'); $scope, (err.toString() || '').match('PNOTFOUND') ? 'Profile not found' : 'Unknown error');
} else { } else {
controllerUtils.bindProfile($scope, iden, firstWallet); controllerUtils.bindProfile($scope, iden, lastFocusedWallet);
} }
}); });
} }

View file

@ -207,10 +207,8 @@ Identity.open = function(email, password, opts, cb) {
wallets.push(w); wallets.push(w);
} }
if (--remaining == 0) { if (--remaining == 0) {
var firstWallet = _.findWhere(wallets, { var lastFocused = iden.profile.getLastFocusedWallet();
id: wids[0] return cb(err, iden, lastFocused);
});
return cb(err, iden, firstWallet);
} }
}) })
}); });
@ -350,7 +348,7 @@ Identity.prototype.toEncryptedObj = function() {
ret.iterations = this.storage.iterations; ret.iterations = this.storage.iterations;
ret.wallets = {}; ret.wallets = {};
_.each(this.openWallets, function(w){ _.each(this.openWallets, function(w) {
ret.wallets[w.getId()] = w.toEncryptedObj(); ret.wallets[w.getId()] = w.toEncryptedObj();
}); });
@ -437,11 +435,8 @@ Identity.prototype.createWallet = function(opts, cb) {
this.addWallet(w, function(err) { this.addWallet(w, function(err) {
if (err) return cb(err); if (err) return cb(err);
self.openWallets.push(w); self.openWallets.push(w);
w.netStart();
self.profile.setLastOpenedTs(w.id, function(err) { return cb(err, w);
w.netStart();
return cb(err, w);
});
}); });
}; };
@ -511,10 +506,8 @@ Identity.prototype.openWallet = function(walletId, cb) {
self.openWallets.push(w); self.openWallets.push(w);
w.store(function(err) { w.store(function(err) {
self.profile.setLastOpenedTs(walletId, function() { w.netStart();
w.netStart(); return cb(err, w);
return cb(err, w);
});
}); });
}); });
// }); // });

View file

@ -76,7 +76,7 @@ Profile.prototype.getWallet = function(walletId, cb) {
Profile.prototype.listWallets = function() { Profile.prototype.listWallets = function() {
return _.sortBy(this.walletInfos, function(winfo) { return _.sortBy(this.walletInfos, function(winfo) {
return -winfo.lastOpenedTs || -winfo.createdTs; return winfo.createdTs;
}); });
}; };
@ -121,12 +121,21 @@ Profile.prototype.addWallet = function(walletId, info, cb) {
}; };
Profile.prototype.setLastOpenedTs = function(walletId, cb) { Profile.prototype.setLastFocusedTs = function(walletId, cb) {
return this.addToWallet(walletId, { return this.addToWallet(walletId, {
lastOpenedTs: Date.now() lastFocusedTs: Date.now()
}, cb); }, cb);
}; };
Profile.prototype.getLastFocusedWallet = function() {
var self = this;
var maxTs = _.max(_.pluck(self.walletInfos, 'lastFocusedTs'));
var last = _.findWhere(_.values(self.walletInfos), {
lastFocusedTs: maxTs
});
return last ? last.id : null;
};
Profile.prototype.store = function(opts, cb) { Profile.prototype.store = function(opts, cb) {
var self = this; var self = this;
var val = self.toObj(); var val = self.toObj();

View file

@ -25,7 +25,6 @@ function TxProposal(opts) {
this.version = opts.version; this.version = opts.version;
this.builder = opts.builder; this.builder = opts.builder;
this.createdTs = opts.createdTs; this.createdTs = opts.createdTs;
this.createdTs = opts.createdTs;
this._inputSigners = []; this._inputSigners = [];
// CopayerIds // CopayerIds

View file

@ -209,10 +209,12 @@ angular.module('copayApp.services')
preconditions.checkState(w && _.isObject(w)); preconditions.checkState(w && _.isObject(w));
$rootScope.wallet = w; $rootScope.wallet = w;
root.redirIfLogged(); $rootScope.iden.profile.setLastFocusedTs(w.id, function() {
root.updateBalance(w, function() { root.redirIfLogged();
$rootScope.$digest(); root.updateBalance(w, function() {
}) $rootScope.$digest();
})
});
}; };
root.bindProfile = function($scope, iden, w) { root.bindProfile = function($scope, iden, w) {

View file

@ -134,6 +134,7 @@ describe('Identity model', function() {
profile.listWallets = sinon.stub().returns([{ profile.listWallets = sinon.stub().returns([{
id: 'walletid' id: 'walletid'
}]); }]);
profile.getLastFocusedWallet = sinon.stub().returns(null);
Identity._openProfile = sinon.stub().callsArgWith(3, null, profile); Identity._openProfile = sinon.stub().callsArgWith(3, null, profile);
Identity._walletRead = sinon.stub().callsArgWith(2, null, wallet); Identity._walletRead = sinon.stub().callsArgWith(2, null, wallet);
}); });
@ -147,7 +148,7 @@ describe('Identity model', function() {
}); });
}); });
it('should return last used wallet', function(done) { it('should return last focused wallet', function(done) {
var wallets = [{ var wallets = [{
id: 'wallet1', id: 'wallet1',
store: sinon.stub().yields(null), store: sinon.stub().yields(null),
@ -162,13 +163,14 @@ describe('Identity model', function() {
netStart: sinon.stub(), netStart: sinon.stub(),
}]; }];
profile.listWallets = sinon.stub().returns(wallets); profile.listWallets = sinon.stub().returns(wallets);
profile.getLastFocusedWallet = sinon.stub().returns(wallets[1]);
Identity._walletRead = sinon.stub(); Identity._walletRead = sinon.stub();
Identity._walletRead.onCall(0).callsArgWith(2, null, wallets[0]); Identity._walletRead.onCall(0).callsArgWith(2, null, wallets[0]);
Identity._walletRead.onCall(1).callsArgWith(2, null, wallets[1]); Identity._walletRead.onCall(1).callsArgWith(2, null, wallets[1]);
Identity._walletRead.onCall(2).callsArgWith(2, null, wallets[2]); Identity._walletRead.onCall(2).callsArgWith(2, null, wallets[2]);
Identity.open(email, password, config, function(err, iden, w) { Identity.open(email, password, config, function(err, iden, w) {
w.id.should.equal('wallet1'); w.id.should.equal('wallet2');
done(); done();
}); });
}); });
@ -270,12 +272,11 @@ describe('Identity model', function() {
Identity._walletRead = sinon.stub().callsArgWith(2, null, wallet); Identity._walletRead = sinon.stub().callsArgWith(2, null, wallet);
}); });
it('should return wallet and call .store, .setLastOpenedTs & .migrateWallet', function(done) { it('should return wallet and call .store & .migrateWallet', function(done) {
iden.openWallet('dummy', function(err, w) { iden.openWallet('dummy', function(err, w) {
should.not.exist(err); should.not.exist(err);
w.store.calledOnce.should.equal(true); w.store.calledOnce.should.equal(true);
iden.profile.setLastOpenedTs.calledTwice.should.equal(true);
// iden.migrateWallet.calledOnce.should.equal(true); // iden.migrateWallet.calledOnce.should.equal(true);
done(); done();
}); });

View file

@ -15,7 +15,7 @@ describe('Profile model', function() {
var storage = new FakeStorage(); var storage = new FakeStorage();
var opts = { var opts = {
email: email, email: email,
hash:hash, hash: hash,
}; };
beforeEach(function() { beforeEach(function() {
@ -87,7 +87,9 @@ describe('Profile model', function() {
describe('#addToWallet', function() { describe('#addToWallet', function() {
it('should warn if wallet does not exist', function(done) { it('should warn if wallet does not exist', function(done) {
var p = new Profile(opts, storage); var p = new Profile(opts, storage);
p.addToWallet('234',{1:1}, function(err) { p.addToWallet('234', {
1: 1
}, function(err) {
err.toString().should.contain('WNOEXIST'); err.toString().should.contain('WNOEXIST');
done(); done();
}); });
@ -95,7 +97,9 @@ describe('Profile model', function() {
it('should add info to a wallet', function(done) { it('should add info to a wallet', function(done) {
var p = new Profile(opts, storage); var p = new Profile(opts, storage);
p.addWallet('234', {}, function(err) { p.addWallet('234', {}, function(err) {
p.addToWallet('234',{'hola':1}, function(err) { p.addToWallet('234', {
'hola': 1
}, function(err) {
var w = p.getWallet('234'); var w = p.getWallet('234');
should.exist(w); should.exist(w);
w.hola.should.equal(1); w.hola.should.equal(1);
@ -113,11 +117,11 @@ describe('Profile model', function() {
var p = new Profile(opts, storage); var p = new Profile(opts, storage);
p.addWallet('123', {}, function(err) { p.addWallet('123', {}, function(err) {
setTimeout(function() { setTimeout(function() {
p.addWallet('234', {}, function(err) { p.addWallet('234', {}, function(err) {
_.pluck(p.listWallets(), 'id').should.deep.equal(['234', '123']); _.pluck(p.listWallets(), 'id').should.deep.equal(['123', '234']);
done(); done();
}) })
},10); }, 10);
}); });
}); });
}); });