diff --git a/src/js/controllers/advancedSettings.js b/src/js/controllers/advancedSettings.js index 826ae3c81..a34e6b749 100644 --- a/src/js/controllers/advancedSettings.js +++ b/src/js/controllers/advancedSettings.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('advancedSettingsController', function($scope, $rootScope, $log, $window, $ionicModal, lodash, configService, uxLanguage, platformInfo, pushNotificationsService, profileService, feeService, storageService, $ionicHistory, $timeout, $ionicScrollDelegate) { +angular.module('copayApp.controllers').controller('advancedSettingsController', function($scope, $rootScope, $log, $window, $ionicModal, lodash, configService, uxLanguage, platformInfo, pushNotificationsService, profileService, feeService, storageService, $ionicHistory, $ionicScrollDelegate, pincodeService) { var updateConfig = function() { var config = configService.getSync(); @@ -15,7 +15,7 @@ angular.module('copayApp.controllers').controller('advancedSettingsController', value: config.hideNextSteps.enabled }; $scope.usePincode = { - value: config.pincode ? config.pincode.enabled : false + enabled: config.pincode ? config.pincode.enabled : false }; }; @@ -41,23 +41,6 @@ angular.module('copayApp.controllers').controller('advancedSettingsController', }); }; - $scope.savePincodeChanges = function(val) { - if (!val || val.length < 4) { - $scope.usePincode = { - value: false - } - return; - } - var opts = { - usePincode: { - enabled: $scope.usePincode.enabled - }, - }; - configService.set(opts, function(err) { - if (err) $log.debug(err); - }); - }; - $scope.recentTransactionsChange = function() { var opts = { recentTransactions: { @@ -69,15 +52,20 @@ angular.module('copayApp.controllers').controller('advancedSettingsController', }); }; - $scope.showPincodeModal = function() { - $scope.fromSettings = true; - $ionicModal.fromTemplateUrl('views/modals/pincode.html', { - scope: $scope, - backdropClickToClose: false, - hardwareBackButtonClose: false - }).then(function(modal) { - $scope.pincodeModal = modal; - $scope.pincodeModal.show(); + $scope.usePincodeChange = function() { + var opts = {}; + opts.enabled = $scope.usePincode.enabled; + opts.from = 'settings'; + + pincodeService.lockChange(opts, function(err) { + if (err) { + $log.warn(err); + + // ToDo show error? + $scope.usePincode.enabled = false; + $scope.$apply(); + return; + } }); }; diff --git a/src/js/controllers/pincode.js b/src/js/controllers/pincode.js index 53c3da987..d7beadfa4 100644 --- a/src/js/controllers/pincode.js +++ b/src/js/controllers/pincode.js @@ -1,7 +1,11 @@ 'use strict'; angular.module('copayApp.controllers').controller('pincodeController', function($timeout, $scope, $log, $window, configService) { - $scope.pincode = ''; + var config = configService.getSync(); + $scope.currentPincode = config.pincode ? config.pincode.value : null; + $scope.pincode = $scope.pc1 = $scope.pc2 = ''; + + console.log('#######', $scope.from, $scope.enabled); angular.element($window).on('keydown', function(e) { if (e.which === 8) { // you can add others here inside brackets. @@ -9,15 +13,16 @@ angular.module('copayApp.controllers').controller('pincodeController', function( $scope.delete(); } - if (e.key && e.key.match(/^[0-9]$/)) + if (e && e.key.match(/^[0-9]$/)) $scope.add(e.key); - else if (e.key && e.key == 'Enter') - checkPasscode(); + else if (e && e.keyCode == 27) + $scope.close(false); + else if (e && e.keyCode == 13) + $scope.save(); }); $scope.add = function(value) { - if (isComplete()) checkPasscode(); - else updatePassCode(value); + updatePassCode(value); }; $scope.delete = function() { @@ -33,24 +38,36 @@ angular.module('copayApp.controllers').controller('pincodeController', function( }; function updatePassCode(value) { - if (value) $scope.pincode = $scope.pincode + value; + if (value && $scope.pincode.length < 4) + $scope.pincode = $scope.pincode + value; $timeout(function() { - checkPasscode(); $scope.$apply(); }); }; - function checkPasscode() { - console.log('Checking'); - configService.whenAvailable(function(config) { - var value = '1234'; - if (value != $scope.pincode) return; - console.log('MATCH'); - }); + $scope.save = function() { + if (!$scope.pc1) { + console.log('No pc 1'); + $scope.pc1 = $scope.pincode; + console.log('$scope.pc1', $scope.pc1); + $scope.pincode = ''; + $timeout(function() { + $scope.$apply(); + }); + return; + } else { + $scope.pc2 = $scope.pincode; + console.log('$scope.pc2', $scope.pc2); + } + + if ($scope.pc1 == $scope.pc2) { + $scope.close($scope.pc1); + } else { + $scope.enabled ? pincodeService.lockApp() : pincodeService.unlockApp(); + } }; - $scope.cancel = function() { - $scope.savePincodeChanges(false); + $scope.close = function() { $scope.pincodeModal.hide(); }; }); diff --git a/src/js/services/pincodeService.js b/src/js/services/pincodeService.js new file mode 100644 index 000000000..d6b244e20 --- /dev/null +++ b/src/js/services/pincodeService.js @@ -0,0 +1,32 @@ +'use strict'; + +angular.module('copayApp.services').factory('pincodeService', function($log, $rootScope, $ionicModal, configService) { + var root = {}; + + var openPincodeModal = function(opts) { + var scope = $rootScope.$new(true); + scope.from = opts.from; + scope.enabled = opts.enabled; + $ionicModal.fromTemplateUrl('views/modals/pincode.html', { + scope: scope, + backdropClickToClose: false, + hardwareBackButtonClose: false + }).then(function(modal) { + scope.pincodeModal = modal; + scope.pincodeModal.show(); + }); + }; + + root.lockChange = function(opts, cb) { + if (opts.enabled) console.log('Locking app from service'); + else console.log('Unlocking app from service'); + openPincodeModal(opts); + }; + + root.isLocked = function() { + var config = configService.getSync(); + return config.pincode ? config.pincode.enabled : false; + }; + + return root; +}); diff --git a/www/views/advancedSettings.html b/www/views/advancedSettings.html index 26566061b..8a8208587 100644 --- a/www/views/advancedSettings.html +++ b/www/views/advancedSettings.html @@ -31,7 +31,7 @@
-