From 171665afe6c03ed5400959f8dd243350afaa4adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Mon, 4 Jan 2016 14:11:24 -0300 Subject: [PATCH] refactor --- src/js/controllers/index.js | 83 ++---------------- src/js/controllers/preferencesGlobal.js | 7 +- src/js/services/profileService.js | 2 +- src/js/services/pushNotificationsService.js | 94 ++++++++++++++++++--- 4 files changed, 93 insertions(+), 93 deletions(-) diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index b2326d67a..ac39c4dc2 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -12,62 +12,15 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.prevState = 'walletHome'; document.addEventListener('deviceready', function() { - if (self.isCordova) { storageService.getDeviceToken(function(err, token) { - if (!token) pushNotificationInit(); + $timeout(function() { + if (!token) pushNotificationsService.pushNotificationsInit(); + }, 5000); }); } - }); - function pushNotificationInit() { - var push = PushNotification.init({ - android: { - senderID: "959259672122" - }, - ios: { - alert: "true", - badge: true, - sound: 'false' - }, - windows: {} - }); - - push.on('registration', function(data) { - var fc = profileService.focusedClient; - var opts = {}; - opts.user = fc.credentials.walletId; - opts.type = (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "ios" : (navigator.userAgent.match(/Android/i)) == "Android" ? "android" : null; - opts.token = data.registrationId; - - storageService.setDeviceToken(data.registrationId, function() { - pushNotificationsService.subscribe(opts).then(function(response) { - $log.debug('Suscribed: ' + response.status); - }, - function(err) { - $log.warn('Error: ' + err); - }); - }); - - }); - - push.on('notification', function(data) { - $log.debug('Notification event: ', data.message); - /* data.message, - data.title, - data.count, - data.sound, - data.image, - data.additionalData - */ - }); - - push.on('error', function(e) { - $log.warn('Error trying to push notifications: ', e); - }); - } - function strip(number) { return (parseFloat(number.toPrecision(12))); }; @@ -1254,32 +1207,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.updateAll(); }); - $rootScope.$on('Local/EnableNotifications', function(event, val) { - storageService.getDeviceToken(function(err, token) { - var fc = profileService.focusedClient; - var opts = {}; - opts.user = [fc.credentials.walletId]; - opts.type = (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "ios" : (navigator.userAgent.match(/Android/i)) == "Android" ? "android" : null; - opts.token = token; - if (val) { - pushNotificationsService.subscribe(opts).then(function(response) { - $log.debug('Suscribed: ' + response.status); - }, - function(err) { - $log.warn('Error: ' + err); - }); - } else { - pushNotificationsService.unsubscribe(token).then(function(response) { - $log.debug('Unsuscribed: ' + response.status); - }, - function(err) { - $log.warn('Error: ' + err); - }); - } - self.updateAll(); - }); - }); - $rootScope.$on('Local/FeeLevelUpdated', function(event, level) { self.setCurrentFeeLevel(level); }); @@ -1346,6 +1273,10 @@ angular.module('copayApp.controllers').controller('indexController', function($r go.walletHome(); }); + $rootScope.$on('Local/EnableNotifications', function(event) { + pushNotificationsService.enableNotifications(); + }); + self.debouncedUpdate = lodash.throttle(function() { self.updateAll({ quiet: true diff --git a/src/js/controllers/preferencesGlobal.js b/src/js/controllers/preferencesGlobal.js index e04f935a5..ab468733f 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, isCordova) { + function($scope, $rootScope, $log, configService, uxLanguage, isCordova, pushNotificationsService) { this.init = function() { var config = configService.getSync(); @@ -41,7 +41,10 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController', } }; configService.set(opts, function(err) { - $rootScope.$emit('Local/EnableNotifications', opts.notifications.enabled); + if (opts.notifications.enabled) + pushNotificationsService.enableNotifications(); + else + pushNotificationsService.disableNotifications(); if (err) $log.debug(err); }); }); diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index e10db0d49..9f50e9260 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -397,7 +397,7 @@ angular.module('copayApp.services') handleImport(function() { root.setAndStoreFocus(walletId, function() { storageService.storeProfile(root.profile, function(err) { - $rootScope.$emit('Local/EnableNotifications', true); + $rootScope.$emit('Local/EnableNotifications'); return cb(err, walletId); }); }); diff --git a/src/js/services/pushNotificationsService.js b/src/js/services/pushNotificationsService.js index 31e41486a..ea0f20d11 100644 --- a/src/js/services/pushNotificationsService.js +++ b/src/js/services/pushNotificationsService.js @@ -1,18 +1,84 @@ - 'use strict'; - angular.module('copayApp.services') - .factory('pushNotificationsService', function($http) { - var root = {}; +'use strict'; +angular.module('copayApp.services') + .factory('pushNotificationsService', function($http, $log, profileService, storageService, lodash) { + var root = {}; - root.subscribe = function(opts) { - return $http.post('http://192.168.1.126:8000/subscribe', opts); - } + root.pushNotificationsInit = function() { - root.unsubscribe = function(token) { - return $http.post('http://192.168.1.126:8000/unsubscribe', { - token: token - }); - } + var push = PushNotification.init({ + android: { + senderID: "959259672122" + }, + ios: { + alert: "true", + badge: true, + sound: 'false' + }, + windows: {} + }); - return root; + push.on('registration', function(data) { + storageService.setDeviceToken(data.registrationId, function() { + root.enableNotifications(); + }); + }); - }); + push.on('notification', function(data) { + $log.debug('Notification event: ', data.message); + /* data.message, + data.title, + data.count, + data.sound, + data.image, + data.additionalData + */ + }); + + push.on('error', function(e) { + $log.warn('Error trying to push notifications: ', e); + }); + }; + + root.enableNotifications = function() { + storageService.getDeviceToken(function(err, token) { + + lodash.forEach(profileService.getWallets('livenet'), function(wallets) { + var opts = {}; + opts.user = wallets.id; + opts.type = (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "ios" : (navigator.userAgent.match(/Android/i)) == "Android" ? "android" : null; + opts.token = token; + + root.subscribe(opts).then(function(response) { + $log.debug('Suscribed: ' + response.status); + }, + function(err) { + $log.warn('Error: ' + err.status); + }); + }); + }); + } + + root.disableNotifications = function() { + storageService.getDeviceToken(function(err, token) { + root.unsubscribe(token).then(function(response) { + $log.debug('Unsubscribed: ' + response.status); + }, + function(err) { + $log.warn('Error: ' + err.status); + }); + }); + } + + root.subscribe = function(opts) { + return $http.post('http://192.168.1.128:8000/subscribe', opts); + } + + root.unsubscribe = function(token) { + return $http.post('http://192.168.1.128:8000/unsubscribe', { + token: token + }); + } + + return root; + + });