From 2dace508162fcf1431bfa8e84bb629aea9113d97 Mon Sep 17 00:00:00 2001 From: Javier Date: Mon, 31 Oct 2016 14:08:52 -0300 Subject: [PATCH] move to notification settings --- .../controllers/preferencesNotifications.js | 128 ++++++++++++------ src/js/services/configService.js | 4 + src/js/services/emailService.js | 40 ++++++ www/views/preferencesNotifications.html | 31 ++++- www/views/tab-settings.html | 2 +- 5 files changed, 161 insertions(+), 44 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..f273bc403 100644 --- a/src/js/controllers/preferencesNotifications.js +++ b/src/js/controllers/preferencesNotifications.js @@ -1,49 +1,97 @@ '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, $timeout, $window, lodash, configService, platformInfo, pushNotificationsService, profileService, emailService) { + var updateConfig = function() { + var config = configService.getSync(); + $scope.appName = $window.appConfig.nameCase; + $scope.PNEnabledByUser = true; + $scope.usePushNotifications = platformInfo.isCordova && !platformInfo.isWP; + $scope.isIOSApp = platformInfo.isIOS && platformInfo.isCordova; - var updateConfig = function() { - - var config = configService.getSync(); - var isCordova = platformInfo.isCordova; - var isIOS = platformInfo.isIOS; - - $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.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.latestEmail = { + value: getLatestEmailConfig() }; - $scope.$on("$ionicView.enter", function(event, data) { - updateConfig(); + $scope.newEmail = lodash.clone($scope.latestEmail); + var isEmailEnabled = config.emailNotifications ? config.emailNotifications.enabled : false; + + $scope.emailNotifications = { + value: isEmailEnabled && $scope.newEmail.value ? true : false + }; + + $timeout(function() { + $scope.$apply(); }); + }; + + $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.emailNotificationsChange = function() { + var opts = { + emailNotifications: { + enabled: $scope.emailNotifications.value + } + }; + configService.set(opts, function(err) { + if (err) $log.debug(err); + }); + + $scope.latestEmail = { + value: getLatestEmailConfig() + }; + + $scope.newEmail = lodash.clone($scope.latestEmail); + + if (!$scope.emailNotifications.value) { + emailService.enableEmailNotifications({ + enabled: $scope.emailNotifications.value, + email: null + }); + } + $timeout(function() { + $scope.$apply(); + }); + }; + + $scope.save = function() { + emailService.enableEmailNotifications({ + enabled: $scope.emailNotifications.value, + email: $scope.newEmail.value + }); + + $scope.latestEmail = { + value: $scope.newEmail.value + }; + + $timeout(function() { + $scope.$apply(); + }); + }; + + function getLatestEmailConfig() { + var config = configService.getSync(); + return config.emailFor ? lodash.values(config.emailFor)[0] : null; + }; + + $scope.$on("$ionicView.enter", function(event, data) { + updateConfig(); }); +}); diff --git a/src/js/services/configService.js b/src/js/services/configService.js index c766c0582..618118e35 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: false, + }, }; var configCache = null; diff --git a/src/js/services/emailService.js b/src/js/services/emailService.js new file mode 100644 index 000000000..4712c77e4 --- /dev/null +++ b/src/js/services/emailService.js @@ -0,0 +1,40 @@ +'use strict'; + +angular.module('copayApp.services').factory('emailService', function($log, configService, profileService, lodash, walletService) { + var root = {}; + + root.enableEmailNotifications = function(opts) { + opts = opts || {}; + + var wallets = profileService.getWallets(); + var keys = lodash.map(wallets, function(w) { + return w.credentials.walletId; + }); + + lodash.each(wallets, function(w) { + walletService.updateRemotePreferences(w, { + email: opts.enabled ? opts.email : null + }, function(err) { + if (err) $log.warn(err); + }); + }); + + var config = configService.getSync(); + if (!config.emailFor) + config.emailFor = {}; + + lodash.each(keys, function(k) { + config.emailFor[k] = opts.email; + }); + + if (!opts.enabled) return; + + configService.set({ + emailFor: config.emailFor + }, function(err) { + if (err) $log.debug(err); + }); + }; + + return root; +}); diff --git a/www/views/preferencesNotifications.html b/www/views/preferencesNotifications.html index 7385fd380..527b99edc 100644 --- a/www/views/preferencesNotifications.html +++ b/www/views/preferencesNotifications.html @@ -7,15 +7,40 @@
-
+
Notifications
- + Enable push notifications + + + Enable email notifications + + +
+
+
+ You'll receive email notifications about payments sent and received from {{wallet.name}}. +
+
+ +
+
+ +
+ +
+
-
+
Notifications
Push notifications for {{appName}} are currently disabled. Enable them in the Settings app. 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
- +