Simple addressbook
This commit is contained in:
parent
597e9cec23
commit
6dd8b98dfc
12 changed files with 352 additions and 66 deletions
66
src/js/services/addressbookService.js
Normal file
66
src/js/services/addressbookService.js
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.services').factory('addressbookService', function(storageService, profileService) {
|
||||
var root = {};
|
||||
|
||||
root.getLabel = function(addr, cb) {
|
||||
var fc = profileService.focusedClient;
|
||||
storageService.getAddressbook(fc.credentials.network, function(err, ab) {
|
||||
if (!ab) return cb();
|
||||
ab = JSON.parse(ab);
|
||||
if (ab[addr]) return cb(ab[addr]);
|
||||
else return cb();
|
||||
});
|
||||
};
|
||||
|
||||
root.list = function(cb) {
|
||||
var fc = profileService.focusedClient;
|
||||
storageService.getAddressbook(fc.credentials.network, function(err, ab) {
|
||||
if (err) return cb('Could not get the Addressbook');
|
||||
if (ab) ab = JSON.parse(ab);
|
||||
return cb(err, ab);
|
||||
});
|
||||
};
|
||||
|
||||
root.add = function(entry, cb) {
|
||||
var fc = profileService.focusedClient;
|
||||
root.list(function(err, ab) {
|
||||
if (err) return cb(err);
|
||||
if (!ab) ab = {};
|
||||
if (ab[entry.address]) return cb('Entry already exist');
|
||||
ab[entry.address] = entry.label;
|
||||
storageService.setAddressbook(fc.credentials.network, JSON.stringify(ab), function(err, ab) {
|
||||
if (err) return cb('Error adding new entry');
|
||||
root.list(function(err, ab) {
|
||||
return cb(err, ab);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
root.remove = function(addr, cb) {
|
||||
var fc = profileService.focusedClient;
|
||||
root.list(function(err, ab) {
|
||||
if (err) return cb(err);
|
||||
if (!ab) return;
|
||||
if (!ab[addr]) return cb('Entry does not exist');
|
||||
delete ab[addr];
|
||||
storageService.setAddressbook(fc.credentials.network, JSON.stringify(ab), function(err) {
|
||||
if (err) return cb('Error deleting entry');
|
||||
root.list(function(err, ab) {
|
||||
return cb(err, ab);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
root.removeAll = function() {
|
||||
var fc = profileService.focusedClient;
|
||||
storageService.removeAddressbook(fc.credentials.network, function(err) {
|
||||
if (err) return cb('Error deleting addressbook');
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
||||
return root;
|
||||
});
|
||||
|
|
@ -204,5 +204,17 @@ angular.module('copayApp.services')
|
|||
storage.remove('glideraToken-' + network, cb);
|
||||
};
|
||||
|
||||
root.setAddressbook = function(network, addressbook, cb) {
|
||||
storage.set('addressbook-' + network, addressbook, cb);
|
||||
};
|
||||
|
||||
root.getAddressbook = function(network, cb) {
|
||||
storage.get('addressbook-' + network, cb);
|
||||
};
|
||||
|
||||
root.removeAddressbook = function(network, cb) {
|
||||
storage.remove('addressbook-' + network, cb);
|
||||
};
|
||||
|
||||
return root;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue