diff --git a/src/js/controllers/lock.js b/src/js/controllers/lock.js index 4046fd8f3..8932ce8df 100644 --- a/src/js/controllers/lock.js +++ b/src/js/controllers/lock.js @@ -1,18 +1,38 @@ 'use strict'; angular.module('copayApp.controllers').controller('lockController', function($state, $scope, $timeout, $log, configService, popupService, gettextCatalog, appConfigService, fingerprintService, profileService, lodash) { + var NONE = 'none'; + var PIN = 'pin'; + var FINGERPRINT = 'fingerprint'; $scope.$on("$ionicView.beforeEnter", function(event) { var config = configService.getSync(); - $scope.fingerprintAvailable = fingerprintService.isAvailable(); + $scope.locking = config.lock.method != PIN; - $scope.usePin = { - enabled: config.lock && config.lock.method == 'pin' ? true : false - }; - $scope.useFingerprint = { - enabled: config.lock && config.lock.method == 'fingerprint' ? true : false - }; + $scope.options = [ + { + method: NONE, + label: gettextCatalog.getString('Disabled'), + value: config.lock.method == '', + }, + { + method: PIN, + label: gettextCatalog.getString('Enable PIN'), + value: config.lock.method == PIN, + needsBackup: null, + }, + ]; + if (fingerprintService.isAvailable()) { + $scope.options.push({ + method: FINGERPRINT, + label: gettextCatalog.getString('Enable Fingerprint'), + value: config.lock.method == FINGERPRINT, + needsBackup: null, + }); + } + + $scope.currentOption = lodash.find($scope.options, 'value'); processWallets(); }); @@ -25,66 +45,73 @@ angular.module('copayApp.controllers').controller('lockController', function($st if (singleLivenetWallet) { $scope.errorMsg = gettextCatalog.getString('Backup your wallet before using this function'); + disableOptsUntilBackup(); } else if (atLeastOneLivenetWallet) { $scope.errorMsg = gettextCatalog.getString('Backup all livenet wallets before using this function'); - } else $scope.errorMsg = null; + disableOptsUntilBackup(); + } else { + enableOptsAfterBackup(); + $scope.errorMsg = null; + } + + function enableOptsAfterBackup() { + $scope.options[1].needsBackup = false; + if ($scope.options[2]) $scope.options[2].needsBackup = false; + }; + + function disableOptsUntilBackup() { + $scope.options[1].needsBackup = true; + if ($scope.options[2]) $scope.options[2].needsBackup = true; + }; $timeout(function() { $scope.$apply(); }); }; - $scope.usePinChange = function() { - $scope.usePin.enabled = !$scope.usePin.enabled; - $state.transitionTo('tabs.lock.pin', { - fromSettings: true, - locking: !$scope.usePin.enabled - }).then(function() { - $timeout(function() { - $scope.usePin.enabled = !$scope.usePin.enabled; - }, 1000); + $scope.select = function(method) { + if (method == NONE) + saveConfig(); + else if (method == FINGERPRINT) { + var config = configService.getSync(); + if (config.lock.method == PIN) { + askForDisablePin(function(disablePin) { + if (disablePin) saveConfig(FINGERPRINT); + }); + } else saveConfig(FINGERPRINT); + } else if (method == PIN) { + $state.transitionTo('tabs.lock.pin', { + fromSettings: true, + locking: $scope.locking + }); + } + $timeout(function() { + $scope.$apply(); }); }; - $scope.useFingerprintChange = function() { - if ($scope.usePin.enabled) { - var message = gettextCatalog.getString('{{appName}} is protected by Pin. Are you sure you want to disable it?', { - appName: appConfigService.nameCase - }); - var okText = gettextCatalog.getString('Continue'); - var cancelText = gettextCatalog.getString('Cancel'); - popupService.showConfirm(null, message, okText, cancelText, function(ok) { - if (!ok) { - $scope.useFingerprint = { - enabled: false - }; - $timeout(function() { - $scope.$apply(); - }); - return; - } - saveConfig(); - }); - } else - saveConfig(); + function askForDisablePin(cb) { + var message = gettextCatalog.getString('{{appName}} is protected by Pin. Are you sure you want to disable it?', { + appName: appConfigService.nameCase + }); + var okText = gettextCatalog.getString('Continue'); + var cancelText = gettextCatalog.getString('Cancel'); + popupService.showConfirm(null, message, okText, cancelText, function(ok) { + if (!ok) return cb(false); + return cb(true); + }); + }; - function saveConfig() { - $scope.usePin = { - enabled: false - }; - $timeout(function() { - $scope.$apply(); - }); - var opts = { - lock: { - method: $scope.useFingerprint.enabled ? 'fingerprint' : '', - value: '', - } - }; - - configService.set(opts, function(err) { - if (err) $log.debug(err); - }); + function saveConfig(method) { + var opts = { + lock: { + method: method || '', + value: '', + } }; + + configService.set(opts, function(err) { + if (err) $log.debug(err); + }); }; }); diff --git a/www/views/lock.html b/www/views/lock.html index 344889926..d0e49e15e 100644 --- a/www/views/lock.html +++ b/www/views/lock.html @@ -6,17 +6,11 @@ - - Enable Pin - + + {{opt.label}} + -
- - Enable Fingerprint - -
- -
+
{{errorMsg}}