remove events and refactor of push notifications functions

This commit is contained in:
Gabriel Bazán 2016-01-26 14:44:17 -03:00
commit e19ece08c7
4 changed files with 25 additions and 35 deletions

View file

@ -16,7 +16,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
if (self.usePushNotifications) {
storageService.getDeviceToken(function(err, token) {
$timeout(function() {
if (!token) pushNotificationsService.pushNotificationsInit();
if (!token) pushNotificationsService.pushNotificationsInit(profileService.walletClients);
}, 5000);
});
}
@ -1285,24 +1285,11 @@ angular.module('copayApp.controllers').controller('indexController', function($r
go.walletHome();
});
$rootScope.$on('Local/SubscribeNotifications', function(event) {
if (!self.usePushNotifications) return;
$rootScope.$on('Local/ProfileCreated', function(event) {
self.updateRemotePreferences({
saveAll: true
}, function() {
$log.debug('Remote preferences saved');
pushNotificationsService.enableNotifications();
});
});
$rootScope.$on('Local/UnsubscribeNotifications', function(event, walletId, cb) {
if (!self.usePushNotifications) return cb();
pushNotificationsService.unsubscribe(walletId, function(err) {
if (err) $log.warn('Subscription error: ' + err.code);
else $log.debug('Unsubscribed from push notifications service');
return cb();
});
});

View file

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('preferencesGlobalController',
function($scope, $rootScope, $log, configService, uxLanguage, pushNotificationsService) {
function($scope, $rootScope, $log, configService, uxLanguage, pushNotificationsService, profileService) {
this.init = function() {
var config = configService.getSync();
@ -39,9 +39,9 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController',
};
configService.set(opts, function(err) {
if (opts.pushNotifications.enabled)
pushNotificationsService.enableNotifications();
pushNotificationsService.enableNotifications(profileService.walletClients);
else
pushNotificationsService.disableNotifications();
pushNotificationsService.disableNotifications(profileService.walletClients);
if (err) $log.debug(err);
});
});

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.services')
.factory('profileService', function profileServiceFactory($rootScope, $location, $timeout, $filter, $log, lodash, storageService, bwcService, configService, notificationService, isChromeApp, isCordova, gettext, gettextCatalog, nodeWebkit, bwsError, uxLanguage, bitcore) {
.factory('profileService', function profileServiceFactory($rootScope, $location, $timeout, $filter, $log, lodash, storageService, bwcService, configService, notificationService, pushNotificationsService, isChromeApp, isCordova, gettext, gettextCatalog, nodeWebkit, bwsError, uxLanguage, bitcore) {
var root = {};
@ -310,7 +310,9 @@ angular.module('copayApp.services')
var fc = root.focusedClient;
var walletId = fc.credentials.walletId;
$rootScope.$emit('Local/UnsubscribeNotifications', walletId, function() {
pushNotificationsService.unsubscribe(root.getClient(walletId), function(err) {
if (err) $log.warn('Subscription error: ' + err.code);
else $log.debug('Unsubscribed from push notifications service');
$log.debug('Deleting Wallet:', fc.credentials.walletName);
@ -407,8 +409,9 @@ angular.module('copayApp.services')
handleImport(function() {
root.setAndStoreFocus(walletId, function() {
storageService.storeProfile(root.profile, function(err) {
$rootScope.$emit('Local/ProfileCreated');
if (config.pushNotifications.enabled)
$rootScope.$emit('Local/SubscribeNotifications');
pushNotificationsService.enableNotifications(root.walletClients);
return cb(err, walletId);
});
});
@ -636,7 +639,7 @@ angular.module('copayApp.services')
root.unlockFC = function(cb) {
var fc = root.focusedClient;
if (!fc.isPrivKeyEncrypted())
if (!fc.isPrivKeyEncrypted())
return cb();
$log.debug('Wallet is encrypted');

View file

@ -1,11 +1,11 @@
'use strict';
angular.module('copayApp.services')
.factory('pushNotificationsService', function($http, $log, isMobile, profileService, storageService, configService, lodash, isCordova) {
.factory('pushNotificationsService', function($http, $log, isMobile, storageService, configService, lodash, isCordova) {
var root = {};
var defaults = configService.getDefaults();
var usePushNotifications = isCordova && !isMobile.Windows();
root.pushNotificationsInit = function() {
root.pushNotificationsInit = function(walletClients) {
if (!usePushNotifications) return;
var push = PushNotification.init(defaults.pushNotifications.config);
@ -13,7 +13,7 @@ angular.module('copayApp.services')
push.on('registration', function(data) {
$log.debug('Starting push notification registration');
storageService.setDeviceToken(data.registrationId, function() {
root.enableNotifications();
root.enableNotifications(walletsClients);
});
});
@ -33,15 +33,15 @@ angular.module('copayApp.services')
});
};
root.enableNotifications = function() {
root.enableNotifications = function(walletsClients) {
if (!usePushNotifications) return;
storageService.getDeviceToken(function(err, token) {
lodash.forEach(profileService.getWallets(null), function(wallet) {
lodash.forEach(walletsClients, function(walletClient) {
var opts = {};
opts.type = isMobile.iOS() ? "ios" : isMobile.Android() ? "android" : null;
opts.token = token;
root.subscribe(opts, wallet.id, function(err, response) {
root.subscribe(opts, walletClient, function(err, response) {
if (err) $log.warn('Subscription error: ' + err.code);
else $log.debug('Subscribed to push notifications service: ' + JSON.stringify(response));
});
@ -49,29 +49,29 @@ angular.module('copayApp.services')
});
}
root.disableNotifications = function() {
root.disableNotifications = function(walletsClients) {
if (!usePushNotifications) return;
lodash.forEach(profileService.getWallets(null), function(wallet) {
root.unsubscribe(wallet.id, function(err) {
lodash.forEach(walletsClients, function(walletClient) {
root.unsubscribe(walletClient, function(err) {
if (err) $log.warn('Subscription error: ' + err.code);
else $log.debug('Unsubscribed from push notifications service');
});
});
}
root.subscribe = function(opts, walletId, cb) {
root.subscribe = function(opts, walletClient, cb) {
if (!usePushNotifications) return;
var walletClient = profileService.getClient(walletId);
walletClient.pushNotificationsSubscribe(opts, function(err, resp) {
if (err) return cb(err);
return cb(null, resp);
});
}
root.unsubscribe = function(walletId, cb) {
root.unsubscribe = function(walletClient, cb) {
if (!usePushNotifications) return;
var walletClient = profileService.getClient(walletId);
walletClient.pushNotificationsUnsubscribe(function(err) {
if (err) return cb(err);
return cb(null);