feat(scan): implement permission priming in scanService
This commit is contained in:
parent
ce3812b220
commit
f41c56ba04
3 changed files with 143 additions and 25 deletions
|
|
@ -1,29 +1,89 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('tabScanController', function($scope, $log, $timeout, scannerService, incomingData) {
|
||||
angular.module('copayApp.controllers').controller('tabScanController', function($scope, $log, $timeout, scannerService, incomingData, $state, $ionicHistory, $rootScope) {
|
||||
|
||||
$scope.$on("$ionicView.beforeEnter", function() {
|
||||
$log.debug('Preparing to display available controls.');
|
||||
var scannerStates = {
|
||||
unauthorized: 'unauthorized',
|
||||
denied: 'denied',
|
||||
unavailable: 'unavailable',
|
||||
loading: 'loading',
|
||||
visible: 'visible'
|
||||
};
|
||||
$scope.scannerStates = scannerStates;
|
||||
|
||||
function _updateCapabilities(){
|
||||
var capabilities = scannerService.getCapabilities();
|
||||
$scope.scannerIsAvailable = capabilities.isAvailable;
|
||||
$scope.scannerHasPermission = capabilities.hasPermission;
|
||||
$scope.scannerIsDenied = capabilities.isDenied;
|
||||
$scope.scannerIsRestricted = capabilities.isRestricted;
|
||||
$scope.canEnableLight = capabilities.canEnableLight;
|
||||
$scope.canChangeCamera = capabilities.canChangeCamera;
|
||||
$scope.canOpenSettings = capabilities.canOpenSettings;
|
||||
}
|
||||
|
||||
function _handleCapabilities(){
|
||||
if(!$scope.scannerIsAvailable){
|
||||
$scope.currentState = scannerStates.unavailable;
|
||||
} else if($scope.scannerIsDenied){
|
||||
$scope.currentState = scannerStates.denied;
|
||||
} else if($scope.scannerIsRestricted){
|
||||
$scope.currentState = scannerStates.denied;
|
||||
} else if(!$scope.scannerHasPermission){
|
||||
$scope.currentState = scannerStates.unauthorized;
|
||||
} else if($scope.scannerHasPermission){
|
||||
activate();
|
||||
}
|
||||
}
|
||||
|
||||
function _initScanView(){
|
||||
_updateCapabilities();
|
||||
_handleCapabilities();
|
||||
}
|
||||
|
||||
$scope.$on("$ionicView.beforeEnter", function() {
|
||||
$scope.currentState = scannerStates.loading;
|
||||
});
|
||||
|
||||
// This could be much cleaner with a Promise API
|
||||
// (needs a polyfill for some platforms)
|
||||
$rootScope.$on('scannerServiceInitialized', function(){
|
||||
$log.debug('Scanner initialization finished, reinitializing scan view...');
|
||||
_initScanView();
|
||||
});
|
||||
|
||||
$scope.$on("$ionicView.afterEnter", function() {
|
||||
if(scannerService.isInitialized()){
|
||||
_initScanView();
|
||||
}
|
||||
});
|
||||
|
||||
function activate(){
|
||||
scannerService.activate(function(){
|
||||
$log.debug('Scanner activated, setting to visible...');
|
||||
$scope.currentState = scannerStates.visible;
|
||||
scannerService.scan(function(err, contents){
|
||||
if(err){
|
||||
$log.debug('Scan canceled.');
|
||||
} else if ($state.params.passthroughMode) {
|
||||
$rootScope.scanResult = contents;
|
||||
goBack();
|
||||
} else {
|
||||
incomingData.redir(contents);
|
||||
handleSuccessfulScan(contents);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
$scope.$on("$ionicView.afterLeave", function() {
|
||||
scannerService.deactivate();
|
||||
});
|
||||
|
||||
function handleSuccessfulScan(contents){
|
||||
$log.debug('Scan returned: "' + contents + '"');
|
||||
incomingData.redir(contents);
|
||||
}
|
||||
|
||||
$scope.toggleLight = function(){
|
||||
scannerService.toggleLight(function(lightEnabled){
|
||||
$scope.lightActive = lightEnabled;
|
||||
|
|
@ -38,8 +98,15 @@ angular.module('copayApp.controllers').controller('tabScanController', function(
|
|||
$timeout(function(){
|
||||
$scope.cameraToggleActive = false;
|
||||
$log.debug('Camera toggle control deactivated.');
|
||||
}, 200);
|
||||
}, 600);
|
||||
});
|
||||
};
|
||||
|
||||
$scope.canGoBack = function(){
|
||||
return $state.params.passthroughMode;
|
||||
}
|
||||
function goBack(){
|
||||
$ionicHistory.backView().go();
|
||||
}
|
||||
$scope.goBack = goBack;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue