diff --git a/src/js/controllers/preferencesEmail.js b/src/js/controllers/preferencesEmail.js deleted file mode 100644 index c865c67dc..000000000 --- a/src/js/controllers/preferencesEmail.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict'; - -angular.module('copayApp.controllers').controller('preferencesEmailController', function($scope, $ionicHistory, $stateParams, gettextCatalog, profileService, walletService, configService) { - - $scope.wallet = profileService.getWallet($stateParams.walletId); - var walletId = $scope.wallet.credentials.walletId; - - var config = configService.getSync(); - config.emailFor = config.emailFor || {}; - $scope.emailForExist = config.emailFor && config.emailFor[walletId]; - $scope.email = { - value: config.emailFor && config.emailFor[walletId] - }; - - - $scope.save = function(val) { - var opts = { - emailFor: {} - }; - opts.emailFor[walletId] = val; - - walletService.updateRemotePreferences($scope.wallet, { - email: val, - }, function(err) { - if (err) $log.warn(err); - configService.set(opts, function(err) { - if (err) $log.warn(err); - $ionicHistory.goBack(); - }); - }); - }; -}); 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/routes.js b/src/js/routes.js index dde598bd4..6903a097b 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -276,7 +276,9 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr templateUrl: 'views/confirm.html' } }, - params: { paypro: null } + params: { + paypro: null + } }) .state('tabs.send.addressbook', { url: '/addressbook/add/:fromSendTab/:addressbookEntry', @@ -465,15 +467,6 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr } } }) - .state('tabs.preferences.preferencesEmail', { - url: '/preferencesEmail', - views: { - 'tab-settings@tabs': { - controller: 'preferencesEmailController', - templateUrl: 'views/preferencesEmail.html' - } - } - }) .state('tabs.preferences.backupWarning', { url: '/backupWarning/:from', views: { @@ -892,7 +885,9 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr templateUrl: 'views/confirm.html' } }, - params: { paypro: null } + params: { + paypro: null + } }) .state('tabs.bitpayCard.preferences', { url: '/preferences', 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/preferences.html b/www/views/preferences.html index 81fb6a19a..35b787557 100644 --- a/www/views/preferences.html +++ b/www/views/preferences.html @@ -29,14 +29,6 @@ - - Email Notifications - - Disabled - {{wallet.email}} - - -