Wallet/src/js/controllers/lockSetup.js

128 lines
3.5 KiB
JavaScript
Raw Normal View History

'use strict';
2017-04-20 13:05:33 -03:00
angular.module('copayApp.controllers').controller('lockSetupController', function($state, $scope, $timeout, $log, configService, gettextCatalog, fingerprintService, profileService, lodash) {
2017-04-04 16:47:36 -03:00
function init() {
2017-04-04 15:14:42 -03:00
$scope.options = [
{
2017-04-18 13:19:16 -03:00
method: null,
2017-04-04 15:14:42 -03:00
label: gettextCatalog.getString('Disabled'),
},
{
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-04 15:14:42 -03:00
needsBackup: null,
disabled: null,
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-04 15:14:42 -03:00
needsBackup: null,
disabled: null,
2017-04-04 15:14:42 -03:00
});
}
2017-03-30 17:45:57 -03:00
checkAndSelectOption();
2017-03-30 17:45:57 -03:00
processWallets();
2017-04-04 16:47:36 -03:00
};
$scope.$on("$ionicView.beforeEnter", function(event) {
init();
});
function checkAndSelectOption() {
var config = configService.getSync();
var method = config.lock && config.lock.method;
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
});
}
$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) {
var config = configService.getSync();
var savedMethod = config.lock && config.lock.method;
if (!selectedMethod) {
if (!savedMethod) return;
if (savedMethod == 'pin') $state.transitionTo('tabs.pin', {
fromSettings: true,
locking: false,
});
else saveConfig();
2017-04-18 13:19:16 -03:00
} else if (selectedMethod == 'pin') {
if (savedMethod == 'pin') return;
$state.transitionTo('tabs.pin', {
2017-04-04 15:14:42 -03:00
fromSettings: true,
2017-04-18 13:19:16 -03:00
locking: savedMethod == 'pin' ? false : true
2017-04-04 15:14:42 -03:00
});
2017-04-20 13:05:33 -03:00
} else if (selectedMethod == 'fingerprint') {
if (savedMethod == 'fingerprint') return;
else saveConfig('fingerprint');
2017-04-04 15:14:42 -03:00
}
$timeout(function() {
$scope.$apply();
});
};
2017-04-04 15:14:42 -03:00
function saveConfig(method) {
var opts = {
lock: {
2017-04-18 13:19:16 -03:00
method: method || null,
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);
checkAndSelectOption();
2017-04-04 15:14:42 -03:00
});
};
});