Wallet/src/js/controllers/pincode.js

81 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-02-23 16:56:46 -05:00
'use strict';
angular.module('copayApp.controllers').controller('pincodeController', function($state, $stateParams, $ionicHistory, $timeout, $scope, $log, $window, configService) {
2017-03-03 14:18:56 -03:00
2017-03-06 12:45:37 -03:00
$scope.$on("$ionicView.beforeEnter", function(event) {
2017-03-03 14:18:56 -03:00
$scope.currentPincode = $scope.newPincode = '';
2017-03-06 12:45:37 -03:00
$scope.fromSettings = $stateParams.fromSettings == 'true' ? true : false;
$scope.locking = $stateParams.locking == 'true' ? true : false;
2017-03-03 14:18:56 -03:00
});
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-03-06 12:45:37 -03:00
$scope.updatePinCode();
2017-02-23 16:56:46 -05:00
}
};
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;
};
2017-03-06 12:45:37 -03:00
$scope.updatePinCode = function(value) {
if (value && !isComplete()) {
2017-03-02 16:32:08 -03:00
$scope.currentPincode = $scope.currentPincode + value;
2017-03-06 12:45:37 -03:00
}
2017-02-23 16:56:46 -05:00
$timeout(function() {
$scope.$apply();
});
2017-03-06 12:45:37 -03:00
if (!$scope.locking && isComplete()) {
$scope.save();
2017-03-06 12:45:37 -03:00
}
2017-02-23 16:56:46 -05:00
};
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.locking) {
2017-03-06 12:45:37 -03:00
if (match) {
$scope.fromSettings ? saveSettings($scope.locking, '') : $scope.close(150);
2017-03-06 12:45:37 -03:00
}
} else {
checkCodes();
2017-03-02 16:54:55 -03:00
}
2017-03-02 16:32:08 -03:00
};
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-06 12:45:37 -03:00
$timeout(function() {
$scope.$apply();
});
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-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-03-02 16:32:08 -03:00
});
2017-02-28 10:32:10 -03:00
};
2017-03-02 11:25:38 -03:00
$scope.close = function(delay) {
$timeout(function() {
$ionicHistory.viewHistory().backView ? $ionicHistory.goBack() : $state.go('tabs.home');
}, delay || 1);
2017-03-02 11:25:38 -03:00
};
2017-02-23 16:56:46 -05:00
});