2014-04-17 07:46:49 -07:00
|
|
|
'use strict';
|
|
|
|
|
2014-04-23 17:20:44 -07:00
|
|
|
angular.module('copay.controllerUtils')
|
|
|
|
.factory('controllerUtils', function($rootScope, $sce, $location, Socket, video) {
|
|
|
|
var root = {};
|
|
|
|
$rootScope.videoSrc = {};
|
2014-04-30 15:50:13 -07:00
|
|
|
$rootScope.loading = false;
|
|
|
|
|
2014-04-23 17:20:44 -07:00
|
|
|
$rootScope.getVideoURL = function(copayer) {
|
2014-04-24 14:24:03 -07:00
|
|
|
var encoded = $rootScope.videoSrc[copayer];
|
|
|
|
if (!encoded) return;
|
|
|
|
var url = decodeURI(encoded);
|
|
|
|
var trusted = $sce.trustAsResourceUrl(url);
|
|
|
|
return trusted;
|
2014-04-23 17:20:44 -07:00
|
|
|
};
|
2014-04-17 12:27:15 -07:00
|
|
|
|
2014-04-29 14:20:44 -07:00
|
|
|
$rootScope.getWalletDisplay = function() {
|
|
|
|
var w = $rootScope.wallet;
|
|
|
|
return w && (w.name || w.id);
|
|
|
|
};
|
|
|
|
|
2014-04-23 17:20:44 -07:00
|
|
|
root.logout = function() {
|
|
|
|
$rootScope.wallet = null;
|
|
|
|
delete $rootScope['wallet'];
|
|
|
|
$rootScope.totalBalance = 0;
|
2014-04-28 07:56:27 -07:00
|
|
|
video.close();
|
|
|
|
$rootScope.videoSrc = {};
|
2014-04-23 17:20:44 -07:00
|
|
|
$location.path('signin');
|
|
|
|
};
|
2014-04-17 12:27:15 -07:00
|
|
|
|
2014-04-23 17:20:44 -07:00
|
|
|
root.onError = function(scope) {
|
|
|
|
if (scope) scope.loading = false;
|
|
|
|
root.logout();
|
|
|
|
}
|
2014-04-23 14:07:20 -07:00
|
|
|
|
2014-04-23 17:20:44 -07:00
|
|
|
root.onErrorDigest = function(scope) {
|
|
|
|
root.onError(scope);
|
|
|
|
$rootScope.$digest();
|
|
|
|
}
|
2014-04-24 19:13:55 -07:00
|
|
|
|
|
|
|
root.startNetwork = function(w) {
|
2014-04-23 17:20:44 -07:00
|
|
|
var handlePeerVideo = function(err, peerID, url) {
|
|
|
|
if (err) {
|
2014-04-28 07:56:27 -07:00
|
|
|
delete $rootScope.videoSrc[peerID];
|
2014-04-24 14:24:03 -07:00
|
|
|
return;
|
2014-04-23 17:20:44 -07:00
|
|
|
}
|
2014-04-23 17:26:41 -07:00
|
|
|
$rootScope.videoSrc[peerID] = encodeURI(url);
|
2014-04-30 15:50:13 -07:00
|
|
|
$rootScope.$digest();
|
2014-04-23 17:20:44 -07:00
|
|
|
};
|
|
|
|
w.on('badMessage', function(peerId) {
|
|
|
|
$rootScope.flashMessage = {
|
|
|
|
type: 'error',
|
|
|
|
message: 'Received wrong message from peer id:' + peerId
|
|
|
|
};
|
|
|
|
});
|
2014-04-24 12:07:49 -07:00
|
|
|
w.on('created', function(myPeerID) {
|
|
|
|
video.setOwnPeer(myPeerID, w, handlePeerVideo);
|
2014-04-23 17:20:44 -07:00
|
|
|
$rootScope.wallet = w;
|
2014-04-30 15:50:13 -07:00
|
|
|
$location.path('addresses');
|
2014-04-23 17:20:44 -07:00
|
|
|
});
|
|
|
|
w.on('refresh', function() {
|
2014-04-30 15:50:13 -07:00
|
|
|
root.setSocketHandlers();
|
|
|
|
root.updateBalance(function() {
|
|
|
|
$rootScope.$digest();
|
|
|
|
});
|
|
|
|
$rootScope.$digest();
|
2014-04-23 17:20:44 -07:00
|
|
|
});
|
2014-04-25 12:19:24 -07:00
|
|
|
w.on('publicKeyRingUpdated', function() {
|
|
|
|
root.setSocketHandlers();
|
|
|
|
});
|
2014-04-23 17:20:44 -07:00
|
|
|
w.on('openError', root.onErrorDigest);
|
|
|
|
w.on('peer', function(peerID) {
|
2014-04-24 12:07:49 -07:00
|
|
|
video.callPeer(peerID, handlePeerVideo);
|
2014-04-23 17:20:44 -07:00
|
|
|
});
|
|
|
|
w.on('close', root.onErrorDigest);
|
|
|
|
w.netStart();
|
|
|
|
};
|
2014-04-24 12:35:52 -07:00
|
|
|
|
2014-04-30 15:50:13 -07:00
|
|
|
root.updateBalance = function(cb) {
|
|
|
|
root.setSocketHandlers();
|
|
|
|
$rootScope.balanceByAddr = {};
|
2014-04-23 17:20:44 -07:00
|
|
|
var w = $rootScope.wallet;
|
2014-04-30 15:50:13 -07:00
|
|
|
$rootScope.addrInfos = w.getAddressesInfo();
|
|
|
|
if ($rootScope.addrInfos.length === 0) return;
|
|
|
|
$rootScope.loading = true;
|
2014-04-23 17:20:44 -07:00
|
|
|
w.getBalance(false, function(balance, balanceByAddr) {
|
2014-04-30 15:50:13 -07:00
|
|
|
console.log('New total balance:', balance);
|
2014-04-23 17:20:44 -07:00
|
|
|
$rootScope.totalBalance = balance;
|
|
|
|
$rootScope.balanceByAddr = balanceByAddr;
|
2014-04-30 15:50:13 -07:00
|
|
|
$rootScope.selectedAddr = $rootScope.addrInfos[0].address.toString();
|
|
|
|
$rootScope.loading = false;
|
|
|
|
if (cb) cb();
|
2014-04-30 08:25:33 -07:00
|
|
|
});
|
|
|
|
w.getBalance(true, function(balance) {
|
|
|
|
console.log('New available balance:', balance);
|
2014-04-30 15:50:13 -07:00
|
|
|
$rootScope.availableBalance = balance;
|
|
|
|
$rootScope.loading = false;
|
|
|
|
if (cb) cb();
|
2014-04-21 09:16:15 -07:00
|
|
|
});
|
2014-04-23 17:20:44 -07:00
|
|
|
};
|
2014-04-17 07:46:49 -07:00
|
|
|
|
2014-04-23 17:20:44 -07:00
|
|
|
root.setSocketHandlers = function() {
|
2014-04-30 15:50:13 -07:00
|
|
|
// TODO: optimize this?
|
2014-04-23 17:20:44 -07:00
|
|
|
Socket.removeAllListeners();
|
2014-04-30 10:28:33 -07:00
|
|
|
if (!$rootScope.wallet) return;
|
2014-04-18 15:08:01 -07:00
|
|
|
|
2014-04-23 17:20:44 -07:00
|
|
|
var addrs = $rootScope.wallet.getAddressesStr();
|
|
|
|
for (var i = 0; i < addrs.length; i++) {
|
|
|
|
console.log('### SUBSCRIBE TO', addrs[i]);
|
|
|
|
Socket.emit('subscribe', addrs[i]);
|
|
|
|
}
|
|
|
|
addrs.forEach(function(addr) {
|
|
|
|
Socket.on(addr, function(txid) {
|
|
|
|
console.log('Received!', txid);
|
2014-04-30 15:50:13 -07:00
|
|
|
root.updateBalance(function() {
|
|
|
|
$rootScope.$digest();
|
|
|
|
});
|
2014-04-23 17:20:44 -07:00
|
|
|
});
|
2014-04-17 09:25:36 -07:00
|
|
|
});
|
2014-04-23 17:20:44 -07:00
|
|
|
};
|
|
|
|
return root;
|
|
|
|
});
|