Wallet/src/js/controllers/preferencesNotifications.js

97 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-09-28 10:50:33 -03:00
'use strict';
2016-12-27 15:19:53 -03:00
angular.module('copayApp.controllers').controller('preferencesNotificationsController', function($scope, $log, $timeout, appConfigService, lodash, configService, platformInfo, pushNotificationsService, profileService, emailService) {
2016-10-31 14:08:52 -03:00
var updateConfig = function() {
var config = configService.getSync();
2016-12-27 15:19:53 -03:00
$scope.appName = appConfigService.nameCase;
2016-10-31 14:08:52 -03:00
$scope.PNEnabledByUser = true;
$scope.usePushNotifications = platformInfo.isCordova && !platformInfo.isWP;
$scope.isIOSApp = platformInfo.isIOS && platformInfo.isCordova;
$scope.pushNotifications = {
value: config.pushNotifications.enabled
};
$scope.latestEmail = {
value: getLatestEmailConfig()
};
$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
}
2016-10-31 14:08:52 -03:00
};
configService.set(opts, function(err) {
if (opts.pushNotifications.enabled)
profileService.pushNotificationsInit();
else
pushNotificationsService.disableNotifications(profileService.getWallets());
if (err) $log.debug(err);
});
};
2016-09-28 10:50:33 -03:00
2016-10-31 14:08:52 -03:00
$scope.emailNotificationsChange = function() {
var opts = {
emailNotifications: {
enabled: $scope.emailNotifications.value
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
});
$scope.latestEmail = {
value: getLatestEmailConfig()
2016-09-28 10:50:33 -03:00
};
2016-10-31 14:08:52 -03:00
$scope.newEmail = lodash.clone($scope.latestEmail);
if (!$scope.emailNotifications.value) {
emailService.enableEmailNotifications({
enabled: $scope.emailNotifications.value,
email: null
});
2016-10-31 14:08:52 -03:00
}
$timeout(function() {
$scope.$apply();
});
};
$scope.save = function() {
emailService.enableEmailNotifications({
enabled: $scope.emailNotifications.value,
email: $scope.newEmail.value
});
$scope.latestEmail = {
value: $scope.newEmail.value
2016-10-28 10:13:40 -03:00
};
2016-10-31 14:08:52 -03:00
$timeout(function() {
$scope.$apply();
2016-10-28 10:13:40 -03:00
});
2016-10-31 14:08:52 -03:00
};
function getLatestEmailConfig() {
var config = configService.getSync();
return config.emailFor ? lodash.values(config.emailFor)[0] : null;
};
$scope.$on("$ionicView.enter", function(event, data) {
updateConfig();
2016-09-28 10:50:33 -03:00
});
2016-10-31 14:08:52 -03:00
});