2017-02-23 16:56:46 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-03-03 14:18:56 -03:00
|
|
|
angular.module('copayApp.controllers').controller('pincodeController', function($rootScope, $state, $timeout, $scope, $log, $window, configService) {
|
|
|
|
|
|
|
|
|
|
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
|
|
|
|
$scope.currentPincode = $scope.newPincode = '';
|
|
|
|
|
$scope.fromSettings = data.stateParams.fromSettings == 'true' ? true : false;
|
|
|
|
|
$scope.locking = data.stateParams.locking == 'true' ? true : false;
|
|
|
|
|
console.log('### From Settings:', $scope.fromSettings, 'Locking:', $scope.locking);
|
|
|
|
|
});
|
2017-02-23 16:56:46 -05:00
|
|
|
|
|
|
|
|
angular.element($window).on('keydown', function(e) {
|
2017-03-02 11:25:38 -03:00
|
|
|
if (e.which === 8) {
|
2017-02-23 16:56:46 -05:00
|
|
|
e.preventDefault();
|
|
|
|
|
$scope.delete();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-01 17:29:39 -03:00
|
|
|
if (e && e.key.match(/^[0-9]$/))
|
2017-02-23 16:56:46 -05:00
|
|
|
$scope.add(e.key);
|
2017-03-01 17:29:39 -03:00
|
|
|
else if (e && e.keyCode == 27)
|
2017-03-02 16:32:08 -03:00
|
|
|
$scope.close();
|
2017-03-01 17:29:39 -03:00
|
|
|
else if (e && e.keyCode == 13)
|
|
|
|
|
$scope.save();
|
2017-02-23 16:56:46 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$scope.add = function(value) {
|
2017-03-01 17:29:39 -03:00
|
|
|
updatePassCode(value);
|
2017-02-23 16:56:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.delete = function() {
|
2017-03-02 16:32:08 -03:00
|
|
|
if ($scope.currentPincode.length > 0) {
|
|
|
|
|
$scope.currentPincode = $scope.currentPincode.substring(0, $scope.currentPincode.length - 1);
|
2017-02-23 16:56:46 -05:00
|
|
|
updatePassCode();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function isComplete() {
|
2017-03-02 16:32:08 -03:00
|
|
|
if ($scope.currentPincode.length < 4) return false;
|
2017-02-23 16:56:46 -05:00
|
|
|
else return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function updatePassCode(value) {
|
2017-03-02 16:32:08 -03:00
|
|
|
if (value && $scope.currentPincode.length < 4)
|
|
|
|
|
$scope.currentPincode = $scope.currentPincode + value;
|
2017-02-23 16:56:46 -05:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
};
|
2017-02-24 14:46:54 -05:00
|
|
|
|
2017-03-01 17:29:39 -03:00
|
|
|
$scope.save = function() {
|
2017-03-02 16:32:08 -03:00
|
|
|
if (!isComplete()) return;
|
|
|
|
|
var config = configService.getSync();
|
|
|
|
|
var match = config.pincode.value == $scope.currentPincode ? true : false;
|
|
|
|
|
|
2017-03-02 16:54:55 -03:00
|
|
|
if (!$scope.fromSettings && match) {
|
|
|
|
|
$scope.currentPincode = '';
|
|
|
|
|
$scope.close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!$scope.locking) {
|
|
|
|
|
if (!match) return;
|
|
|
|
|
saveSettings($scope.locking, '');
|
|
|
|
|
}
|
2017-03-02 16:32:08 -03:00
|
|
|
checkCodes();
|
|
|
|
|
};
|
2017-03-01 17:29:39 -03:00
|
|
|
|
2017-03-02 16:32:08 -03:00
|
|
|
function checkCodes() {
|
|
|
|
|
if (!$scope.newPincode) {
|
|
|
|
|
$scope.newPincode = $scope.currentPincode;
|
|
|
|
|
$scope.currentPincode = '';
|
2017-03-01 17:29:39 -03:00
|
|
|
} else {
|
2017-03-02 16:32:08 -03:00
|
|
|
if ($scope.newPincode == $scope.currentPincode)
|
|
|
|
|
saveSettings($scope.locking, $scope.newPincode);
|
2017-03-01 17:29:39 -03:00
|
|
|
}
|
2017-03-02 16:32:08 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
2017-02-24 14:46:54 -05:00
|
|
|
};
|
2017-02-28 10:32:10 -03:00
|
|
|
|
2017-03-02 16:32:08 -03:00
|
|
|
function saveSettings(enabled, value) {
|
|
|
|
|
var opts = {
|
|
|
|
|
pincode: {
|
|
|
|
|
enabled: enabled,
|
|
|
|
|
value: value
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
configService.set(opts, function(err) {
|
|
|
|
|
if (err) $log.debug(err);
|
|
|
|
|
$scope.close();
|
|
|
|
|
});
|
2017-02-28 10:32:10 -03:00
|
|
|
};
|
2017-03-02 11:25:38 -03:00
|
|
|
|
2017-03-02 16:32:08 -03:00
|
|
|
$scope.close = function() {
|
|
|
|
|
$rootScope.$emit('updatePincodeOption');
|
2017-03-03 14:18:56 -03:00
|
|
|
if ($scope.fromSettings) $state.go('tabs.advanced');
|
|
|
|
|
else $state.go('tabs.home');
|
2017-03-02 11:25:38 -03:00
|
|
|
};
|
2017-02-23 16:56:46 -05:00
|
|
|
});
|