From 425d8ffb3324ba64ff1635fd5c430af33679403e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Fri, 15 Jan 2016 11:59:29 -0300 Subject: [PATCH] push notifications service addapted to bws --- src/js/controllers/index.js | 30 ++++++++++----- src/js/controllers/preferencesGlobal.js | 6 +-- src/js/services/profileService.js | 4 +- src/js/services/pushNotificationsService.js | 41 +++++++++------------ 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index 9fe28c75c..92e408aab 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, pushNotificationsService, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, nodeWebkit, addonManager, feeService, isChromeApp, bwsError, txFormatService, uxLanguage, $state, glideraService, isMobile, addressbookService) { +angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, isMobile, pushNotificationsService, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, nodeWebkit, addonManager, feeService, isChromeApp, bwsError, txFormatService, uxLanguage, $state, glideraService, isMobile, addressbookService) { var self = this; var SOFT_CONFIRMATION_LIMIT = 12; self.isCordova = isCordova; @@ -12,7 +12,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.prevState = 'walletHome'; document.addEventListener('deviceready', function() { - if (self.isCordova) { + if (isMobile.Android() || isMobile.iOS()) { storageService.getDeviceToken(function(err, token) { $timeout(function() { if (!token) pushNotificationsService.pushNotificationsInit(); @@ -31,15 +31,24 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.menu = [{ 'title': gettext('Receive'), - 'icon': {false:'icon-receive', true: 'icon-receive-active'}, + 'icon': { + false: 'icon-receive', + true: 'icon-receive-active' + }, 'link': 'receive' }, { 'title': gettext('Activity'), - 'icon': {false:'icon-activity',true: 'icon-activity-active'}, + 'icon': { + false: 'icon-activity', + true: 'icon-activity-active' + }, 'link': 'walletHome' }, { 'title': gettext('Send'), - 'icon': {false:'icon-send', true: 'icon-send-active'}, + 'icon': { + false: 'icon-send', + true: 'icon-send-active' + }, 'link': 'send' }]; @@ -403,7 +412,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.availableMaxBalance = strip((self.availableBalanceSat - feeToSendMaxSat) * self.satToUnit); self.feeToSendMaxStr = profileService.formatAmount(feeToSendMaxSat) + ' ' + self.unitName; } - + if (cb) return cb(self.currentFeePerKb, self.availableMaxBalance, self.feeToSendMaxStr); }); } @@ -1277,8 +1286,11 @@ angular.module('copayApp.controllers').controller('indexController', function($r pushNotificationsService.enableNotifications(); }); - $rootScope.$on('Local/UnsubscribeNotifications', function(event, walletId) { - pushNotificationsService.unsubscribe(walletId); + $rootScope.$on('Local/UnsubscribeNotifications', function(event) { + pushNotificationsService.unsubscribe(null, function(err, response) { + if (err) $log.warn('Error: ' + err.code); + $log.debug('Unsubscribed: ' + response); + }); }); self.debouncedUpdate = lodash.throttle(function() { @@ -1482,5 +1494,5 @@ angular.module('copayApp.controllers').controller('indexController', function($r $rootScope.$apply(); }); }); - + }); diff --git a/src/js/controllers/preferencesGlobal.js b/src/js/controllers/preferencesGlobal.js index ee24f76a8..36184f34a 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, pushNotificationsService) { + function($scope, $rootScope, $log, configService, isMobile, uxLanguage, pushNotificationsService) { this.init = function() { var config = configService.getSync(); @@ -17,7 +17,7 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController', $scope.notifications = config.notifications ? config.notifications.enabled : true; }; - if (isCordova) $scope.mobile = true; + if (isMobile.Android() || isMobile.iOS()) $scope.mobile = true; else $scope.mobile = false; var unwatchSpendUnconfirmed = $scope.$watch('spendUnconfirmed', function(newVal, oldVal) { @@ -36,7 +36,7 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController', var unwatchNotification = $scope.$watch('notifications', function(newVal, oldVal) { if (newVal == oldVal) return; var opts = { - pushNotifications: { + pushNotifications: { enabled: newVal } }; diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index 889321b14..29efab586 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -304,6 +304,8 @@ angular.module('copayApp.services') root.deleteWalletFC = function(opts, cb) { var fc = root.focusedClient; var walletId = fc.credentials.walletId; + + $rootScope.$emit('Local/UnsubscribeNotifications'); $log.debug('Deleting Wallet:', fc.credentials.walletName); fc.removeAllListeners(); @@ -326,8 +328,6 @@ angular.module('copayApp.services') if (err) $log.warn(err); }); - $rootScope.$emit('Local/UnsubscribeNotifications', walletId); - $timeout(function() { root.setWalletClients(); root.setAndStoreFocus(null, function() { diff --git a/src/js/services/pushNotificationsService.js b/src/js/services/pushNotificationsService.js index 68eac8027..cb8e1140a 100644 --- a/src/js/services/pushNotificationsService.js +++ b/src/js/services/pushNotificationsService.js @@ -34,43 +34,38 @@ angular.module('copayApp.services') storageService.getDeviceToken(function(err, token) { lodash.forEach(profileService.getWallets('testnet'), function(wallets) { var opts = {}; - opts.user = wallets.id + '$' + wallets.copayerId; opts.type = isMobile.iOS() ? "ios" : isMobile.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.subscribe(opts, wallets.id, function(err, response) { + if (err) $log.warn('Error: ' + err.code); + $log.debug('Suscribed: ' + JSON.stringify(response)); + }); }); }); } root.disableNotifications = function() { storageService.getDeviceToken(function(err, token) { - root.unsubscribeAll(token).then(function(response) { - $log.debug('Unsubscribed: ' + response.status); - }, - function(err) { - $log.warn('Error: ' + err.status); - }); + root.unsubscribe(token, function(err, response) { + if (err) $log.warn('Error: ' + err.code); + $log.debug('Unsubscribed: ' + response); + }); }); } - root.subscribe = function(opts) { - return $http.post(defaults.pushNotifications.url + '/subscribe', opts); - } - - root.unsubscribe = function(user) { - return $http.post(defaults.pushNotifications.url + '/unsubscribe', { - user: user + root.subscribe = function(opts, walletId, cb) { + var walletClient = profileService.getClient(walletId); + walletClient.pushNotificationsSubscribe(opts, function(err, resp) { + if (err) return cb(err); + return cb(null, resp); }); } - root.unsubscribeAll = function(token) { - return $http.post(defaults.pushNotifications.url + '/unsubscribe', { - token: token + root.unsubscribe = function(opts, cb) { + var walletClient = profileService.focusedClient; + walletClient.pushNotificationsUnsubscribe(opts, function(err, resp) { + if (err) return cb(err); + return cb(null, resp); }); }