From 58b573ac4e767bb572f4442e96df43691d23bbdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Fri, 15 Jan 2016 17:09:50 -0300 Subject: [PATCH] unsubscribe method refactor --- src/js/controllers/index.js | 11 +++-- src/js/services/profileService.js | 48 +++++++++++---------- src/js/services/pushNotificationsService.js | 18 ++++---- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index fb5eea173..dd012e694 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -1288,10 +1288,13 @@ angular.module('copayApp.controllers').controller('indexController', function($r pushNotificationsService.enableNotifications(); }); - $rootScope.$on('Local/UnsubscribeNotifications', function(event) { - pushNotificationsService.unsubscribe(null, function(err, response) { - if (err) $log.warn('Error: ' + err.code); - $log.debug('Unsubscribed: ' + response); + $rootScope.$on('Local/UnsubscribeNotifications', function(event, walletId, cb) { + storageService.getDeviceToken(function(err, token) { + pushNotificationsService.unsubscribe(token, walletId, function(err, response) { + if (err) $log.warn('Error: ' + err.code); + $log.debug('Unsubscribed: ' + response); + return cb(); + }); }); }); diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index 29efab586..b66f487ff 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -305,35 +305,37 @@ angular.module('copayApp.services') var fc = root.focusedClient; var walletId = fc.credentials.walletId; - $rootScope.$emit('Local/UnsubscribeNotifications'); - $log.debug('Deleting Wallet:', fc.credentials.walletName); + $rootScope.$emit('Local/UnsubscribeNotifications', walletId, function() { - fc.removeAllListeners(); - root.profile.credentials = lodash.reject(root.profile.credentials, { - walletId: walletId - }); + $log.debug('Deleting Wallet:', fc.credentials.walletName); - delete root.walletClients[walletId]; - root.focusedClient = null; + fc.removeAllListeners(); + root.profile.credentials = lodash.reject(root.profile.credentials, { + walletId: walletId + }); - storageService.clearLastAddress(walletId, function(err) { - if (err) $log.warn(err); - }); + delete root.walletClients[walletId]; + root.focusedClient = null; - storageService.removeTxHistory(walletId, function(err) { - if (err) $log.warn(err); - }); + storageService.clearLastAddress(walletId, function(err) { + if (err) $log.warn(err); + }); - storageService.clearBackupFlag(walletId, function(err) { - if (err) $log.warn(err); - }); + storageService.removeTxHistory(walletId, function(err) { + if (err) $log.warn(err); + }); - $timeout(function() { - root.setWalletClients(); - root.setAndStoreFocus(null, function() { - storageService.storeProfile(root.profile, function(err) { - if (err) return cb(err); - return cb(); + storageService.clearBackupFlag(walletId, function(err) { + if (err) $log.warn(err); + }); + + $timeout(function() { + root.setWalletClients(); + root.setAndStoreFocus(null, function() { + storageService.storeProfile(root.profile, function(err) { + if (err) return cb(err); + return cb(); + }); }); }); }); diff --git a/src/js/services/pushNotificationsService.js b/src/js/services/pushNotificationsService.js index cb8e1140a..c3d36b3a2 100644 --- a/src/js/services/pushNotificationsService.js +++ b/src/js/services/pushNotificationsService.js @@ -32,11 +32,11 @@ angular.module('copayApp.services') root.enableNotifications = function() { storageService.getDeviceToken(function(err, token) { - lodash.forEach(profileService.getWallets('testnet'), function(wallets) { + lodash.forEach(profileService.getWallets('testnet'), function(wallet) { var opts = {}; opts.type = isMobile.iOS() ? "ios" : isMobile.Android() ? "android" : null; opts.token = token; - root.subscribe(opts, wallets.id, function(err, response) { + root.subscribe(opts, wallet.id, function(err, response) { if (err) $log.warn('Error: ' + err.code); $log.debug('Suscribed: ' + JSON.stringify(response)); }); @@ -46,9 +46,11 @@ angular.module('copayApp.services') root.disableNotifications = function() { storageService.getDeviceToken(function(err, token) { - root.unsubscribe(token, function(err, response) { - if (err) $log.warn('Error: ' + err.code); - $log.debug('Unsubscribed: ' + response); + lodash.forEach(profileService.getWallets('testnet'), function(wallet) { + root.unsubscribe(token, wallet.id, function(err, response) { + if (err) $log.warn('Error: ' + err.code); + $log.debug('Unsubscribed: ' + response); + }); }); }); } @@ -61,9 +63,9 @@ angular.module('copayApp.services') }); } - root.unsubscribe = function(opts, cb) { - var walletClient = profileService.focusedClient; - walletClient.pushNotificationsUnsubscribe(opts, function(err, resp) { + root.unsubscribe = function(token, walletId, cb) { + var walletClient = profileService.getClient(walletId); + walletClient.pushNotificationsUnsubscribe(token, function(err, resp) { if (err) return cb(err); return cb(null, resp); });