From 9b21292a682d2830d8d36cf33e0be823d309aa80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Tue, 9 May 2017 12:36:32 -0300 Subject: [PATCH] apply pin logic to modal view --- src/js/controllers/lockSetup.js | 10 +- src/js/controllers/modals/pin.js | 64 ++++---- src/js/controllers/pin.js | 224 -------------------------- src/js/routes.js | 18 +-- src/js/services/applicationService.js | 17 +- www/views/pin.html | 73 --------- 6 files changed, 45 insertions(+), 361 deletions(-) delete mode 100644 src/js/controllers/pin.js delete mode 100644 www/views/pin.html diff --git a/src/js/controllers/lockSetup.js b/src/js/controllers/lockSetup.js index 0503a7aee..9613abaac 100644 --- a/src/js/controllers/lockSetup.js +++ b/src/js/controllers/lockSetup.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('lockSetupController', function($state, $scope, $timeout, $log, configService, gettextCatalog, fingerprintService, profileService, lodash) { +angular.module('copayApp.controllers').controller('lockSetupController', function($state, $scope, $timeout, $log, configService, gettextCatalog, fingerprintService, profileService, lodash, applicationService) { function init() { $scope.options = [ @@ -120,9 +120,7 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio function disableMethod(method) { switch (method) { case 'pin': - $state.transitionTo('tabs.pin', { - action: 'disable' - }); + applicationService.pinModal('disable'); break; case 'fingerprint': fingerprintService.check('unlockingApp', function(err) { @@ -136,9 +134,7 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio function enableMethod(method) { switch (method) { case 'pin': - $state.transitionTo('tabs.pin', { - action: 'setup' - }); + applicationService.pinModal('setup'); break; case 'fingerprint': saveConfig('fingerprint'); diff --git a/src/js/controllers/modals/pin.js b/src/js/controllers/modals/pin.js index 5d3f23a27..a580d0442 100644 --- a/src/js/controllers/modals/pin.js +++ b/src/js/controllers/modals/pin.js @@ -4,27 +4,21 @@ angular.module('copayApp.controllers').controller('pinController', function($sta var ATTEMPT_LIMIT = 3; var ATTEMPT_LOCK_OUT_TIME = 5 * 60; var currentPin; - - $scope.$on("$ionicView.beforeEnter", function(event) { - currentPin = $scope.confirmPin = ''; - $scope.action = $stateParams.action; - $scope.match = $scope.error = $scope.disableButtons = false; - $scope.currentAttempts = 0; - $scope.appName = appConfigService.name; - }); - - $scope.$on("$ionicView.enter", function(event) { - configService.whenAvailable(function(config) { - if (!config.lock) return; - $scope.bannedUntil = config.lock.bannedUntil || null; - if ($scope.bannedUntil) { - var now = Math.floor(Date.now() / 1000); - if (now < $scope.bannedUntil) { - $scope.error = $scope.disableButtons = true; - lockTimeControl($scope.bannedUntil); - } + currentPin = $scope.confirmPin = ''; + console.log("############################11111"); + $scope.match = $scope.error = $scope.disableButtons = false; + $scope.currentAttempts = 0; + $scope.appName = appConfigService.name; + configService.whenAvailable(function(config) { + if (!config.lock) return; + $scope.bannedUntil = config.lock.bannedUntil || null; + if ($scope.bannedUntil) { + var now = Math.floor(Date.now() / 1000); + if (now < $scope.bannedUntil) { + $scope.error = $scope.disableButtons = true; + lockTimeControl($scope.bannedUntil); } - }); + } }); function getSavedMethod() { @@ -75,7 +69,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta }; $scope.getFilledClass = function(limit) { - return currentPin && currentPin.length >= limit ? 'filled-' + $scope.appName : null; + return currentPin.length >= limit ? 'filled-' + $scope.appName : null; }; $scope.delete = function() { @@ -110,6 +104,8 @@ angular.module('copayApp.controllers').controller('pinController', function($sta }; $scope.save = function() { + console.log("##################################### CHECKING"); + console.log($scope.action); if (!$scope.isComplete()) return; var savedMethod = getSavedMethod(); @@ -129,7 +125,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta if (isMatch(currentPin)) { console.log("##################################### CHECKING"); applicationService.successfullUnlocked = true; - $scope.pinModal.hide(); + $scope.hideModal(); return; } showError(); @@ -179,7 +175,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta configService.set(opts, function(err) { if (err) $log.debug(err); - $scope.close(); + $scope.hideModal(); }); }; @@ -194,7 +190,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta configService.set(opts, function(err) { if (err) $log.debug(err); - $scope.close(); + $scope.hideModal(); }); }; @@ -211,14 +207,14 @@ angular.module('copayApp.controllers').controller('pinController', function($sta }); }; - $scope.close = function(delay) { - $timeout(function() { - var shouldReturn = $ionicHistory.viewHistory().backView && $ionicHistory.viewHistory().backView.stateName != 'starting'; - - if (shouldReturn) - $ionicHistory.goBack() - else - $state.go('tabs.home'); - }, delay || 1); - }; + // $scope.close = function(delay) { + // $timeout(function() { + // var shouldReturn = $ionicHistory.viewHistory().backView && $ionicHistory.viewHistory().backView.stateName != 'starting'; + // + // if (shouldReturn) + // $ionicHistory.goBack() + // else + // $state.go('tabs.home'); + // }, delay || 1); + // }; }); diff --git a/src/js/controllers/pin.js b/src/js/controllers/pin.js deleted file mode 100644 index 27fb48def..000000000 --- a/src/js/controllers/pin.js +++ /dev/null @@ -1,224 +0,0 @@ -'use strict'; - -angular.module('copayApp.controllers').controller('pinController', function($state, $interval, $stateParams, $ionicHistory, $timeout, $scope, $log, configService, appConfigService, applicationService) { - var ATTEMPT_LIMIT = 3; - var ATTEMPT_LOCK_OUT_TIME = 5 * 60; - var currentPin; - - $scope.$on("$ionicView.beforeEnter", function(event) { - currentPin = $scope.confirmPin = ''; - $scope.action = $stateParams.action; - $scope.match = $scope.error = $scope.disableButtons = false; - $scope.currentAttempts = 0; - $scope.appName = appConfigService.name; - }); - - $scope.$on("$ionicView.enter", function(event) { - configService.whenAvailable(function(config) { - if (!config.lock) return; - $scope.bannedUntil = config.lock.bannedUntil || null; - if ($scope.bannedUntil) { - var now = Math.floor(Date.now() / 1000); - if (now < $scope.bannedUntil) { - $scope.error = $scope.disableButtons = true; - lockTimeControl($scope.bannedUntil); - } - } - }); - }); - - function getSavedMethod() { - var config = configService.getSync(); - if (config.lock) return config.lock.method; - return 'none'; - }; - - function checkAttempts() { - $scope.currentAttempts += 1; - $log.debug('Attempts to unlock:', $scope.currentAttempts); - if ($scope.currentAttempts === ATTEMPT_LIMIT) { - $scope.currentAttempts = 0; - var bannedUntil = Math.floor(Date.now() / 1000) + ATTEMPT_LOCK_OUT_TIME; - saveFailedAttempt(bannedUntil); - } - }; - - function lockTimeControl(bannedUntil) { - setExpirationTime(); - - var countDown = $interval(function() { - setExpirationTime(); - }, 1000); - - function setExpirationTime() { - var now = Math.floor(Date.now() / 1000); - if (now > bannedUntil) { - if (countDown) reset(); - } else { - $scope.disableButtons = true; - var totalSecs = bannedUntil - now; - var m = Math.floor(totalSecs / 60); - var s = totalSecs % 60; - $scope.expires = ('0' + m).slice(-2) + ":" + ('0' + s).slice(-2); - } - }; - - function reset() { - $scope.expires = $scope.error = $scope.disableButtons = null; - currentPin = $scope.confirmPin = ''; - $interval.cancel(countDown); - $timeout(function() { - $scope.$apply(); - }); - return; - }; - }; - - $scope.getFilledClass = function(limit) { - return currentPin.length >= limit ? 'filled-' + $scope.appName : null; - }; - - $scope.delete = function() { - if ($scope.disableButtons) return; - if (currentPin.length > 0) { - currentPin = currentPin.substring(0, currentPin.length - 1); - $scope.error = false; - $scope.updatePin(); - } - }; - - $scope.isComplete = function() { - if (currentPin.length < 4) return false; - else return true; - }; - - $scope.updatePin = function(value) { - if ($scope.disableButtons) return; - $scope.error = false; - if (value && !$scope.isComplete()) { - currentPin = currentPin + value; - $timeout(function() { - $scope.$apply(); - }); - } - $scope.save(); - }; - - function isMatch(pin) { - var config = configService.getSync(); - return config.lock.value == pin; - }; - - $scope.save = function() { - if (!$scope.isComplete()) return; - var savedMethod = getSavedMethod(); - - switch ($scope.action) { - case 'setup': - applyAndCheckPin(); - break; - case 'disable': - if (isMatch(currentPin)) { - deletePin(); - } else { - showError(); - checkAttempts(); - } - break; - case 'check': - if (isMatch(currentPin)) { - console.log("##################################### CHECKING"); - applicationService.successfullUnlocked = true; - $scope.close(); - return; - } - showError(); - checkAttempts(); - break; - } - }; - - function showError() { - $timeout(function() { - $scope.confirmPin = currentPin = ''; - $scope.error = true; - }, 200); - - $timeout(function() { - $scope.$apply(); - }); - }; - - function applyAndCheckPin() { - if (!$scope.confirmPin) { - $timeout(function() { - $scope.confirmPin = currentPin; - currentPin = ''; - }, 200); - } else { - if ($scope.confirmPin == currentPin) - savePin($scope.confirmPin); - else { - $scope.confirmPin = currentPin = ''; - $scope.error = true; - } - } - $timeout(function() { - $scope.$apply(); - }); - }; - - function deletePin() { - var opts = { - lock: { - method: 'none', - value: null, - bannedUntil: null, - } - }; - - configService.set(opts, function(err) { - if (err) $log.debug(err); - $scope.close(); - }); - }; - - function savePin(value) { - var opts = { - lock: { - method: 'pin', - value: value, - bannedUntil: null, - } - }; - - configService.set(opts, function(err) { - if (err) $log.debug(err); - $scope.close(); - }); - }; - - function saveFailedAttempt(bannedUntil) { - var opts = { - lock: { - bannedUntil: bannedUntil, - } - }; - - configService.set(opts, function(err) { - if (err) $log.debug(err); - lockTimeControl(bannedUntil); - }); - }; - - $scope.close = function(delay) { - $timeout(function() { - var shouldReturn = $ionicHistory.viewHistory().backView && $ionicHistory.viewHistory().backView.stateName != 'starting'; - - if (shouldReturn) - $ionicHistory.goBack() - else - $state.go('tabs.home'); - }, delay || 1); - }; -}); diff --git a/src/js/routes.js b/src/js/routes.js index 437868ccb..4b1cdf416 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -119,18 +119,6 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr } }) - /* - * - * Pin - * - */ - - .state('pin', { - url: '/pin/:action', - controller: 'pinController', - templateUrl: 'views/pin.html', - }) - /* * * Locked @@ -1245,7 +1233,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr $ionicPlatform.on('resume', function() { applicationService.successfullUnlocked = false; - applicationService.pinModal(); + applicationService.pinModal('check'); // checkAndApplyLock(true); }); @@ -1319,13 +1307,11 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr $log.debug(' fromParams:' + JSON.stringify(fromParams || {})); configService.whenAvailable(function(config) { var lockMethod = config.lock && config.lock.method; - console.log(lockMethod); - console.log("########################"); if (!lockMethod || lockMethod == 'none') return; if (!applicationService.successfullUnlocked && !applicationService.pinIsOpen) { console.log("################################# OPEN PIN MODAL"); - applicationService.pinModal(); + applicationService.pinModal('check'); } }); }); diff --git a/src/js/services/applicationService.js b/src/js/services/applicationService.js index bcefb8f55..5c7bee379 100644 --- a/src/js/services/applicationService.js +++ b/src/js/services/applicationService.js @@ -36,25 +36,28 @@ angular.module('copayApp.services') } }; - root.pinModal = function() { + root.pinModal = function(action) { root.pinIsOpen = true; root.successfullUnlocked = false; var scope = $rootScope.$new(true); - $ionicModal.fromTemplateUrl('views/modals/pintestview.html', { + console.log(action); + console.log("###########################111"); + scope.action = action; + $ionicModal.fromTemplateUrl('views/modals/pin.html', { scope: scope, animation: 'slide-in-up', backdropClickToClose: false, hardwareBackButtonClose: false }).then(function(modal) { - scope.pintestview = modal; - scope.pintestview.show(); + scope.pinModal = modal; + scope.openModal(); }); scope.openModal = function() { - scope.modal.show(); + scope.pinModal.show(); }; - scope.closeModal = function() { - scope.modal.hide(); + scope.hideModal = function() { + scope.pinModal.hide(); }; // Cleanup the modal when we're done with it! scope.$on('$destroy', function() { diff --git a/www/views/pin.html b/www/views/pin.html deleted file mode 100644 index ab1b67dec..000000000 --- a/www/views/pin.html +++ /dev/null @@ -1,73 +0,0 @@ - - - - - -
-
-
Please enter your PIN
-
Confirm your PIN
-
-
Incorrect PIN, try again.
- -
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
1
-
-
-
2
-
-
-
3
-
-
-
-
-
4
-
-
-
5
-
-
-
6
-
-
-
-
-
7
-
-
-
8
-
-
-
9
-
-
-
-
-
-
0
-
-
-
-
-
-
-
-