unsubscribe method refactor

This commit is contained in:
Gabriel Bazán 2016-01-15 17:09:50 -03:00
commit 58b573ac4e
3 changed files with 42 additions and 35 deletions

View file

@ -1288,10 +1288,13 @@ angular.module('copayApp.controllers').controller('indexController', function($r
pushNotificationsService.enableNotifications(); pushNotificationsService.enableNotifications();
}); });
$rootScope.$on('Local/UnsubscribeNotifications', function(event) { $rootScope.$on('Local/UnsubscribeNotifications', function(event, walletId, cb) {
pushNotificationsService.unsubscribe(null, function(err, response) { storageService.getDeviceToken(function(err, token) {
if (err) $log.warn('Error: ' + err.code); pushNotificationsService.unsubscribe(token, walletId, function(err, response) {
$log.debug('Unsubscribed: ' + response); if (err) $log.warn('Error: ' + err.code);
$log.debug('Unsubscribed: ' + response);
return cb();
});
}); });
}); });

View file

@ -305,35 +305,37 @@ 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'); $rootScope.$emit('Local/UnsubscribeNotifications', walletId, function() {
$log.debug('Deleting Wallet:', fc.credentials.walletName);
fc.removeAllListeners(); $log.debug('Deleting Wallet:', fc.credentials.walletName);
root.profile.credentials = lodash.reject(root.profile.credentials, {
walletId: walletId
});
delete root.walletClients[walletId]; fc.removeAllListeners();
root.focusedClient = null; root.profile.credentials = lodash.reject(root.profile.credentials, {
walletId: walletId
});
storageService.clearLastAddress(walletId, function(err) { delete root.walletClients[walletId];
if (err) $log.warn(err); root.focusedClient = null;
});
storageService.removeTxHistory(walletId, function(err) { storageService.clearLastAddress(walletId, function(err) {
if (err) $log.warn(err); if (err) $log.warn(err);
}); });
storageService.clearBackupFlag(walletId, function(err) { storageService.removeTxHistory(walletId, function(err) {
if (err) $log.warn(err); if (err) $log.warn(err);
}); });
$timeout(function() { storageService.clearBackupFlag(walletId, function(err) {
root.setWalletClients(); if (err) $log.warn(err);
root.setAndStoreFocus(null, function() { });
storageService.storeProfile(root.profile, function(err) {
if (err) return cb(err); $timeout(function() {
return cb(); root.setWalletClients();
root.setAndStoreFocus(null, function() {
storageService.storeProfile(root.profile, function(err) {
if (err) return cb(err);
return cb();
});
}); });
}); });
}); });

View file

@ -32,11 +32,11 @@ angular.module('copayApp.services')
root.enableNotifications = function() { root.enableNotifications = function() {
storageService.getDeviceToken(function(err, token) { storageService.getDeviceToken(function(err, token) {
lodash.forEach(profileService.getWallets('testnet'), function(wallets) { lodash.forEach(profileService.getWallets('testnet'), function(wallet) {
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, wallets.id, function(err, response) { root.subscribe(opts, wallet.id, function(err, response) {
if (err) $log.warn('Error: ' + err.code); if (err) $log.warn('Error: ' + err.code);
$log.debug('Suscribed: ' + JSON.stringify(response)); $log.debug('Suscribed: ' + JSON.stringify(response));
}); });
@ -46,9 +46,11 @@ angular.module('copayApp.services')
root.disableNotifications = function() { root.disableNotifications = function() {
storageService.getDeviceToken(function(err, token) { storageService.getDeviceToken(function(err, token) {
root.unsubscribe(token, function(err, response) { lodash.forEach(profileService.getWallets('testnet'), function(wallet) {
if (err) $log.warn('Error: ' + err.code); root.unsubscribe(token, wallet.id, function(err, response) {
$log.debug('Unsubscribed: ' + 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) { root.unsubscribe = function(token, walletId, cb) {
var walletClient = profileService.focusedClient; var walletClient = profileService.getClient(walletId);
walletClient.pushNotificationsUnsubscribe(opts, function(err, resp) { walletClient.pushNotificationsUnsubscribe(token, function(err, resp) {
if (err) return cb(err); if (err) return cb(err);
return cb(null, resp); return cb(null, resp);
}); });