added support for localstorage

This commit is contained in:
Mario Colque 2014-04-01 18:22:07 -03:00
parent 910906664c
commit c08eda28ab
6 changed files with 96 additions and 38 deletions

View File

@ -205,6 +205,7 @@
<script src="js/filters.js"></script>
<script src="js/services/network.js"></script>
<script src="js/services/storage.js"></script>
<script src="js/controllers/header.js"></script>
<script src="js/controllers/home.js"></script>

View File

@ -11,7 +11,8 @@ angular.module('copay',[
'copay.backup',
'copay.network',
'copay.signin',
'copay.peer'
'copay.peer',
'copay.storage'
]);
angular.module('copay.header', []);
@ -22,4 +23,5 @@ angular.module('copay.backup', []);
angular.module('copay.network', []);
angular.module('copay.signin', []);
angular.module('copay.peer', []);
angular.module('copay.storage', []);

View File

@ -4,7 +4,6 @@ angular.module('copay.peer').controller('PeerController',
function($scope, $rootScope, $location, $routeParams, Network) {
$scope.init = function() {
console.log('connecting peer init');
//Network.connect($rootScope.masterId);
};
});

View File

@ -1,28 +1,43 @@
'use strict';
angular.module('copay.signin').controller('SigninController',
function($scope, $rootScope, $location, Network) {
function($scope, $rootScope, $location, Network, Storage) {
var peerData = Storage.get('peerData');
$scope.loading = false;
$rootScope.peerId = null;
$rootScope.peerId = peerData ? peerData.peerId : null;
$scope.create = function() {
$scope.loading = true;
Network.init(function(pid) {
$rootScope.masterId = pid;
$location.path('peer');
Network.init(function() {
$location.path('peer');
});
};
$scope.join = function(cid) {
$scope.loading = true;
Network.init(function() {
Network.connect(cid, function() {
$rootScope.masterId = cid;
$location.path('peer');
});
});
if (cid) {
$rootScope.connectedTo.push(cid);
Network.init(function() {
Network.connect(cid, function() {
$location.path('peer');
});
});
}
};
if (peerData && peerData.peerId) {
console.log('------- reloaded ----------');
console.log(peerData);
$rootScope.peerId = peerData.peerId;
$rootScope.connectedPeers = peerData.connectedPeers;
console.log(peerData.connectedTo[0]);
$scope.join(peerData.connectedTo[0]);
}
});

View File

@ -1,11 +1,10 @@
'use strict';
angular.module('copay.network')
.factory('Network', function($rootScope) {
.factory('Network', function($rootScope, Storage) {
var peer;
$rootScope.connectedPeers = [];
$rootScope.connectedTo = [];
$rootScope.masterId = null;
$rootScope.peerId = null;
var _arrayDiff = function(a, b) {
@ -22,6 +21,18 @@ angular.module('copay.network')
return diff;
};
var _isInArray = function(i, array) {
return array.indexOf(i) > -1;
};
var _saveDataStorage = function() {
Storage.save('peerData', {
peerId: $rootScope.peerId,
connectedTo: $rootScope.connectedTo,
connectedPeers: $rootScope.connectedPeers
});
};
var _sender = function(pid, data) {
if (pid !== $rootScope.peerId) {
console.log('-------- sending data to: ' + pid + ' --------');
@ -46,9 +57,14 @@ angular.module('copay.network')
console.log(data);
var obj = JSON.parse(data);
if (obj.data.peers) {
if (obj.data.type === 'connectToPeers')
_connectToPeers(obj.data.peers);
}
if (obj.data.type === 'getPeers')
_send(obj.sender, {
type: 'connectToPeers',
peers: $rootScope.connectedPeers
});
};
var _connectToPeers = function(peers) {
@ -67,9 +83,13 @@ angular.module('copay.network')
peer.on('open', function(pid) {
$rootScope.peerId = pid;
$rootScope.connectedPeers.push(pid);
cb(pid);
if (!_isInArray(pid, $rootScope.connectedPeers))
$rootScope.connectedPeers.push(pid);
_saveDataStorage();
cb();
$rootScope.$digest();
});
@ -77,25 +97,26 @@ angular.module('copay.network')
peer.on('connection', function(conn) {
if (conn.label === 'wallet') {
conn.on('open', function() {
console.log('-------- ' + conn.peer + ' conected to me --------');
console.log('<<<<<<<<<<< ' + conn.peer + ' conected to me --------');
console.log($rootScope.masterId);
console.log($rootScope.peerId);
if ($rootScope.masterId === $rootScope.peerId) {
console.log('-------- I am the master --------');
var isConnected = $rootScope.connectedTo.indexOf(conn.peer);
if (isConnected === -1) {
var c = peer.connect(conn.peer, {
label: 'wallet',
serialization: 'none',
reliable: false,
metadata: { message: 'hi peer!' }
metadata: { message: 'hi copayer!' }
});
c.on('open', function() {
$rootScope.connectedPeers.push(conn.peer);
$rootScope.connectedTo.push(conn.peer);
console.log('>>>>>>>>> i am connected to ' + conn.peer);
if (!_isInArray(conn.peer, $rootScope.connectedPeers))
$rootScope.connectedPeers.push(conn.peer);
_send($rootScope.connectedPeers, { peers: $rootScope.connectedPeers });
if (!_isInArray(conn.peer, $rootScope.connectedTo))
$rootScope.connectedTo.push(conn.peer);
_saveDataStorage();
$rootScope.$digest();
});
@ -104,6 +125,8 @@ angular.module('copay.network')
}
});
peer.on('data', _onData);
peer.on('close', function() {
console.log('------- connection closed ---------');
});
@ -121,19 +144,14 @@ angular.module('copay.network')
c.on('open', function() {
console.log('-------- I\'m connected to ' + pid + ' ------');
console.log($rootScope.connectedPeers);
console.log($rootScope.connectedTo);
$rootScope.connectedPeers.push(pid);
$rootScope.connectedTo.push(pid);
if (typeof cb === 'function')
cb();
if (typeof cb === 'function') cb();
_send(c.peer, { type: 'getPeers' });
$rootScope.$digest();
console.log($rootScope.connectedPeers);
console.log($rootScope.connectedTo);
});
c.on('data', _onData);
@ -150,13 +168,14 @@ angular.module('copay.network')
_sender(pid, data);
});
} else if (typeof pids === 'string') {
_sender(pid, data);
_sender(pids, data);
}
};
var _disconnect = function() {
peer.disconnect();
peer.destroy();
Storage.remove('peerData');
console.log('Disconnected and destroyed connection');
}

22
js/services/storage.js Normal file
View File

@ -0,0 +1,22 @@
'use strict';
angular.module('copay.storage')
.factory('Storage', function($rootScope) {
return {
get: function(key) {
return JSON.parse(localStorage.getItem(key));
},
save: function(key, data) {
localStorage.setItem(key, JSON.stringify(data));
},
remove: function(key) {
localStorage.removeItem(key);
},
clearAll: function() {
localStorage.clear();
}
};
});