refactor
This commit is contained in:
parent
1d3e74164d
commit
a53ba1fea0
2 changed files with 25 additions and 11 deletions
|
|
@ -5,7 +5,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
var SOFT_CONFIRMATION_LIMIT = 12;
|
||||
self.isCordova = isCordova;
|
||||
self.isChromeApp = isChromeApp;
|
||||
self.pushDeviceType = isMobile.iOS() || isMobile.Android();
|
||||
self.usePushNotifications = isMobile.iOS() || isMobile.Android();
|
||||
self.isSafari = isMobile.Safari();
|
||||
self.onGoingProcess = {};
|
||||
self.historyShowLimit = 10;
|
||||
|
|
@ -13,13 +13,13 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
self.prevState = 'walletHome';
|
||||
|
||||
document.addEventListener('deviceready', function() {
|
||||
if (self.pushDeviceType) {
|
||||
storageService.getDeviceToken(function(err, token) {
|
||||
$timeout(function() {
|
||||
if (!token) pushNotificationsService.pushNotificationsInit();
|
||||
}, 5000);
|
||||
});
|
||||
}
|
||||
if (!self.usePushNotifications) return;
|
||||
|
||||
storageService.getDeviceToken(function(err, token) {
|
||||
$timeout(function() {
|
||||
if (!token) pushNotificationsService.pushNotificationsInit();
|
||||
}, 5000);
|
||||
});
|
||||
});
|
||||
|
||||
function strip(number) {
|
||||
|
|
@ -1286,12 +1286,15 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
});
|
||||
|
||||
$rootScope.$on('Local/SubscribeNotifications', function(event) {
|
||||
if (self.pushDeviceType) {
|
||||
pushNotificationsService.enableNotifications();
|
||||
}
|
||||
if (!self.usePushNotifications) return;
|
||||
|
||||
pushNotificationsService.enableNotifications();
|
||||
|
||||
});
|
||||
|
||||
$rootScope.$on('Local/UnsubscribeNotifications', function(event, walletId, cb) {
|
||||
if (self.usePushNotifications) return cb();
|
||||
|
||||
pushNotificationsService.unsubscribe(walletId, function(err, response) {
|
||||
if (err) $log.warn('Error: ' + err.code);
|
||||
$log.debug('Unsubscribed: ' + response);
|
||||
|
|
|
|||
|
|
@ -3,8 +3,11 @@ angular.module('copayApp.services')
|
|||
.factory('pushNotificationsService', function($http, $log, isMobile, profileService, storageService, configService, lodash) {
|
||||
var root = {};
|
||||
var defaults = configService.getDefaults();
|
||||
var usePushNotifications = isMobile.iOS() || isMobile.Android();
|
||||
|
||||
root.pushNotificationsInit = function() {
|
||||
if (!usePushNotifications) return;
|
||||
|
||||
var push = PushNotification.init(defaults.pushNotifications.config);
|
||||
|
||||
push.on('registration', function(data) {
|
||||
|
|
@ -31,6 +34,8 @@ angular.module('copayApp.services')
|
|||
};
|
||||
|
||||
root.enableNotifications = function() {
|
||||
if (!usePushNotifications) return;
|
||||
|
||||
storageService.getDeviceToken(function(err, token) {
|
||||
lodash.forEach(profileService.getWallets('testnet'), function(wallet) {
|
||||
var opts = {};
|
||||
|
|
@ -45,6 +50,8 @@ angular.module('copayApp.services')
|
|||
}
|
||||
|
||||
root.disableNotifications = function() {
|
||||
if (!usePushNotifications) return;
|
||||
|
||||
lodash.forEach(profileService.getWallets('testnet'), function(wallet) {
|
||||
root.unsubscribe(wallet.id, function(err, response) {
|
||||
if (err) $log.warn('Error: ' + err.code);
|
||||
|
|
@ -54,6 +61,8 @@ angular.module('copayApp.services')
|
|||
}
|
||||
|
||||
root.subscribe = function(opts, walletId, cb) {
|
||||
if (!usePushNotifications) return;
|
||||
|
||||
var walletClient = profileService.getClient(walletId);
|
||||
walletClient.pushNotificationsSubscribe(opts, function(err, resp) {
|
||||
if (err) return cb(err);
|
||||
|
|
@ -62,6 +71,8 @@ angular.module('copayApp.services')
|
|||
}
|
||||
|
||||
root.unsubscribe = function(walletId, cb) {
|
||||
if (!usePushNotifications) return;
|
||||
|
||||
var walletClient = profileService.getClient(walletId);
|
||||
walletClient.pushNotificationsUnsubscribe(function(err, resp) {
|
||||
if (err) return cb(err);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue