diff --git a/js/controllers/homeWallet.js b/js/controllers/homeWallet.js index 2c86559da..6b131e8f0 100644 --- a/js/controllers/homeWallet.js +++ b/js/controllers/homeWallet.js @@ -1,7 +1,95 @@ 'use strict'; -angular.module('copayApp.controllers').controller('HomeWalletController', function($scope, $rootScope) { +angular.module('copayApp.controllers').controller('HomeWalletController', function($scope, $rootScope, $timeout, $filter, rateService) { $scope.init = function() { - $rootScope.title = 'Home'; + $rootScope.title = 'Home'; + + $scope.rateService = rateService; + $scope.isRateAvailable = false; + + var w = $rootScope.wallet; + w.on('txProposalEvent', function() { _updateTxs()}); + $timeout(function() { + _updateTxs(); + }, 1); + + rateService.whenAvailable(function() { + $scope.isRateAvailable = true; + $scope.$digest(); + }); + }; + + // This is necesarry, since wallet can change in homeWallet, without running init() again. + var removeWatch = $rootScope.$watch('wallet.id', function(newWallet, oldWallet) { + if ($rootScope.wallet && $rootScope.wallet.isComplete() && newWallet !== oldWallet) { + var w = $rootScope.wallet; + $rootScope.pendingTxCount = 0; + w.on('txProposalEvent', function() { _updateTxs()}); + _updateTxs(); + } + }); + + $scope.$on("$destroy", function(){ + var w = $rootScope.wallet; + removeWatch(); + w.removeListener('txProposalEvent', function() {_updateTxs()} ); + }); + + $scope.setAlternativeAmount = function(w, tx, cb) { + rateService.whenAvailable(function() { + _.each(tx.outs, function(out) { + var valueSat = out.valueSat * w.settings.unitToSatoshi; + out.alternativeAmount = $filter('noFractionNumber')(rateService.toFiat(valueSat, $scope.alternativeIsoCode), 2); + out.alternativeIsoCode = $scope.alternativeIsoCode; + }); + if (cb) return cb(tx); + }); }; + + var _updateTxs = _.throttle(function() { + var w = $rootScope.wallet; + if (!w) return; + + $scope.alternativeIsoCode = w.settings.alternativeIsoCode; + $scope.myId = w.getMyCopayerId(); + + var res = w.getPendingTxProposals(); + _.each(res.txs, function(tx) { + $scope.setAlternativeAmount(w, tx); + if (tx.merchant) { + var url = tx.merchant.request_url; + var domain = /^(?:https?)?:\/\/([^\/:]+).*$/.exec(url)[1]; + tx.merchant.domain = domain; + } + if (tx.outs) { + _.each(tx.outs, function(out) { + out.valueSat = out.value; + out.value = $filter('noFractionNumber')(out.value); + }); + } + }); + $scope.txps = res.txs; + console.log('[homeWallet.js:45]',$scope.txps); //TODO + }, 100); + + + + $scope.sign = function(ntxid) { + var w = $rootScope.wallet; + $scope.loading = true; + $scope.error = $scope.success = null; + w.signAndSend(ntxid, function(err, id, status) { + $scope.loading = false; + $scope.notifyStatus(status); + _updateTxs(); + }); + }; + + $scope.reject = function(ntxid) { + var w = $rootScope.wallet; + w.reject(ntxid); + notification.warning('Transaction rejected', 'You rejected the transaction successfully'); + _updateTxs(); + }; + }); diff --git a/js/controllers/send.js b/js/controllers/send.js index f2279fe8a..bf52a5be8 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -8,7 +8,7 @@ angular.module('copayApp.controllers').controller('SendController', preconditions.checkState(w); preconditions.checkState(w.settings.unitToSatoshi); - $rootScope.title = 'Send'; + $rootScope.title = w.isShared() ? 'Create Transaction Proposal' : 'Send'; $scope.loading = false; $scope.error = $scope.success = null; var satToUnit = 1 / w.settings.unitToSatoshi; @@ -39,30 +39,7 @@ angular.module('copayApp.controllers').controller('SendController', }); if (cb) return cb(tx); }); - }; - - - $scope.updateTxs = _.throttle(function() { - var w = $rootScope.wallet; - if (!w) return; - - var res = w.getPendingTxProposals(); - _.each(res.txs, function(tx) { - $scope.setAlternativeAmount(w, tx); - if (tx.merchant) { - var url = tx.merchant.request_url; - var domain = /^(?:https?)?:\/\/([^\/:]+).*$/.exec(url)[1]; - tx.merchant.domain = domain; - } - if (tx.outs) { - _.each(tx.outs, function(out) { - out.valueSat = out.value; - out.value = $filter('noFractionNumber')(out.value); - }); - } - }); - $scope.txps = res.txs; - }, 1000); + }; /** * Setting the two related amounts as properties prevents an infinite @@ -119,16 +96,8 @@ angular.module('copayApp.controllers').controller('SendController', $scope.init = function() { - $rootScope.pendingTxCount = 0; - $scope.updateTxs(); - var w = $rootScope.wallet; - w.on('txProposalEvent', $scope.updateTxs); - }; - - $scope.$on("$destroy", function(){ - var w = $rootScope.wallet; - w.removeListener('txProposalEvent', $scope.updateTxs ); - }); + // Empty + }; $scope.showAddressBook = function() { return w && _.keys(w.addressBook).length > 0; @@ -155,7 +124,6 @@ angular.module('copayApp.controllers').controller('SendController', $scope.error = message; $scope.loading = false; - $scope.updateTxs(); }; $scope.submitForm = function(form) { @@ -201,7 +169,6 @@ angular.module('copayApp.controllers').controller('SendController', if (err) return $scope._showError(err); $scope.notifyStatus(status); - $scope.updateTxs(); }); }; @@ -422,25 +389,8 @@ angular.module('copayApp.controllers').controller('SendController', w.issueTx(ntxid, function(err, txid, status) { $scope.notifyStatus(status); if (cb) return cb(); - else $scope.updateTxs(); }); - }; - - $scope.sign = function(ntxid) { - $scope.loading = true; - $scope.error = $scope.success = null; - w.signAndSend(ntxid, function(err, id, status) { - $scope.loading = false; - $scope.notifyStatus(status); - $scope.updateTxs(); - }); - }; - - $scope.reject = function(ntxid) { - w.reject(ntxid); - notification.warning('Transaction rejected', 'You rejected the transaction successfully'); - $scope.updateTxs(); - }; + }; $scope.clearMerchant = function(callback) { // TODO: Find a better way of detecting diff --git a/test/unit/controllers/controllersSpec.js b/test/unit/controllers/controllersSpec.js index 7c0314fa1..21dd6dd05 100644 --- a/test/unit/controllers/controllersSpec.js +++ b/test/unit/controllers/controllersSpec.js @@ -53,6 +53,7 @@ describe("Unit: Controllers", function() { var w = {}; w.id = 1234; w.isComplete = sinon.stub().returns(true); + w.isShared = sinon.stub().returns(true); w.privateKey = {}; w.settings = { unitToSatoshi: 100, @@ -218,7 +219,7 @@ describe("Unit: Controllers", function() { }); it('should have a title', function() { - expect(scope.title).equal('Send'); + expect(scope.title).equal('Create Transaction Proposal'); }); it('should return true if wallet has addressBook', function() { @@ -254,13 +255,10 @@ describe("Unit: Controllers", function() { sendForm.amount.$setViewValue(anAmount); sendForm.comment.$setViewValue(aComment); - scope.updateTxs = sinon.spy(); - var w = scope.wallet; scope.submitForm(sendForm); sinon.assert.callCount(w.spend, 1); sinon.assert.callCount(w.broadcastTx, 0); - sinon.assert.callCount(scope.updateTxs, 1); var spendArgs = w.spend.getCall(0).args[0]; spendArgs.toAddress.should.equal(anAddr); spendArgs.amountSat.should.equal(anAmount * scope.wallet.settings.unitToSatoshi); diff --git a/views/homeWallet.html b/views/homeWallet.html index a0c3cff12..68994d632 100644 --- a/views/homeWallet.html +++ b/views/homeWallet.html @@ -16,16 +16,16 @@