Merge pull request #3819 from gabrielbazan7/fix/useDeviceLang01

Use device language in push notifications and service refactor
This commit is contained in:
Matias Alejo Garcia 2016-01-27 14:42:32 -03:00
commit c187f3e9ef
4 changed files with 49 additions and 47 deletions

View file

@ -13,14 +13,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.updatingTxHistory = {}; self.updatingTxHistory = {};
self.prevState = 'walletHome'; self.prevState = 'walletHome';
if (self.usePushNotifications) {
storageService.getDeviceToken(function(err, token) {
$timeout(function() {
if (!token) pushNotificationsService.pushNotificationsInit();
}, 5000);
});
}
function strip(number) { function strip(number) {
return (parseFloat(number.toPrecision(12))); return (parseFloat(number.toPrecision(12)));
}; };
@ -1233,7 +1225,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.updateRemotePreferences({ self.updateRemotePreferences({
saveAll: true saveAll: true
}, function() { }, function() {
$log.debug('Remote preferences saved') $log.debug('Remote preferences saved');
storageService.setRemotePrefsStoredFlag(function() {}); storageService.setRemotePrefsStoredFlag(function() {});
}); });
}); });
@ -1285,21 +1277,16 @@ angular.module('copayApp.controllers').controller('indexController', function($r
go.walletHome(); go.walletHome();
}); });
$rootScope.$on('Local/SubscribeNotifications', function(event) { $rootScope.$on('Local/ProfileCreated', function(event) {
if (!self.usePushNotifications) return; self.updateRemotePreferences({
saveAll: true
pushNotificationsService.enableNotifications(); }, function() {
$log.debug('Remote preferences saved');
});
}); });
$rootScope.$on('Local/UnsubscribeNotifications', function(event, walletId, cb) { $rootScope.$on('Local/pushNotificationsReady', function(event) {
if (!self.usePushNotifications) return cb(); pushNotificationsService.enableNotifications(profileService.walletClients);
pushNotificationsService.unsubscribe(walletId, function(err) {
if (err) $log.warn('Subscription error: ' + err.code);
else $log.debug('Unsubscribed from push notifications service');
return cb();
});
}); });
self.debouncedUpdate = lodash.throttle(function() { self.debouncedUpdate = lodash.throttle(function() {

View file

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

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
angular.module('copayApp.services') 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 = {}; var root = {};
@ -132,6 +132,8 @@ angular.module('copayApp.services')
if (err) return cb(err); if (err) return cb(err);
root._setFocus(focusedWalletId, function() { root._setFocus(focusedWalletId, function() {
$rootScope.$emit('Local/ProfileBound'); $rootScope.$emit('Local/ProfileBound');
storageService.getDeviceToken(function(err, token) {
if (!token) pushNotificationsService.pushNotificationsInit();
root.isDisclaimerAccepted(function(val) { root.isDisclaimerAccepted(function(val) {
if (!val) { if (!val) {
return cb(new Error('NONAGREEDDISCLAIMER: Non agreed disclaimer')); return cb(new Error('NONAGREEDDISCLAIMER: Non agreed disclaimer'));
@ -142,6 +144,7 @@ angular.module('copayApp.services')
}); });
}); });
}); });
});
}; };
@ -310,7 +313,9 @@ angular.module('copayApp.services')
var fc = root.focusedClient; var fc = root.focusedClient;
var walletId = fc.credentials.walletId; 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); $log.debug('Deleting Wallet:', fc.credentials.walletName);
@ -407,8 +412,9 @@ angular.module('copayApp.services')
handleImport(function() { handleImport(function() {
root.setAndStoreFocus(walletId, function() { root.setAndStoreFocus(walletId, function() {
storageService.storeProfile(root.profile, function(err) { storageService.storeProfile(root.profile, function(err) {
$rootScope.$emit('Local/ProfileCreated');
if (config.pushNotifications.enabled) if (config.pushNotifications.enabled)
$rootScope.$emit('Local/SubscribeNotifications'); pushNotificationsService.enableNotifications(root.walletClients);
return cb(err, walletId); return cb(err, walletId);
}); });
}); });

View file

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