Merge pull request #2094 from matiu/ref/txproposalevents

refactor tx proposal events
This commit is contained in:
Gustavo Maximiliano Cortez 2014-12-10 19:01:54 -03:00
commit 20e19af2c7
12 changed files with 219 additions and 259 deletions

View file

@ -2,119 +2,55 @@
angular.module('copayApp.controllers').controller('HomeWalletController', function($scope, $rootScope, $timeout, $filter, $modal, rateService, notification, txStatus, identityService) {
$scope.initHome = function() {
var w = $rootScope.wallet;
$rootScope.title = 'Home';
$scope.rateService = rateService;
$scope.isRateAvailable = false;
var w = $rootScope.wallet;
if (w.isShared())
$scope.copayers = w.getRegisteredPeerIds();
w.on('txProposalEvent', _updateTxs);
_updateTxs();
rateService.whenAvailable(function() {
$scope.isRateAvailable = true;
$scope.$digest();
});
};
// This is necessary, since wallet can change in homeWallet,
// without running init() again.
var removeWatch;
removeWatch = $rootScope.$watch('wallet.id', function(newWallet, oldWallet) {
if ($rootScope.wallet && $rootScope.wallet.isComplete() && newWallet !== oldWallet) {
if (removeWatch)
removeWatch();
if (oldWallet) {
var oldw = $rootScope.iden.getWalletById(oldWallet);
if (oldw)
oldw.removeListener('txProposalEvent', _updateTxs);
}
var w = $rootScope.wallet;
$rootScope.pendingTxCount = 0;
w.on('txProposalEvent', _updateTxs);
_updateTxs();
}
});
$scope.$on("$destroy", function() {
var w = $rootScope.wallet;
if (w) {
removeWatch();
w.removeListener('txProposalEvent', _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;
$timeout(function(){
$scope.$digest();
},1)
}, 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;
if (!txStatus.notify(status))
$scope.error = status;
_updateTxs();
if (err)
$scope.error = err;
else
txStatus.notify(status);
});
};
$scope.reject = function(ntxid) {
var w = $rootScope.wallet;
w.reject(ntxid);
txStatus.notify('txRejected');
_updateTxs();
w.reject(ntxid, function(err, status) {
if (err)
$scope.error = err;
else
txStatus.notify(status);
});
};
$scope.broadcast = function(ntxid) {
var w = $rootScope.wallet;
$scope.error = $scope.success = null;
$scope.loading = true;
w.issueTx(ntxid, function(err, txid, status) {
$scope.loading = false;
if (err)
$scope.error = err;
txStatus.notify(status);
});
};
$scope.openTxModal = function(tx) {
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.tx = tx;
$scope.getShortNetworkName = function() {
var w = $rootScope.wallet;
return w.getNetworkName().substring(0, 4);
@ -131,7 +67,4 @@ angular.module('copayApp.controllers').controller('HomeWalletController', functi
controller: ModalInstanceCtrl,
});
};
});

View file

@ -160,11 +160,10 @@ angular.module('copayApp.controllers').controller('SendController',
comment: comment,
}, function(err, txid, status) {
$scope.loading = false;
if (err)
return $scope.setError(err);
$scope.resetForm(status);
txStatus.notify(status);
$scope.resetForm();
});
};
@ -313,37 +312,6 @@ angular.module('copayApp.controllers').controller('SendController',
}
};
$scope.notifyStatus = function(status) {
var msg;
if (status == copay.Wallet.TX_BROADCASTED)
msg = 'Transaction broadcasted!';
else if (status == copay.Wallet.TX_PROPOSAL_SENT)
msg = 'Transaction proposal created';
else if (status == copay.Wallet.TX_SIGNED)
msg = 'Transaction proposal was signed';
else if (status == copay.Wallet.TX_SIGNED_AND_BROADCASTED)
msg = 'Transaction signed and broadcasted!';
if (msg)
$scope.openTxStatusModal(msg);
else
$scope.error = status;
};
$scope.send = function(ntxid, cb) {
var w = $rootScope.wallet;
$scope.error = $scope.success = null;
$scope.loading = true;
$rootScope.txAlertCount = 0;
w.issueTx(ntxid, function(err, txid, status) {
$scope.loading = false;
$scope.resetForm(status);
if (cb) return cb();
});
};
$scope.setForm = function(to, amount, comment) {
var form = $scope.sendForm;
if (to) {
@ -367,7 +335,7 @@ angular.module('copayApp.controllers').controller('SendController',
}
};
$scope.resetForm = function(status) {
$scope.resetForm = function() {
var form = $scope.sendForm;
$scope.fetchingURL = null;
@ -391,10 +359,6 @@ angular.module('copayApp.controllers').controller('SendController',
form.address.$setViewValue('');
form.address.$render();
}
if (!txStatus.notify(status))
$scope.error = status;
$timeout(function() {
$rootScope.$digest();
}, 1);
@ -434,11 +398,8 @@ angular.module('copayApp.controllers').controller('SendController',
if (err) {
copay.logger.warn(err);
if (err.toString().match('TIMEOUT')) {
$scope.resetForm('Payment server timed out');
} else {
$scope.resetForm(err.toString());
}
$scope.resetForm();
$scope.error = err.toString();
} else {
$scope._merchantData = merchantData;
$scope._domain = merchantData.domain;