From 6690f10118036209e2073b0056b845a8aacba88f Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 11 Dec 2024 22:53:03 +1100 Subject: [PATCH] Handle condition where camera controller fails to initialize --- lib/barcode/camera_controller.dart | 26 ++++++++++++++++++++++++++ lib/l10n/app_en.arb | 3 +++ lib/widget/snacks.dart | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/barcode/camera_controller.dart b/lib/barcode/camera_controller.dart index b3e26fc4..4c6edd7a 100644 --- a/lib/barcode/camera_controller.dart +++ b/lib/barcode/camera_controller.dart @@ -1,10 +1,14 @@ import "dart:math"; import "dart:typed_data"; +import "package:camera/camera.dart"; import "package:flutter/material.dart"; import "package:flutter_tabler_icons/flutter_tabler_icons.dart"; import "package:inventree/app_colors.dart"; +import "package:inventree/inventree/sentry.dart"; import "package:inventree/preferences.dart"; +import "package:inventree/widget/snacks.dart"; +import "package:one_context/one_context.dart"; import "package:wakelock_plus/wakelock_plus.dart"; import "package:flutter_zxing/flutter_zxing.dart"; @@ -133,7 +137,28 @@ class _CameraBarcodeControllerState extends InvenTreeBarcodeControllerState { } }); } + } + void onControllerCreated(CameraController? controller, Exception? error) { + if (error != null) { + sentryReportError( + "CameraBarcodeController.onControllerCreated", + error, + null + ); + } + + if (controller == null) { + showSnackIcon( + L10().cameraCreationError, + icon: TablerIcons.camera_x, + success: false + ); + + if (OneContext.hasContext) { + Navigator.pop(OneContext().context!); + } + } } /* @@ -167,6 +192,7 @@ class _CameraBarcodeControllerState extends InvenTreeBarcodeControllerState { tryInverted: true, tryRotate: true, showGallery: false, + onControllerCreated: onControllerCreated, scanDelay: Duration(milliseconds: scan_delay), resolution: ResolutionPreset.high, lensDirection: CameraLensDirection.back, diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 72d5d92f..d6693bdc 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -202,6 +202,9 @@ "building": "Building", "@building": {}, + "cameraCreationError": "Could not open camera controller", + "@cameraCreationError": {}, + "cameraInternal": "Internal Camera", "@cameraInternal": {}, diff --git a/lib/widget/snacks.dart b/lib/widget/snacks.dart index c5a1aba8..398a4840 100644 --- a/lib/widget/snacks.dart +++ b/lib/widget/snacks.dart @@ -72,7 +72,7 @@ void showSnackIcon(String text, {IconData? icon, Function()? onAction, bool? suc onAction(); } ), - duration: Duration(seconds: onAction == null ? 1 : 2), + duration: Duration(seconds: onAction == null ? 5 : 10), ) );