refactor push object listeners and calls

This commit is contained in:
Javier 2016-03-10 10:58:58 -03:00
commit e180d0bbd4
3 changed files with 46 additions and 67 deletions

View file

@ -49,35 +49,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
ret.tab = 'walletHome';
var vanillaScope = ret;
if (ret.usePushNotifications) {
// Listening for push notifications
var push = PushNotification.init(configService.getDefaults().pushNotifications.config);
push.on('notification', function(data) {
if (!data.additionalData.foreground) {
window.ignoreMobilePause = true;
// window.plugins.spinnerDialog.show(null, gettextCatalog.getString('LOADING...'), true);
$log.debug('Push notification event: ', data.message);
$timeout(function() {
var wallets = profileService.getWallets();
var walletToFind = data.additionalData.walletId;
var walletFound = lodash.find(wallets, function(w) {
return (lodash.isEqual(walletToFind, sjcl.hash.sha256.hash(w.id)));
});
if (!walletFound) return $log.debug('Wallet not found');
profileService.setAndStoreFocus(walletFound.id, function() {
// $timeout(function() {
// window.plugins.spinnerDialog.hide();
// }, 200);
});
}, 100);
}
});
}
function strip(number) {
return (parseFloat(number.toPrecision(12)));
};
@ -1407,10 +1378,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
});
});
$rootScope.$on('Local/pushNotificationsReady', function(event) {
pushNotificationsService.enableNotifications(profileService.walletClients);
});
self.debouncedUpdate = lodash.throttle(function() {
self.updateAll({
quiet: true

View file

@ -1,9 +1,10 @@
'use strict';
angular.module('copayApp.services')
.factory('profileService', function profileServiceFactory($rootScope, $location, $timeout, $filter, $log, lodash, storageService, bwcService, configService, notificationService, pushNotificationsService, isChromeApp, isCordova, gettext, gettextCatalog, nodeWebkit, bwsError, uxLanguage, bitcore) {
.factory('profileService', function profileServiceFactory($rootScope, $location, $timeout, $filter, $log, sjcl, lodash, storageService, bwcService, configService, notificationService, pushNotificationsService, isChromeApp, isCordova, gettext, gettextCatalog, nodeWebkit, bwsError, uxLanguage, bitcore) {
var root = {};
var errors = bwcService.getErrors();
var usePushNotifications = isCordova && !isMobile.Windows();
var FOREGROUND_UPDATE_PERIOD = 5;
var BACKGROUND_UPDATE_PERIOD = 30;
@ -133,22 +134,52 @@ angular.module('copayApp.services')
if (err) return cb(err);
root._setFocus(focusedWalletId, function() {
$rootScope.$emit('Local/ProfileBound');
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();
}
});
if (usePushNotifications)
root.pushNotificationsInit();
root.isDisclaimerAccepted(function(val) {
if (!val) {
return cb(new Error('NONAGREEDDISCLAIMER: Non agreed disclaimer'));
} else {
return cb();
}
});
});
});
});
};
root.pushNotificationsInit = function() {
var defaults = configService.getDefaults();
var push = PushNotification.init(defaults.pushNotifications.config);
push.on('registration', function(data) {
storageService.getDeviceToken(function(err, token) {
if (token) return;
$log.debug('Starting push notification registration');
storageService.setDeviceToken(data.registrationId, function() {
pushNotificationsService.enableNotifications(root.walletClients);
});
});
});
push.on('notification', function(data) {
if (!data.additionalData.foreground) {
window.ignoreMobilePause = true;
$log.debug('Push notification event: ', data.message);
$timeout(function() {
var wallets = root.getWallets();
var walletToFind = data.additionalData.walletId;
var walletFound = lodash.find(wallets, function(w) {
return (lodash.isEqual(walletToFind, sjcl.hash.sha256.hash(w.id)));
});
if (!walletFound) return $log.debug('Wallet not found');
root.setAndStoreFocus(walletFound.id, function() {});
}, 100);
}
});
};
@ -306,8 +337,8 @@ angular.module('copayApp.services')
// check if exist
if (lodash.find(root.profile.credentials, {
'walletId': walletData.walletId
})) {
'walletId': walletData.walletId
})) {
return cb(gettext('Cannot join the same wallet more that once'));
}
} catch (ex) {

View file

@ -2,25 +2,8 @@
angular.module('copayApp.services')
.factory('pushNotificationsService', function($http, $rootScope, $log, isMobile, storageService, configService, lodash, isCordova) {
var root = {};
var defaults = configService.getDefaults();
var usePushNotifications = isCordova && !isMobile.Windows();
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/pushNotificationsReady');
});
});
};
root.enableNotifications = function(walletsClients) {
if (!usePushNotifications) return;
@ -28,19 +11,17 @@ angular.module('copayApp.services')
if (!config.pushNotifications.enabled) return;
storageService.getDeviceToken(function(err, token) {
if (err || !token) {
$log.warn('No token available for this device. Cannot set push notifications');
return;
}
lodash.forEach(walletsClients, function(walletClient) {
var opts = {};
opts.type = isMobile.iOS() ? "ios" : isMobile.Android() ? "android" : null;
opts.token = token;
root.subscribe(opts, walletClient, function(err, response) {
if (err) $log.warn('Subscription error: ' + err.message + ': ' + JSON.stringify(opts));
if (err) $log.warn('Subscription error: ' + err.message + ': ' + JSON.stringify(opts));
else $log.debug('Subscribed to push notifications service: ' + JSON.stringify(response));
});
});