Wallet/src/js/controllers/lockSetup.js

163 lines
4 KiB
JavaScript
Raw Normal View History

'use strict';
2017-05-09 16:42:41 -03:00
angular.module('copayApp.controllers').controller('lockSetupController', function($state, $rootScope, $scope, $timeout, $log, configService, gettextCatalog, fingerprintService, profileService, lodash, applicationService) {
2017-04-04 16:47:36 -03:00
function init() {
2017-04-04 15:14:42 -03:00
$scope.options = [
{
2017-04-20 15:48:05 -03:00
method: 'none',
2017-04-04 15:14:42 -03:00
label: gettextCatalog.getString('Disabled'),
2017-04-20 15:48:05 -03:00
disabled: false,
2017-04-04 15:14:42 -03:00
},
{
2017-04-04 18:06:11 -03:00
method: 'pin',
2017-04-18 13:19:16 -03:00
label: gettextCatalog.getString('Lock by PIN'),
2017-04-20 15:48:05 -03:00
needsBackup: false,
disabled: false,
2017-04-04 15:14:42 -03:00
},
];
if (fingerprintService.isAvailable()) {
$scope.options.push({
2017-04-04 18:06:11 -03:00
method: 'fingerprint',
2017-04-18 13:19:16 -03:00
label: gettextCatalog.getString('Lock by Fingerprint'),
2017-04-20 15:48:05 -03:00
needsBackup: false,
disabled: false,
2017-04-04 15:14:42 -03:00
});
}
2017-03-30 17:45:57 -03:00
2017-04-20 15:48:05 -03:00
initMethodSelector();
2017-03-30 17:45:57 -03:00
processWallets();
2017-04-04 16:47:36 -03:00
};
$scope.$on("$ionicView.beforeEnter", function(event) {
init();
});
2017-04-20 15:48:05 -03:00
function getSavedMethod() {
var config = configService.getSync();
2017-06-12 11:49:34 -03:00
if (config.lock && config.lock.method) return config.lock.method;
2017-04-20 15:48:05 -03:00
return 'none';
};
2017-04-20 15:48:05 -03:00
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;
});
2017-05-09 16:42:41 -03:00
// HACK: Disable until we allow to change between methods directly
2017-04-20 15:48:05 -03:00
if (fingerprintService.isAvailable()) {
switch (savedMethod) {
case 'pin':
disable('fingerprint');
break;
case 'fingerprint':
disable('pin');
break;
}
}
2017-04-20 15:48:05 -03:00
$scope.currentOption = lodash.find($scope.options, {
method: savedMethod
});
$timeout(function() {
$scope.$apply();
});
};
2017-03-30 17:45:57 -03:00
function processWallets() {
var wallets = profileService.getWallets();
var singleLivenetWallet = wallets.length == 1 && wallets[0].network == 'livenet' && wallets[0].needsBackup;
var atLeastOneLivenetWallet = lodash.any(wallets, function(w) {
return w.network == 'livenet' && w.needsBackup;
});
if (singleLivenetWallet) {
$scope.errorMsg = gettextCatalog.getString('Backup your wallet before using this function');
2017-04-04 15:14:42 -03:00
disableOptsUntilBackup();
2017-03-30 17:45:57 -03:00
} else if (atLeastOneLivenetWallet) {
$scope.errorMsg = gettextCatalog.getString('Backup all livenet wallets before using this function');
2017-04-04 15:14:42 -03:00
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;
};
2017-03-30 17:45:57 -03:00
$timeout(function() {
$scope.$apply();
});
};
2017-04-18 13:19:16 -03:00
$scope.select = function(selectedMethod) {
2017-04-20 15:48:05 -03:00
var savedMethod = getSavedMethod();
if (savedMethod == selectedMethod) return;
2017-04-18 13:19:16 -03:00
2017-04-20 15:48:05 -03:00
if (selectedMethod == 'none') {
disableMethod(savedMethod);
} else {
enableMethod(selectedMethod);
}
};
function disableMethod(method) {
switch (method) {
case 'pin':
2017-05-09 12:36:32 -03:00
applicationService.pinModal('disable');
2017-04-20 15:48:05 -03:00
break;
case 'fingerprint':
fingerprintService.check('unlockingApp', function(err) {
if (err) init();
else saveConfig('none');
});
break;
}
};
function enableMethod(method) {
switch (method) {
case 'pin':
2017-05-09 12:36:32 -03:00
applicationService.pinModal('setup');
2017-04-20 15:48:05 -03:00
break;
case 'fingerprint':
saveConfig('fingerprint');
break;
2017-04-04 15:14:42 -03:00
}
};
2017-04-04 15:14:42 -03:00
function saveConfig(method) {
var opts = {
lock: {
2017-04-20 15:48:05 -03:00
method: method,
2017-04-18 13:19:16 -03:00
value: null,
2017-04-04 15:14:42 -03:00
}
};
2017-04-04 15:14:42 -03:00
configService.set(opts, function(err) {
if (err) $log.debug(err);
2017-04-20 15:48:05 -03:00
initMethodSelector();
2017-04-04 15:14:42 -03:00
});
};
2017-05-09 16:42:41 -03:00
$rootScope.$on('pinModalClosed', function() {
2017-05-09 17:29:33 -03:00
init()
2017-05-09 16:42:41 -03:00
});
});