Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
JDonadio committed Apr 21, 2017
1 parent 6b4b675 commit dd154a3
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 100 deletions.
116 changes: 75 additions & 41 deletions src/js/controllers/lockSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,69 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
function init() {
$scope.options = [
{
method: null,
method: 'none',
label: gettextCatalog.getString('Disabled'),
disabled: false,
},
{
method: 'pin',
label: gettextCatalog.getString('Lock by PIN'),
needsBackup: null,
disabled: null,
needsBackup: false,
disabled: false,
},
];

if (fingerprintService.isAvailable()) {
$scope.options.push({
method: 'fingerprint',
label: gettextCatalog.getString('Lock by Fingerprint'),
needsBackup: null,
disabled: null,
needsBackup: false,
disabled: false,
});
}

checkAndSelectOption();
initMethodSelector();
processWallets();
};

$scope.$on("$ionicView.beforeEnter", function(event) {
init();
});

function checkAndSelectOption() {
function getSavedMethod() {
var config = configService.getSync();
var method = config.lock && config.lock.method;
if (config.lock) return config.lock.method;
return 'none';
};

if (!method) {
$scope.currentOption = $scope.options[0];
$scope.options[1].disabled = false;
$scope.options[2].disabled = false;
} else {
if (method == 'fingerprint') $scope.options[1].disabled = true;
if (method == 'pin') $scope.options[2].disabled = true;
$scope.currentOption = lodash.find($scope.options, {
'method': method
});
function initMethodSelector() {
function disable(method) {
lodash.find($scope.options, {
method: method
}).disabled = true;
};

var savedMethod = getSavedMethod();

lodash.each($scope.options, function(o) {
o.disabled = false;
});

// HACK: Disable untill we allow to change between methods directly
if (fingerprintService.isAvailable()) {
switch (savedMethod) {
case 'pin':
disable('fingerprint');
break;
case 'fingerprint':
disable('pin');
break;
}
}

$scope.currentOption = lodash.find($scope.options, {
method: savedMethod
});
$timeout(function() {
$scope.$apply();
});
Expand Down Expand Up @@ -87,42 +107,56 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
};

$scope.select = function(selectedMethod) {
var config = configService.getSync();
var savedMethod = config.lock && config.lock.method;
var savedMethod = getSavedMethod();
if (savedMethod == selectedMethod) return;

if (!selectedMethod) {
if (!savedMethod) return;
if (savedMethod == 'pin') $state.transitionTo('tabs.pin', {
fromSettings: true,
locking: false,
});
else saveConfig();
} else if (selectedMethod == 'pin') {
if (savedMethod == 'pin') return;
$state.transitionTo('tabs.pin', {
fromSettings: true,
locking: savedMethod == 'pin' ? false : true
});
} else if (selectedMethod == 'fingerprint') {
if (savedMethod == 'fingerprint') return;
else saveConfig('fingerprint');
if (selectedMethod == 'none') {
disableMethod(savedMethod);
} else {
enableMethod(selectedMethod);
}
};

function disableMethod(method) {
switch (method) {
case 'pin':
$state.transitionTo('tabs.pin', {
action: 'disable'
});
break;
case 'fingerprint':
fingerprintService.check('unlockingApp', function(err) {
if (err) init();
else saveConfig('none');
});
break;
}
};

function enableMethod(method) {
switch (method) {
case 'pin':
$state.transitionTo('tabs.pin', {
action: 'setup'
});
break;
case 'fingerprint':
saveConfig('fingerprint');
break;
}
$timeout(function() {
$scope.$apply();
});
};

function saveConfig(method) {
var opts = {
lock: {
method: method || null,
method: method,
value: null,
}
};

configService.set(opts, function(err) {
if (err) $log.debug(err);
checkAndSelectOption();
initMethodSelector();
});
};
});
138 changes: 86 additions & 52 deletions src/js/controllers/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
angular.module('copayApp.controllers').controller('pinController', function($state, $interval, $stateParams, $ionicHistory, $timeout, $scope, $log, configService, appConfigService) {
var ATTEMPT_LIMIT = 3;
var ATTEMPT_LOCK_OUT_TIME = 5 * 60;
var currentPin;

$scope.$on("$ionicView.beforeEnter", function(event) {
$scope.currentPin = $scope.confirmPin = '';
$scope.fromSettings = $stateParams.fromSettings == 'true' ? true : false;
$scope.locking = $stateParams.locking == 'true' ? true : false;
currentPin = $scope.confirmPin = '';
$scope.action = $stateParams.action;
$scope.match = $scope.error = $scope.disableButtons = false;
$scope.currentAttempts = 0;
$scope.appName = appConfigService.name;
Expand All @@ -27,26 +27,19 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
});
});

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 limitTime = Math.floor(Date.now() / 1000) + ATTEMPT_LOCK_OUT_TIME;
var config = configService.getSync();
var opts = {
lock: {
method: 'pin',
value: config.lock.value,
bannedUntil: limitTime,
attempts: config.lock.attempts + 1,
}
};

configService.set(opts, function(err) {
if (err) $log.debug(err);
lockTimeControl(limitTime);
});
saveFailedAttempt(limitTime);
}
};

Expand Down Expand Up @@ -74,7 +67,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta

function reset() {
$scope.expires = $scope.error = $scope.disableButtons = null;
$scope.currentPin = $scope.confirmPin = '';
currentPin = $scope.confirmPin = '';
$interval.cancel(countDown);
$timeout(function() {
$scope.$apply();
Expand All @@ -84,69 +77,85 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
};

$scope.getFilledClass = function(limit) {
return $scope.currentPin.length >= limit ? 'filled-' + $scope.appName : null;
return currentPin.length >= limit ? 'filled-' + $scope.appName : null;
};

$scope.delete = function() {
if ($scope.disableButtons) return;
if ($scope.currentPin.length > 0) {
$scope.currentPin = $scope.currentPin.substring(0, $scope.currentPin.length - 1);
if (currentPin.length > 0) {
currentPin = currentPin.substring(0, currentPin.length - 1);
$scope.error = false;
$scope.updatePin();
}
};

$scope.isComplete = function() {
if ($scope.currentPin.length < 4) return false;
if (currentPin.length < 4) return false;
else return true;
};

$scope.updatePin = function(value) {
if ($scope.disableButtons) return;
$scope.error = false;
if (value && !$scope.isComplete()) {
$scope.currentPin = $scope.currentPin + value;
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 config = configService.getSync();
$scope.match = config.lock && config.lock.method == 'pin' && config.lock.value == $scope.currentPin ? true : false;
if (!$scope.locking) {
if ($scope.match) {
if ($scope.fromSettings) saveSettings();
else {
saveSettings('pin', $scope.currentPin);
$scope.error = false;
var savedMethod = getSavedMethod();

switch ($scope.action) {
case 'setup':
applyAndCheckPin();
break;
case 'disable':
if (isMatch(currentPin)) {
deletePin();
} else {
showError();
checkAttempts();
}
} else {
$timeout(function() {
$scope.confirmPin = $scope.currentPin = '';
$scope.error = true;
}, 200);
checkAttempts();
}
} else {
processCodes();
break;
case 'check':
if (isMatch(currentPin)) return $scope.close();
showError();
break;
}
};

function processCodes() {
function showError() {
$timeout(function() {
$scope.confirmPin = currentPin = '';
$scope.error = true;
}, 200);

$timeout(function() {
$scope.$apply();
});
};

function applyAndCheckPin() {
if (!$scope.confirmPin) {
$timeout(function() {
$scope.confirmPin = $scope.currentPin;
$scope.currentPin = '';
$scope.confirmPin = currentPin;
currentPin = '';
}, 200);
} else {
if ($scope.confirmPin == $scope.currentPin)
saveSettings('pin', $scope.confirmPin);
if ($scope.confirmPin == currentPin)
savePin($scope.confirmPin);
else {
$scope.confirmPin = $scope.currentPin = '';
$scope.confirmPin = currentPin = '';
$scope.error = true;
}
}
Expand All @@ -155,15 +164,12 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
});
};

function saveSettings(method, value) {
var config = configService.getSync();
var attempts = config.lock && config.lock.attempts ? config.lock.attempts : 0;
function deletePin() {
var opts = {
lock: {
method: method || null,
value: value || null,
method: 'none',
value: null,
bannedUntil: null,
attempts: attempts + 1,
}
};

Expand All @@ -173,6 +179,34 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
});
};

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(limitTime);
});
};

$scope.close = function(delay) {
$timeout(function() {
var shouldReturn = $ionicHistory.viewHistory().backView && $ionicHistory.viewHistory().backView.stateName != 'starting';
Expand Down
Loading

0 comments on commit dd154a3

Please sign in to comment.