Merge pull request #4 from BenSower/master

Switched to (imo) better error handling
This commit is contained in:
Matthew Smith 2018-01-10 07:36:51 -08:00 committed by GitHub
commit 443c756ab7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 57 additions and 40 deletions

View File

@ -41,7 +41,11 @@ class BarcodeScanPlugin(val activity: Activity): MethodCallHandler,
if (resultCode == Activity.RESULT_OK) {
val barcode = data?.getStringExtra("SCAN_RESULT")
barcode?.let { this.result?.success(barcode) }
} else {
val errorCode = data?.getStringExtra("ERROR_CODE")
this.result?.error(errorCode, null, null)
}
return true
}
return false
}

View File

@ -26,7 +26,6 @@ import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.view.Menu
@ -38,22 +37,19 @@ import me.dm7.barcodescanner.zxing.ZXingScannerView
class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
lateinit var scannerView: me.dm7.barcodescanner.zxing.ZXingScannerView
//lateinit var pressLightSnackBar: Snackbar
companion object {
val REQUEST_TAKE_PHOTO_CAMERA_PERMISSION = 100
val TOGGLE_FLASH = 200
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = ""
scannerView = ZXingScannerView(this)
scannerView.setAutoFocus(true)
setContentView(scannerView)
// pressLightSnackBar = Snackbar.make(scannerView, "Press Screen To Turn Light On",
// Int.MAX_VALUE)
// pressLightSnackBar.show()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -81,6 +77,7 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
override fun onResume() {
super.onResume()
scannerView.setResultHandler(this)
// start camera immediately if permission is already given
if (!requestCameraAccessIfNecessary()) {
scannerView.startCamera()
}
@ -98,40 +95,40 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
finish()
}
fun finishWithError(errorCode: String) {
val intent = Intent()
intent.putExtra("ERROR_CODE", errorCode)
setResult(Activity.RESULT_CANCELED, intent)
finish()
}
private fun requestCameraAccessIfNecessary(): Boolean {
val array = arrayOf(Manifest.permission.CAMERA)
if (ContextCompat
.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
val snackbar = Snackbar.make(scannerView, "Grant access to the camera to enable barcode scanning.", Snackbar.LENGTH_INDEFINITE)
snackbar.setAction("OK") {
ActivityCompat.requestPermissions(this,
array,
ActivityCompat.requestPermissions(this, array,
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
}
snackbar.show()
} else {
ActivityCompat.requestPermissions(this,
array,
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
}
return true
}
return false
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
grantResults: IntArray) {
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,grantResults: IntArray) {
when (requestCode) {
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION -> {
if (PermissionUtil.verifyPermissions(grantResults)) {
scannerView.startCamera()
} else {
finishWithError("PERMISSION_NOT_GRANTED")
}
}
}
else -> {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
}
}
object PermissionUtil {

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(new MyApp());
@ -43,7 +44,21 @@ class _MyAppState extends State<MyApp> {
}
Future scan() async {
try {
String barcode = await BarcodeScanner.scan();
setState(() => this.barcode = barcode);
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
this.barcode = 'The user did not grant the camera permission!';
});
} else {
setState(() => this.barcode = 'Unknown error: $e');
}
} on FormatException{
setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
} catch (e) {
setState(() => this.barcode = 'Unknown error: $e');
}
}
}

View File

@ -32,4 +32,12 @@
}
}
- (void)barcodeScannerViewController:(BarcodeScannerViewController *)controller didFailWithErrorCode:(NSString *)errorCode {
if (self.result){
self.result([FlutterError errorWithCode:errorCode
message:nil
details:nil]);
}
}
@end

View File

@ -38,7 +38,8 @@
if (success) {
[self startScan];
} else {
[self showNoCameraAccessAlert];
[self.delegate barcodeScannerViewController:self didFailWithErrorCode:@"PERMISSION_NOT_GRANTED"];
[self dismissViewControllerAnimated:NO completion:nil];
}
}];
}
@ -81,16 +82,6 @@
}
}
- (void)showNoCameraAccessAlert {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Camera access denied"
message:@"Camera access has been disabled for this application. To turn camera access back on, please go to the iOS settings application."
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil];
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)toggle {
[self toggleFlash:!self.isFlashOn];
[self updateFlashButton];

View File

@ -9,5 +9,6 @@
@protocol BarcodeScannerViewControllerDelegate <NSObject>
- (void)barcodeScannerViewController:(BarcodeScannerViewController *)controller didScanBarcodeWithResult:(NSString *)result;
- (void)barcodeScannerViewController:(BarcodeScannerViewController *)controller didFailWithErrorCode:(NSString *)errorCode;
@end

View File

@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/services.dart';
class BarcodeScanner {
static const CameraAccessDenied = 'PERMISSION_NOT_GRANTED';
static const MethodChannel _channel =
const MethodChannel('com.apptreesoftware.barcode_scan');
static Future<String> scan() async => await _channel.invokeMethod('scan');