From 8be28e85dace3ca2bd1864e88ce21a30e938580e Mon Sep 17 00:00:00 2001 From: JDonadio Date: Tue, 18 Apr 2017 13:19:16 -0300 Subject: [PATCH] refactor --- src/js/controllers/lockSetup.js | 46 +++++++++++++------------ src/js/controllers/pin.js | 4 +-- src/js/controllers/tab-settings.js | 4 +-- src/js/routes.js | 26 +++++++------- src/js/services/configService.js | 4 +-- www/views/{lock.html => lockSetup.html} | 2 +- www/views/tab-settings.html | 2 +- 7 files changed, 45 insertions(+), 43 deletions(-) rename www/views/{lock.html => lockSetup.html} (88%) diff --git a/src/js/controllers/lockSetup.js b/src/js/controllers/lockSetup.js index 6a53bff7b..b9ad3a431 100644 --- a/src/js/controllers/lockSetup.js +++ b/src/js/controllers/lockSetup.js @@ -1,21 +1,16 @@ 'use strict'; -angular.module('copayApp.controllers').controller('lockController', function($state, $scope, $timeout, $log, configService, popupService, gettextCatalog, appConfigService, fingerprintService, profileService, lodash) { +angular.module('copayApp.controllers').controller('lockSetupController', function($state, $scope, $timeout, $log, configService, popupService, gettextCatalog, appConfigService, fingerprintService, profileService, lodash) { function init() { - var config = configService.getSync(); - $scope.locking = config.lock && config.lock.method == 'pin' ? false : true; - $scope.options = [ { - method: '', + method: null, label: gettextCatalog.getString('Disabled'), - selected: config.lock && config.lock.method == '' ? true : false, }, { method: 'pin', - label: gettextCatalog.getString('Enable PIN'), - selected: config.lock && config.lock.method == 'pin' ? true : false, + label: gettextCatalog.getString('Lock by PIN'), needsBackup: null, }, ]; @@ -23,13 +18,17 @@ angular.module('copayApp.controllers').controller('lockController', function($st if (fingerprintService.isAvailable()) { $scope.options.push({ method: 'fingerprint', - label: gettextCatalog.getString('Enable Fingerprint'), - selected: config.lock && config.lock.method == 'fingerprint' ? true : false, + label: gettextCatalog.getString('Lock by Fingerprint'), needsBackup: null, }); } - $scope.currentOption = lodash.find($scope.options, 'selected') || $scope.options[0]; + var config = configService.getSync(); + var method = config.lock && config.lock.method; + if (!method) $scope.currentOption = $scope.options[0]; + else $scope.currentOption = lodash.find($scope.options, { + 'method': method + }); processWallets(); }; @@ -70,21 +69,24 @@ angular.module('copayApp.controllers').controller('lockController', function($st }); }; - $scope.select = function(method) { - if (method == '') + $scope.select = function(selectedMethod) { + var config = configService.getSync(); + var savedMethod = config.lock && config.lock.method; + + if (!selectedMethod) saveConfig(); - else if (method == 'fingerprint') { - var config = configService.getSync(); - if (config.lock.method == 'pin') { + else if (selectedMethod == 'fingerprint') { + if (savedMethod == 'pin') { askForDisablePin(function(disablePin) { if (disablePin) saveConfig('fingerprint'); else init(); }); } else saveConfig('fingerprint'); - } else if (method == 'pin') { - $state.transitionTo('tabs.lock.pin', { + } else if (selectedMethod == 'pin') { + if (savedMethod == 'pin') return; + $state.transitionTo('tabs.pin', { fromSettings: true, - locking: $scope.locking + locking: savedMethod == 'pin' ? false : true }); } $timeout(function() { @@ -93,7 +95,7 @@ angular.module('copayApp.controllers').controller('lockController', function($st }; function askForDisablePin(cb) { - var message = gettextCatalog.getString('{{appName}} is protected by Pin. Are you sure you want to disable it?', { + var message = gettextCatalog.getString('{{appName}} startup is locked by PIN. Are you sure you want to disable it?', { appName: appConfigService.nameCase }); var okText = gettextCatalog.getString('Continue'); @@ -107,8 +109,8 @@ angular.module('copayApp.controllers').controller('lockController', function($st function saveConfig(method) { var opts = { lock: { - method: method || '', - value: '', + method: method || null, + value: null, } }; diff --git a/src/js/controllers/pin.js b/src/js/controllers/pin.js index ef48c0198..9abadb441 100644 --- a/src/js/controllers/pin.js +++ b/src/js/controllers/pin.js @@ -160,8 +160,8 @@ angular.module('copayApp.controllers').controller('pinController', function($sta var attempts = config.lock && config.lock.attempts ? config.lock.attempts : 0; var opts = { lock: { - method: method || '', - value: value || '', + method: method || null, + value: value || null, bannedUntil: null, attempts: attempts + 1, } diff --git a/src/js/controllers/tab-settings.js b/src/js/controllers/tab-settings.js index 80152ca61..843059c84 100644 --- a/src/js/controllers/tab-settings.js +++ b/src/js/controllers/tab-settings.js @@ -54,8 +54,8 @@ angular.module('copayApp.controllers').controller('tabSettingsController', funct $scope.isDevel = platformInfo.isDevel; $scope.appName = appConfigService.nameCase; configService.whenAvailable(function(config) { - $scope.locked = config.lock && config.lock.method != '' ? true : false; - $scope.method = config.lock && config.lock.method != '' ? config.lock.method.charAt(0).toUpperCase() + config.lock.method.slice(1) : gettextCatalog.getString('Disabled'); + $scope.locked = config.lock && config.lock.method; + $scope.method = $scope.locked ? config.lock.method.charAt(0).toUpperCase() + config.lock.method.slice(1) : gettextCatalog.getString('Disabled'); }); }); diff --git a/src/js/routes.js b/src/js/routes.js index 99c16fbb7..da09f3a7b 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -1206,41 +1206,44 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr }); - function checkAndApplyLock(defaultView) { + function checkAndApplyLock(onResume) { + var defaultView = 'tabs.home'; if (!platformInfo.isCordova && !platformInfo.isDevel) { - goTo('tabs.home'); + goTo(defaultView); } function goTo(nextView) { + nextView = nextView || defaultView; $state.transitionTo(nextView).then(function() { - if (nextView == 'lockedView') + if (nextView == 'lockedView') $ionicHistory.clearHistory(); }); }; + startupService.ready(); + configService.whenAvailable(function(config) { var lockMethod = config.lock && config.lock.method; - $log.debug('App Lock:' + (lockMethod||'no') ); + $log.debug('App Lock:' + (lockMethod || 'no')); if (lockMethod == 'fingerprint' && fingerprintService.isAvailable()) { fingerprintService.check('unlockingApp', function(err) { - if (err) + if (err) goTo('lockedView'); - - if ($ionicHistory.currentStateName() == 'lockedView') + if ($ionicHistory.currentStateName() == 'lockedView' || !onResume) goTo('tabs.home'); }); } else if (lockMethod == 'pin') { goTo('pin'); - } else if (defaultView) { + } else { goTo(defaultView); } }); } $ionicPlatform.on('resume', function() { - checkAndApplyLock(); + checkAndApplyLock(true); }); $ionicPlatform.on('menubutton', function() { @@ -1284,10 +1287,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr historyRoot: true }); - if (platformInfo.isCordova) - startupService.ready(); - - checkAndApplyLock('tabs.home'); + checkAndApplyLock(); }); }; // After everything have been loaded, initialize handler URL diff --git a/src/js/services/configService.js b/src/js/services/configService.js index 3d979af7f..2b92e58ff 100644 --- a/src/js/services/configService.js +++ b/src/js/services/configService.js @@ -54,8 +54,8 @@ angular.module('copayApp.services').factory('configService', function(storageSer }, lock: { - method: '', - value: '', + method: null, + value: null, bannedUntil: null, attempts: null, }, diff --git a/www/views/lock.html b/www/views/lockSetup.html similarity index 88% rename from www/views/lock.html rename to www/views/lockSetup.html index 821cb7310..1bab91b8b 100644 --- a/www/views/lock.html +++ b/www/views/lockSetup.html @@ -1,6 +1,6 @@ - {{'Lock App' | translate}} + {{'Startup Lock' | translate}} diff --git a/www/views/tab-settings.html b/www/views/tab-settings.html index e0bbe0e0e..cfc054614 100644 --- a/www/views/tab-settings.html +++ b/www/views/tab-settings.html @@ -89,7 +89,7 @@ - + {{'Lock App' | translate}}