apply pin logic to modal view

This commit is contained in:
Gabriel Bazán 2017-05-09 12:36:32 -03:00
commit 9b21292a68
6 changed files with 45 additions and 361 deletions

View file

@ -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');

View file

@ -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);
// };
});

View file

@ -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);
};
});

View file

@ -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');
}
});
});

View file

@ -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() {

View file

@ -1,73 +0,0 @@
<ion-view id="pin" hide-tabs hide-back-button="action == 'check'">
<ion-nav-bar class="bar-clear">
<ion-nav-back-button>
</ion-nav-back-button>
</ion-nav-bar>
<div class="content">
<div class="block-text row">
<div class="message" ng-if="!confirmPin && !error" translate>Please enter your PIN</div>
<div class="message" ng-if="confirmPin && !error" translate>Confirm your PIN</div>
<div class="message error" ng-if="error">
<div ng-if="!expires" translate>Incorrect PIN, try again.</div>
<time ng-if="expires" translate>Try again in {{expires}}</time>
</div>
</div>
<div class="app-icon">
<i class="icon big-icon-svg">
<div class="bg"></div>
</i>
</div>
<div class="block-code">
<div class="row">
<div class="col circle-{{appName}}" ng-class="getFilledClass(1)"></div>
<div class="col circle-{{appName}}" ng-class="getFilledClass(2)"></div>
<div class="col circle-{{appName}}" ng-class="getFilledClass(3)"></div>
<div class="col circle-{{appName}}" ng-class="getFilledClass(4)"></div>
</div>
</div>
<div class="block-buttons">
<div class="row">
<div class="col" ng-click="updatePin('1')">
<div class="keyboard">1</div>
</div>
<div class="col" ng-click="updatePin('2')">
<div class="keyboard">2</div>
</div>
<div class="col" ng-click="updatePin('3')">
<div class="keyboard">3</div>
</div>
</div>
<div class="row">
<div class="col" ng-click="updatePin('4')">
<div class="keyboard">4</div>
</div>
<div class="col" ng-click="updatePin('5')">
<div class="keyboard">5</div>
</div>
<div class="col" ng-click="updatePin('6')">
<div class="keyboard">6</div>
</div>
</div>
<div class="row">
<div class="col" ng-click="updatePin('7')">
<div class="keyboard">7</div>
</div>
<div class="col" ng-click="updatePin('8')">
<div class="keyboard">8</div>
</div>
<div class="col" ng-click="updatePin('9')">
<div class="keyboard">9</div>
</div>
</div>
<div class="row">
<div class="col"></div>
<div class="col" ng-click="updatePin('0')">
<div class="">0</div>
</div>
<div class="col" ng-click="delete()">
<div class="keyboard icon ion-arrow-left-a"></div>
</div>
</div>
</div>
</div>
</ion-view>