From 509047e2b01da6929ad69429e6fbe1f89d9d12d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Tue, 26 Jan 2016 10:38:27 -0300 Subject: [PATCH 1/4] use detect language in push notifications, save remote preferences when creating a new profile --- src/js/controllers/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index af458e8a0..ff0ef3469 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -1287,8 +1287,12 @@ angular.module('copayApp.controllers').controller('indexController', function($r $rootScope.$on('Local/SubscribeNotifications', function(event) { if (!self.usePushNotifications) return; - - pushNotificationsService.enableNotifications(); + self.updateRemotePreferences({ + saveAll: true + }, function() { + $log.debug('Remote preferences saved'); + pushNotificationsService.enableNotifications(); + }); }); From e19ece08c72bb95fa0d5f94b665755f67180bcab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Tue, 26 Jan 2016 14:44:17 -0300 Subject: [PATCH 2/4] remove events and refactor of push notifications functions --- src/js/controllers/index.js | 17 ++------------ src/js/controllers/preferencesGlobal.js | 6 ++--- src/js/services/profileService.js | 11 +++++---- src/js/services/pushNotificationsService.js | 26 ++++++++++----------- 4 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index ff0ef3469..dce42a0ad 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -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(); }); }); diff --git a/src/js/controllers/preferencesGlobal.js b/src/js/controllers/preferencesGlobal.js index 5d5a04238..d7ce7bf93 100644 --- a/src/js/controllers/preferencesGlobal.js +++ b/src/js/controllers/preferencesGlobal.js @@ -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); }); }); diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index 37ff6a700..f6da17d15 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -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'); diff --git a/src/js/services/pushNotificationsService.js b/src/js/services/pushNotificationsService.js index 128c74dc4..1126d3135 100644 --- a/src/js/services/pushNotificationsService.js +++ b/src/js/services/pushNotificationsService.js @@ -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); From 426d8d29b0ad1994b9a3a56fdeda7689582dcfd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Tue, 26 Jan 2016 15:10:39 -0300 Subject: [PATCH 3/4] add push notification registration event --- src/js/controllers/index.js | 8 +++++++- src/js/services/pushNotificationsService.js | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index dce42a0ad..a2fb1644a 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -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(profileService.walletClients); + if (!token) pushNotificationsService.pushNotificationsInit(); }, 5000); }); } @@ -1293,6 +1293,12 @@ angular.module('copayApp.controllers').controller('indexController', function($r }); }); + $rootScope.$on('Local/pushNotificationsRegistration', function(event) { + var config = configService.getSync(); + if (self.usePushNotifications && config.pushNotifications.enabled) + pushNotificationsService.enableNotifications(profileService.walletClients); + }); + self.debouncedUpdate = lodash.throttle(function() { self.updateAll({ quiet: true diff --git a/src/js/services/pushNotificationsService.js b/src/js/services/pushNotificationsService.js index 1126d3135..c1abb7013 100644 --- a/src/js/services/pushNotificationsService.js +++ b/src/js/services/pushNotificationsService.js @@ -5,7 +5,7 @@ angular.module('copayApp.services') var defaults = configService.getDefaults(); var usePushNotifications = isCordova && !isMobile.Windows(); - root.pushNotificationsInit = function(walletClients) { + root.pushNotificationsInit = function() { 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(walletsClients); + $rootScope.$emit('Local/pushNotificationsRegistration'); }); }); From 9f0a1ae537bcff64fde3330a4b3f3e7fa5870c6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Tue, 26 Jan 2016 15:42:07 -0300 Subject: [PATCH 4/4] refactor --- src/js/controllers/index.js | 16 +++------------- src/js/services/profileService.js | 15 +++++++++------ src/js/services/pushNotificationsService.js | 13 +++++++++++-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index a2fb1644a..842cc04dc 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -13,14 +13,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.updatingTxHistory = {}; self.prevState = 'walletHome'; - if (self.usePushNotifications) { - storageService.getDeviceToken(function(err, token) { - $timeout(function() { - if (!token) pushNotificationsService.pushNotificationsInit(); - }, 5000); - }); - } - function strip(number) { return (parseFloat(number.toPrecision(12))); }; @@ -1233,7 +1225,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.updateRemotePreferences({ saveAll: true }, function() { - $log.debug('Remote preferences saved') + $log.debug('Remote preferences saved'); storageService.setRemotePrefsStoredFlag(function() {}); }); }); @@ -1293,10 +1285,8 @@ angular.module('copayApp.controllers').controller('indexController', function($r }); }); - $rootScope.$on('Local/pushNotificationsRegistration', function(event) { - var config = configService.getSync(); - if (self.usePushNotifications && config.pushNotifications.enabled) - pushNotificationsService.enableNotifications(profileService.walletClients); + $rootScope.$on('Local/pushNotificationsReady', function(event) { + pushNotificationsService.enableNotifications(profileService.walletClients); }); self.debouncedUpdate = lodash.throttle(function() { diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index f6da17d15..57076382a 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -132,12 +132,15 @@ angular.module('copayApp.services') if (err) return cb(err); root._setFocus(focusedWalletId, function() { $rootScope.$emit('Local/ProfileBound'); - root.isDisclaimerAccepted(function(val) { - if (!val) { - return cb(new Error('NONAGREEDDISCLAIMER: Non agreed disclaimer')); - } else { - return cb(); - } + storageService.getDeviceToken(function(err, token) { + if (!token) pushNotificationsService.pushNotificationsInit(); + root.isDisclaimerAccepted(function(val) { + if (!val) { + return cb(new Error('NONAGREEDDISCLAIMER: Non agreed disclaimer')); + } else { + return cb(); + } + }); }); }); }); diff --git a/src/js/services/pushNotificationsService.js b/src/js/services/pushNotificationsService.js index c1abb7013..132ccb43d 100644 --- a/src/js/services/pushNotificationsService.js +++ b/src/js/services/pushNotificationsService.js @@ -1,6 +1,6 @@ 'use strict'; angular.module('copayApp.services') - .factory('pushNotificationsService', function($http, $log, isMobile, storageService, configService, lodash, isCordova) { + .factory('pushNotificationsService', function($http, $rootScope, $log, isMobile, storageService, configService, lodash, isCordova) { var root = {}; var defaults = configService.getDefaults(); var usePushNotifications = isCordova && !isMobile.Windows(); @@ -8,12 +8,15 @@ angular.module('copayApp.services') root.pushNotificationsInit = function() { if (!usePushNotifications) return; + var config = configService.getSync(); + if (!config.pushNotifications.enabled) return; + var push = PushNotification.init(defaults.pushNotifications.config); push.on('registration', function(data) { $log.debug('Starting push notification registration'); storageService.setDeviceToken(data.registrationId, function() { - $rootScope.$emit('Local/pushNotificationsRegistration'); + $rootScope.$emit('Local/pushNotificationsReady'); }); }); @@ -36,6 +39,9 @@ angular.module('copayApp.services') root.enableNotifications = function(walletsClients) { if (!usePushNotifications) return; + var config = configService.getSync(); + if (!config.pushNotifications.enabled) return; + storageService.getDeviceToken(function(err, token) { lodash.forEach(walletsClients, function(walletClient) { var opts = {}; @@ -63,6 +69,9 @@ angular.module('copayApp.services') root.subscribe = function(opts, walletClient, cb) { if (!usePushNotifications) return; + var config = configService.getSync(); + if (!config.pushNotifications.enabled) return; + walletClient.pushNotificationsSubscribe(opts, function(err, resp) { if (err) return cb(err); return cb(null, resp);