Wallet/js/services/controllerUtils.js

141 lines
4 KiB
JavaScript
Raw Normal View History

2014-04-17 11:46:49 -03:00
'use strict';
2014-04-23 21:20:44 -03:00
angular.module('copay.controllerUtils')
.factory('controllerUtils', function($rootScope, $sce, $location, Socket, video) {
var root = {};
2014-05-07 19:04:36 -03:00
$rootScope.videoInfo = {};
2014-04-30 19:50:13 -03:00
$rootScope.loading = false;
2014-04-23 21:20:44 -03:00
$rootScope.getVideoURL = function(copayer) {
2014-05-07 19:04:36 -03:00
var vi = $rootScope.videoInfo[copayer]
if (!vi) return;
2014-05-08 16:58:36 -03:00
if ($rootScope.wallet.getOnlinePeerIDs().indexOf(copayer) === -1) {
// peer disconnected, remove his video
delete $rootScope.videoInfo[copayer]
return;
}
2014-05-07 19:04:36 -03:00
var encoded = vi.url;
2014-04-24 18:24:03 -03:00
var url = decodeURI(encoded);
var trusted = $sce.trustAsResourceUrl(url);
return trusted;
2014-04-23 21:20:44 -03:00
};
2014-05-07 19:04:36 -03:00
$rootScope.getVideoMutedStatus = function(copayer) {
var vi = $rootScope.videoInfo[copayer]
if (!vi) {
return;
}
return vi.muted;
};
2014-04-29 18:20:44 -03:00
$rootScope.getWalletDisplay = function() {
var w = $rootScope.wallet;
return w && (w.name || w.id);
};
2014-04-23 21:20:44 -03:00
root.logout = function() {
$rootScope.wallet = null;
delete $rootScope['wallet'];
$rootScope.totalBalance = 0;
2014-04-28 11:56:27 -03:00
video.close();
2014-05-07 19:04:36 -03:00
$rootScope.videoInfo = {};
2014-04-23 21:20:44 -03:00
$location.path('signin');
};
2014-04-23 21:20:44 -03:00
root.onError = function(scope) {
if (scope) scope.loading = false;
root.logout();
}
2014-04-23 18:07:20 -03:00
2014-04-23 21:20:44 -03:00
root.onErrorDigest = function(scope) {
root.onError(scope);
$rootScope.$digest();
}
2014-04-24 23:13:55 -03:00
root.startNetwork = function(w) {
2014-04-23 21:20:44 -03:00
var handlePeerVideo = function(err, peerID, url) {
if (err) {
2014-05-07 19:04:36 -03:00
delete $rootScope.videoInfo[peerID];
2014-04-24 18:24:03 -03:00
return;
2014-04-23 21:20:44 -03:00
}
2014-05-07 19:04:36 -03:00
$rootScope.videoInfo[peerID] = {
url: encodeURI(url),
muted: peerID === w.network.peerId
};
2014-04-30 19:50:13 -03:00
$rootScope.$digest();
2014-04-23 21:20:44 -03:00
};
w.on('badMessage', function(peerId) {
$rootScope.flashMessage = {
type: 'error',
message: 'Received wrong message from peer id:' + peerId
};
});
w.on('ready', function(myPeerID) {
2014-04-24 16:07:49 -03:00
video.setOwnPeer(myPeerID, w, handlePeerVideo);
2014-04-23 21:20:44 -03:00
$rootScope.wallet = w;
2014-04-30 19:50:13 -03:00
$location.path('addresses');
$rootScope.$digest();
2014-04-23 21:20:44 -03:00
});
w.on('refresh', function() {
2014-05-09 15:39:02 -03:00
root.updateBalance();
2014-04-30 19:50:13 -03:00
$rootScope.$digest();
2014-04-23 21:20:44 -03:00
});
w.on('openError', root.onErrorDigest);
2014-05-09 14:35:57 -03:00
w.on('connect', function(peerID) {
if (peerID) {
video.callPeer(peerID, handlePeerVideo);
}
$rootScope.$digest();
2014-04-23 21:20:44 -03:00
});
2014-05-09 14:44:05 -03:00
w.on('disconnect', function(peerID) {
$rootScope.$digest();
});
2014-04-23 21:20:44 -03:00
w.on('close', root.onErrorDigest);
w.netStart();
};
2014-04-30 19:50:13 -03:00
root.updateBalance = function(cb) {
root.setSocketHandlers();
$rootScope.balanceByAddr = {};
2014-04-23 21:20:44 -03:00
var w = $rootScope.wallet;
2014-04-30 19:50:13 -03:00
$rootScope.addrInfos = w.getAddressesInfo();
if ($rootScope.addrInfos.length === 0) return;
$rootScope.loading = true;
2014-04-23 21:20:44 -03:00
w.getBalance(false, function(balance, balanceByAddr) {
2014-04-30 19:50:13 -03:00
console.log('New total balance:', balance);
2014-04-23 21:20:44 -03:00
$rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr;
2014-04-30 19:50:13 -03:00
$rootScope.selectedAddr = $rootScope.addrInfos[0].address.toString();
$rootScope.loading = false;
2014-05-09 15:17:24 -03:00
$rootScope.$digest();
2014-04-30 19:50:13 -03:00
if (cb) cb();
2014-04-30 12:25:33 -03:00
});
w.getBalance(true, function(balance) {
console.log('New available balance:', balance);
2014-04-30 19:50:13 -03:00
$rootScope.availableBalance = balance;
$rootScope.loading = false;
if (cb) cb();
2014-04-21 13:16:15 -03:00
});
2014-04-23 21:20:44 -03:00
};
2014-04-17 11:46:49 -03:00
2014-04-23 21:20:44 -03:00
root.setSocketHandlers = function() {
2014-04-30 19:50:13 -03:00
// TODO: optimize this?
2014-04-23 21:20:44 -03:00
Socket.removeAllListeners();
2014-04-30 14:28:33 -03:00
if (!$rootScope.wallet) return;
2014-04-18 19:08:01 -03:00
2014-04-23 21:20:44 -03: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-05-09 15:39:02 -03:00
root.updateBalance();
2014-04-23 21:20:44 -03:00
});
});
2014-04-23 21:20:44 -03:00
};
return root;
});