From 410d35495b8539733aa0fc805b39ab6d90742622 Mon Sep 17 00:00:00 2001 From: Javier Date: Fri, 28 Oct 2016 10:13:40 -0300 Subject: [PATCH] add toggle for email notifications --- .../controllers/preferencesNotifications.js | 93 +++++++++++-------- src/js/controllers/tab-settings.js | 10 +- src/js/services/configService.js | 4 + src/js/services/emailService.js | 36 +++++++ www/views/preferencesNotifications.html | 6 +- www/views/tab-settings.html | 2 +- 6 files changed, 101 insertions(+), 50 deletions(-) create mode 100644 src/js/services/emailService.js diff --git a/src/js/controllers/preferencesNotifications.js b/src/js/controllers/preferencesNotifications.js index 5cf80a4af..3f8d43d2b 100644 --- a/src/js/controllers/preferencesNotifications.js +++ b/src/js/controllers/preferencesNotifications.js @@ -1,49 +1,62 @@ 'use strict'; -angular.module('copayApp.controllers').controller('preferencesNotificationsController', - function($scope, $rootScope, $log, $window, lodash, configService, uxLanguage, platformInfo, pushNotificationsService, profileService, feeService) { +angular.module('copayApp.controllers').controller('preferencesNotificationsController', function($scope, $log, $window, configService, platformInfo, pushNotificationsService, profileService, emailService) { + var updateConfig = function() { + $scope.appName = $window.appConfig.nameCase; + $scope.PNEnabledByUser = true; + $scope.usePushNotifications = platformInfo.isCordova && !platformInfo.isWP; + $scope.isIOSApp = platformInfo.isIOS && platformInfo.isCordova; - var updateConfig = function() { + if ($scope.isIOSApp) { + try { + PushNotification.hasPermission(function(data) { + $scope.PNEnabledByUser = data.isEnabled; + }); + } catch (e) { + $log.error(e); + }; + } - var config = configService.getSync(); - var isCordova = platformInfo.isCordova; - var isIOS = platformInfo.isIOS; + var config = configService.getSync(); - $scope.appName = $window.appConfig.nameCase; - $scope.PNEnabledByUser = true; - $scope.isIOSApp = isIOS && isCordova; - if ($scope.isIOSApp) { - try { - PushNotification.hasPermission(function(data) { - $scope.PNEnabledByUser = data.isEnabled; - }); - } catch(e) { - $log.error(e); - }; + $scope.pushNotifications = { + value: config.pushNotifications.enabled + }; + + $scope.emailNotifications = { + value: config.emailNotifications ? config.emailNotifications.enabled : false + }; + }; + + $scope.pushNotificationsChange = function() { + if (!$scope.pushNotifications) return; + var opts = { + pushNotifications: { + enabled: $scope.pushNotifications.value } - - $scope.pushNotifications = { - value: config.pushNotifications.enabled - }; }; - - $scope.pushNotificationsChange = function() { - if (!$scope.pushNotifications) return; - var opts = { - pushNotifications: { - enabled: $scope.pushNotifications.value - } - }; - configService.set(opts, function(err) { - if (opts.pushNotifications.enabled) - profileService.pushNotificationsInit(); - else - pushNotificationsService.disableNotifications(profileService.getWallets()); - if (err) $log.debug(err); - }); - }; - - $scope.$on("$ionicView.enter", function(event, data) { - updateConfig(); + configService.set(opts, function(err) { + if (opts.pushNotifications.enabled) + profileService.pushNotificationsInit(); + else + pushNotificationsService.disableNotifications(profileService.getWallets()); + if (err) $log.debug(err); }); + }; + + $scope.emailNotificationsChange = function() { + var opts = { + emailNotifications: { + enabled: $scope.emailNotifications.value + } + }; + emailService.enableEmailNotifications($scope.emailNotifications.value); + configService.set(opts, function(err) { + if (err) $log.debug(err); + }); + }; + + $scope.$on("$ionicView.enter", function(event, data) { + updateConfig(); }); +}); diff --git a/src/js/controllers/tab-settings.js b/src/js/controllers/tab-settings.js index 773a6a5f3..1022a77de 100644 --- a/src/js/controllers/tab-settings.js +++ b/src/js/controllers/tab-settings.js @@ -1,17 +1,11 @@ 'use strict'; -angular.module('copayApp.controllers').controller('tabSettingsController', function($scope, $window, uxLanguage, platformInfo, profileService, feeService, configService, externalLinkService) { +angular.module('copayApp.controllers').controller('tabSettingsController', function($scope, $window, uxLanguage, profileService, feeService, configService, externalLinkService) { var updateConfig = function() { - - var config = configService.getSync(); - var isCordova = platformInfo.isCordova; - var isWP = platformInfo.isWP; - - $scope.usePushNotifications = isCordova && !isWP; - $scope.appName = $window.appConfig.nameCase; + var config = configService.getSync(); $scope.unitName = config.wallet.settings.unitName; $scope.currentLanguageName = uxLanguage.getCurrentLanguageName(); $scope.selectedAlternative = { diff --git a/src/js/services/configService.js b/src/js/services/configService.js index c766c0582..b3397b162 100644 --- a/src/js/services/configService.js +++ b/src/js/services/configService.js @@ -85,6 +85,10 @@ angular.module('copayApp.services').factory('configService', function(storageSer windows: {}, } }, + + emailNotifications: { + enabled: true, + }, }; var configCache = null; diff --git a/src/js/services/emailService.js b/src/js/services/emailService.js new file mode 100644 index 000000000..63d940214 --- /dev/null +++ b/src/js/services/emailService.js @@ -0,0 +1,36 @@ +'use strict'; + +angular.module('copayApp.services').factory('emailService', function($log, configService, profileService, lodash, walletService) { + var root = {}; + + root.enableEmailNotifications = function(val) { + val = val || false; + + var config = configService.getSync(); + + if (!config.emailFor) { + $log.debug('No email configuration available'); + return; + } + + var keys = lodash.keys(config.emailFor); + var wallets = lodash.map(keys, function(k) { + return profileService.getWallet(k); + }); + + if (!wallets) { + $log.debug('No wallets found'); + return; + } + + lodash.each(wallets, function(w) { + walletService.updateRemotePreferences(w, { + email: val ? config.emailFor[w.credentials.walletId] : null + }, function(err) { + if (err) $log.warn(err); + }); + }); + }; + + return root; +}); diff --git a/www/views/preferencesNotifications.html b/www/views/preferencesNotifications.html index 7385fd380..bb6a97e18 100644 --- a/www/views/preferencesNotifications.html +++ b/www/views/preferencesNotifications.html @@ -10,9 +10,13 @@
Notifications
- + Enable push notifications + + + Enable email notifications +
diff --git a/www/views/tab-settings.html b/www/views/tab-settings.html index f624537e4..075ff0a81 100644 --- a/www/views/tab-settings.html +++ b/www/views/tab-settings.html @@ -39,7 +39,7 @@
Preferences
- +