add pincode service - refactor

This commit is contained in:
JDonadio 2017-03-01 17:29:39 -03:00
commit 313269a45c
5 changed files with 85 additions and 48 deletions

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('advancedSettingsController', function($scope, $rootScope, $log, $window, $ionicModal, lodash, configService, uxLanguage, platformInfo, pushNotificationsService, profileService, feeService, storageService, $ionicHistory, $timeout, $ionicScrollDelegate) {
angular.module('copayApp.controllers').controller('advancedSettingsController', function($scope, $rootScope, $log, $window, $ionicModal, lodash, configService, uxLanguage, platformInfo, pushNotificationsService, profileService, feeService, storageService, $ionicHistory, $ionicScrollDelegate, pincodeService) {
var updateConfig = function() {
var config = configService.getSync();
@ -15,7 +15,7 @@ angular.module('copayApp.controllers').controller('advancedSettingsController',
value: config.hideNextSteps.enabled
};
$scope.usePincode = {
value: config.pincode ? config.pincode.enabled : false
enabled: config.pincode ? config.pincode.enabled : false
};
};
@ -41,23 +41,6 @@ angular.module('copayApp.controllers').controller('advancedSettingsController',
});
};
$scope.savePincodeChanges = function(val) {
if (!val || val.length < 4) {
$scope.usePincode = {
value: false
}
return;
}
var opts = {
usePincode: {
enabled: $scope.usePincode.enabled
},
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
});
};
$scope.recentTransactionsChange = function() {
var opts = {
recentTransactions: {
@ -69,15 +52,20 @@ angular.module('copayApp.controllers').controller('advancedSettingsController',
});
};
$scope.showPincodeModal = function() {
$scope.fromSettings = true;
$ionicModal.fromTemplateUrl('views/modals/pincode.html', {
scope: $scope,
backdropClickToClose: false,
hardwareBackButtonClose: false
}).then(function(modal) {
$scope.pincodeModal = modal;
$scope.pincodeModal.show();
$scope.usePincodeChange = function() {
var opts = {};
opts.enabled = $scope.usePincode.enabled;
opts.from = 'settings';
pincodeService.lockChange(opts, function(err) {
if (err) {
$log.warn(err);
// ToDo show error?
$scope.usePincode.enabled = false;
$scope.$apply();
return;
}
});
};

View file

@ -1,7 +1,11 @@
'use strict';
angular.module('copayApp.controllers').controller('pincodeController', function($timeout, $scope, $log, $window, configService) {
$scope.pincode = '';
var config = configService.getSync();
$scope.currentPincode = config.pincode ? config.pincode.value : null;
$scope.pincode = $scope.pc1 = $scope.pc2 = '';
console.log('#######', $scope.from, $scope.enabled);
angular.element($window).on('keydown', function(e) {
if (e.which === 8) { // you can add others here inside brackets.
@ -9,15 +13,16 @@ angular.module('copayApp.controllers').controller('pincodeController', function(
$scope.delete();
}
if (e.key && e.key.match(/^[0-9]$/))
if (e && e.key.match(/^[0-9]$/))
$scope.add(e.key);
else if (e.key && e.key == 'Enter')
checkPasscode();
else if (e && e.keyCode == 27)
$scope.close(false);
else if (e && e.keyCode == 13)
$scope.save();
});
$scope.add = function(value) {
if (isComplete()) checkPasscode();
else updatePassCode(value);
updatePassCode(value);
};
$scope.delete = function() {
@ -33,24 +38,36 @@ angular.module('copayApp.controllers').controller('pincodeController', function(
};
function updatePassCode(value) {
if (value) $scope.pincode = $scope.pincode + value;
if (value && $scope.pincode.length < 4)
$scope.pincode = $scope.pincode + value;
$timeout(function() {
checkPasscode();
$scope.$apply();
});
};
function checkPasscode() {
console.log('Checking');
configService.whenAvailable(function(config) {
var value = '1234';
if (value != $scope.pincode) return;
console.log('MATCH');
});
$scope.save = function() {
if (!$scope.pc1) {
console.log('No pc 1');
$scope.pc1 = $scope.pincode;
console.log('$scope.pc1', $scope.pc1);
$scope.pincode = '';
$timeout(function() {
$scope.$apply();
});
return;
} else {
$scope.pc2 = $scope.pincode;
console.log('$scope.pc2', $scope.pc2);
}
if ($scope.pc1 == $scope.pc2) {
$scope.close($scope.pc1);
} else {
$scope.enabled ? pincodeService.lockApp() : pincodeService.unlockApp();
}
};
$scope.cancel = function() {
$scope.savePincodeChanges(false);
$scope.close = function() {
$scope.pincodeModal.hide();
};
});

View file

@ -0,0 +1,32 @@
'use strict';
angular.module('copayApp.services').factory('pincodeService', function($log, $rootScope, $ionicModal, configService) {
var root = {};
var openPincodeModal = function(opts) {
var scope = $rootScope.$new(true);
scope.from = opts.from;
scope.enabled = opts.enabled;
$ionicModal.fromTemplateUrl('views/modals/pincode.html', {
scope: scope,
backdropClickToClose: false,
hardwareBackButtonClose: false
}).then(function(modal) {
scope.pincodeModal = modal;
scope.pincodeModal.show();
});
};
root.lockChange = function(opts, cb) {
if (opts.enabled) console.log('Locking app from service');
else console.log('Unlocking app from service');
openPincodeModal(opts);
};
root.isLocked = function() {
var config = configService.getSync();
return config.pincode ? config.pincode.enabled : false;
};
return root;
});

View file

@ -31,7 +31,7 @@
<div class="item item-divider"></div>
<ion-toggle ng-model="usePincode.value" toggle-class="toggle-balanced" ng-change="showPincodeModal()">
<ion-toggle ng-model="usePincode.enabled" toggle-class="toggle-balanced" ng-change="usePincodeChange()">
<span class="toggle-label" translate>Use pin to lock/unlock the app</span>
</ion-toggle>
</div>

View file

@ -1,5 +1,5 @@
<ion-modal-view id="pin-code" ng-controller="pincodeController">
<div ng-if="fromSettings" ng-click="cancel()" class="close" translate>Close</div>
<div ng-if="from == 'settings'" ng-click="close()" class="close" translate>Close</div>
<div class="content">
<div class="block-code">
<div class="row">
@ -53,7 +53,7 @@
</div>
<div class="row">
<div class="col">
<button class="button button-light button-stretch icon ion-checkmark" ng-click="save()"></button>
<button class="button button-light button-stretch icon ion-checkmark" ng-click="save()" ng-disabled=""></button>
</div>
<div class="col">
<button class="button button-light button-stretch" ng-click="add('0')">0</button>