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