Wallet/src/js/controllers/preferencesNotifications.js

109 lines
3 KiB
JavaScript
Raw Normal View History

2016-09-28 10:50:33 -03:00
'use strict';
angular.module('copayApp.controllers').controller('preferencesNotificationsController', function($scope, $log, $timeout, appConfigService, lodash, configService, platformInfo, pushNotificationsService, emailService, $ionicNavBarDelegate) {
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 = {
2017-02-03 18:03:29 -03:00
value: config.pushNotificationsEnabled
2016-10-31 14:08:52 -03:00
};
2017-07-14 15:45:18 -03:00
var isConfirmedTxsNotificationsEnabled = config.confirmedTxsNotifications ? config.confirmedTxsNotifications.enabled : false;
$scope.confirmedTxsNotifications = {
value: isConfirmedTxsNotificationsEnabled
};
2016-10-31 14:08:52 -03:00
$scope.latestEmail = {
value: emailService.getEmailIfEnabled()
2016-10-31 14:08:52 -03:00
};
$scope.newEmail = lodash.clone($scope.latestEmail);
var isEmailEnabled = config.emailNotifications ? config.emailNotifications.enabled : false;
$scope.emailNotifications = {
value: isEmailEnabled && $scope.newEmail.value ? true : false
};
var isSoundEnabled = config.soundsEnabled ? config.soundsEnabled : false;
$scope.sounds = {
value: isSoundEnabled
};
2016-10-31 14:08:52 -03:00
$timeout(function() {
$scope.$apply();
});
};
$scope.pushNotificationsChange = function() {
if (!$scope.pushNotifications) return;
var opts = {
2017-02-03 18:03:29 -03:00
pushNotificationsEnabled: $scope.pushNotifications.value
2016-10-31 14:08:52 -03:00
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
2017-02-03 18:03:29 -03:00
if (opts.pushNotificationsEnabled)
pushNotificationsService.init();
else
pushNotificationsService.disable();
2016-10-31 14:08:52 -03:00
});
};
2016-09-28 10:50:33 -03:00
2017-07-14 15:45:18 -03:00
$scope.confirmedTxsNotificationsChange = function() {
if (!$scope.pushNotifications) return;
var opts = {
2017-07-14 15:45:18 -03:00
confirmedTxsNotifications: {
enabled: $scope.confirmedTxsNotifications.value
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
});
};
2016-10-31 14:08:52 -03:00
$scope.emailNotificationsChange = function() {
var opts = {
enabled: $scope.emailNotifications.value,
email: $scope.newEmail.value
2016-10-31 14:08:52 -03:00
};
$scope.latestEmail = {
value: emailService.getEmailIfEnabled()
2016-09-28 10:50:33 -03:00
};
emailService.updateEmail(opts);
2016-10-31 14:08:52 -03:00
};
$scope.soundNotificationsChange = function() {
if (!$scope.sounds) return;
var opts = {
soundsEnabled: $scope.sounds.value
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
});
};
2016-10-31 14:08:52 -03:00
$scope.save = function() {
emailService.updateEmail({
2016-10-31 14:08:52 -03:00
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
};
$scope.$on("$ionicView.enter", function(event, data) {
$ionicNavBarDelegate.showBar(true);
2016-10-31 14:08:52 -03:00
updateConfig();
2016-09-28 10:50:33 -03:00
});
2016-10-31 14:08:52 -03:00
});