2014-04-17 11:46:49 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2014-04-17 13:25:36 -03:00
|
|
|
angular.module('copay.controllerUtils').factory('controllerUtils', function ($rootScope, $location, Socket) {
|
2014-04-17 11:46:49 -03:00
|
|
|
var root = {};
|
2014-04-17 16:27:15 -03:00
|
|
|
|
|
|
|
|
root.logout = function(scope) {
|
|
|
|
|
delete $rootScope['wallet'];
|
|
|
|
|
$rootScope.totalBalance = 0;
|
|
|
|
|
$location.path('signin');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.onError = function(scope) {
|
|
|
|
|
if (scope) scope.loading = false;
|
|
|
|
|
$rootScope.flashMessage = {type:'error', message: 'Could not connect to peer'};
|
|
|
|
|
root.logout();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
root.onErrorDigest = function(scope) {
|
|
|
|
|
root.onError(scope);
|
|
|
|
|
$rootScope.$digest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-04-17 11:46:49 -03:00
|
|
|
root.setupUxHandlers = function(w) {
|
2014-04-17 16:27:15 -03:00
|
|
|
|
|
|
|
|
w.on('badMessage', function(peerId) {
|
|
|
|
|
$rootScope.flashMessage = {type:'error', message: 'Received wrong message from peer id:' + peerId};
|
|
|
|
|
});
|
|
|
|
|
|
2014-04-17 11:46:49 -03:00
|
|
|
w.on('created', function() {
|
|
|
|
|
$location.path('peer');
|
|
|
|
|
$rootScope.wallet = w;
|
2014-04-17 12:03:18 -03:00
|
|
|
|
2014-04-18 13:20:35 -03:00
|
|
|
$rootScope.wallet.getBalance(function(balance) {
|
2014-04-17 12:03:18 -03:00
|
|
|
$rootScope.totalBalance = balance;
|
|
|
|
|
$rootScope.$digest();
|
|
|
|
|
});
|
2014-04-17 11:46:49 -03:00
|
|
|
});
|
|
|
|
|
w.on('refresh', function() {
|
2014-04-17 16:27:15 -03:00
|
|
|
console.log('[controllerUtils.js] Refreshing'); //TODO
|
2014-04-18 13:20:35 -03:00
|
|
|
$rootScope.$digest();
|
2014-04-17 11:46:49 -03:00
|
|
|
});
|
2014-04-17 16:27:15 -03:00
|
|
|
w.on('openError', root.onErrorDigest);
|
|
|
|
|
w.on('close', root.onErrorDigest);
|
2014-04-17 11:46:49 -03:00
|
|
|
};
|
|
|
|
|
|
2014-04-18 13:20:35 -03:00
|
|
|
root.handleTransactionByAddress = function(scope, cb) {
|
2014-04-17 13:25:36 -03:00
|
|
|
var socket = Socket(scope);
|
|
|
|
|
var addrs = $rootScope.wallet.getAddressesStr();
|
|
|
|
|
for(var i=0;i<addrs.length;i++) {
|
|
|
|
|
socket.emit('subscribe', addrs[i]);
|
|
|
|
|
}
|
|
|
|
|
addrs.forEach(function(addr) {
|
|
|
|
|
socket.on(addr, function(txid) {
|
|
|
|
|
console.log('Received!', txid);
|
2014-04-18 13:20:35 -03:00
|
|
|
$rootScope.wallet.getBalance(function(balance) {
|
2014-04-17 13:25:36 -03:00
|
|
|
scope.$apply(function() {
|
|
|
|
|
$rootScope.totalBalance = balance;
|
|
|
|
|
});
|
|
|
|
|
console.log('New balance:', balance);
|
2014-04-18 13:20:35 -03:00
|
|
|
if (typeof cb === 'function') return cb();
|
2014-04-17 13:25:36 -03:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-17 11:46:49 -03:00
|
|
|
return root;
|
|
|
|
|
});
|
|
|
|
|
|