From e749c62ccd7760d62541bc33f2f47afa259a9586 Mon Sep 17 00:00:00 2001 From: julian Date: Wed, 11 Dec 2024 16:41:00 -0600 Subject: [PATCH] chore: satisfy linter --- .../desktop/qr_code_scanner_dialog.dart | 149 ++++++++++-------- 1 file changed, 83 insertions(+), 66 deletions(-) diff --git a/lib/widgets/desktop/qr_code_scanner_dialog.dart b/lib/widgets/desktop/qr_code_scanner_dialog.dart index edb6a074a..260ce61c9 100644 --- a/lib/widgets/desktop/qr_code_scanner_dialog.dart +++ b/lib/widgets/desktop/qr_code_scanner_dialog.dart @@ -23,14 +23,15 @@ import 'primary_button.dart'; import 'secondary_button.dart'; class QrCodeScannerDialog extends StatefulWidget { - final Function(String) onQrCodeDetected; + final void Function(String) onQrCodeDetected; - QrCodeScannerDialog({ + const QrCodeScannerDialog({ + super.key, required this.onQrCodeDetected, }); @override - _QrCodeScannerDialogState createState() => _QrCodeScannerDialogState(); + State createState() => _QrCodeScannerDialogState(); } class _QrCodeScannerDialogState extends State { @@ -66,16 +67,16 @@ class _QrCodeScannerDialogState extends State { }); if (Platform.isLinux && _cameraLinuxPlugin != null) { - await _cameraLinuxPlugin!.initializeCamera(); + await _cameraLinuxPlugin.initializeCamera(); Logging.instance.log("Linux Camera initialized", level: LogLevel.Info); } else if (Platform.isWindows && _cameraWindowsPlugin != null) { final List cameras = - await _cameraWindowsPlugin!.availableCameras(); + await _cameraWindowsPlugin.availableCameras(); if (cameras.isEmpty) { throw CameraException('No cameras available', 'No cameras found.'); } final CameraDescription camera = cameras[0]; // Could be user-selected. - _cameraId = await _cameraWindowsPlugin!.createCameraWithSettings( + _cameraId = await _cameraWindowsPlugin.createCameraWithSettings( camera, const MediaSettings( resolutionPreset: ResolutionPreset.low, @@ -84,11 +85,13 @@ class _QrCodeScannerDialogState extends State { enableAudio: false, ), ); - await _cameraWindowsPlugin!.initializeCamera(_cameraId); + await _cameraWindowsPlugin.initializeCamera(_cameraId); // await _cameraWindowsPlugin!.onCameraInitialized(_cameraId).first; // TODO [prio=low]: Make this work. ^^^ - Logging.instance.log("Windows Camera initialized with ID: $_cameraId", - level: LogLevel.Info); + Logging.instance.log( + "Windows Camera initialized with ID: $_cameraId", + level: LogLevel.Info, + ); } else if (Platform.isMacOS) { final List videoDevices = await CameraMacOS.instance .listDevices(deviceType: CameraMacOSDeviceType.video); @@ -104,8 +107,9 @@ class _QrCodeScannerDialogState extends State { }); Logging.instance.log( - "macOS Camera initialized with ID: $_macOSDeviceId", - level: LogLevel.Info); + "macOS Camera initialized with ID: $_macOSDeviceId", + level: LogLevel.Info, + ); } if (mounted) { setState(() { @@ -129,13 +133,15 @@ class _QrCodeScannerDialogState extends State { Future _stopCamera() async { try { if (Platform.isLinux && _cameraLinuxPlugin != null) { - _cameraLinuxPlugin!.stopCamera(); + _cameraLinuxPlugin.stopCamera(); Logging.instance.log("Linux Camera stopped", level: LogLevel.Info); } else if (Platform.isWindows && _cameraWindowsPlugin != null) { // if (_cameraId >= 0) { - await _cameraWindowsPlugin!.dispose(_cameraId); - Logging.instance.log("Windows Camera stopped with ID: $_cameraId", - level: LogLevel.Info); + await _cameraWindowsPlugin.dispose(_cameraId); + Logging.instance.log( + "Windows Camera stopped with ID: $_cameraId", + level: LogLevel.Info, + ); // } else { // Logging.instance.log("Windows Camera ID is null. Cannot dispose.", // level: LogLevel.Error); @@ -143,8 +149,10 @@ class _QrCodeScannerDialogState extends State { } else if (Platform.isMacOS) { // if (_macOSDeviceId != null) { await CameraMacOS.instance.stopImageStream(); - Logging.instance.log("macOS Camera stopped with ID: $_macOSDeviceId", - level: LogLevel.Info); + Logging.instance.log( + "macOS Camera stopped with ID: $_macOSDeviceId", + level: LogLevel.Info, + ); // } else { // Logging.instance.log("macOS Camera ID is null. Cannot stop.", // level: LogLevel.Error); @@ -168,7 +176,7 @@ class _QrCodeScannerDialogState extends State { try { String? base64Image; if (Platform.isLinux && _cameraLinuxPlugin != null) { - base64Image = await _cameraLinuxPlugin!.captureImage(); + base64Image = await _cameraLinuxPlugin.captureImage(); } else if (Platform.isWindows) { final XFile xfile = await _cameraWindowsPlugin!.takePicture(_cameraId); @@ -180,14 +188,14 @@ class _QrCodeScannerDialogState extends State { if (macOSimg == null) { Logging.instance .log("Failed to capture image", level: LogLevel.Error); - await Future.delayed(Duration(milliseconds: _imageDelayInMs)); + await Future.delayed(Duration(milliseconds: _imageDelayInMs)); continue; } final img.Image? image = img.decodeImage(macOSimg.bytes!); if (image == null) { Logging.instance .log("Failed to capture image", level: LogLevel.Error); - await Future.delayed(Duration(milliseconds: _imageDelayInMs)); + await Future.delayed(Duration(milliseconds: _imageDelayInMs)); continue; } base64Image = base64Encode(img.encodePng(image)); @@ -196,7 +204,7 @@ class _QrCodeScannerDialogState extends State { // Logging.instance // .log("Failed to capture image", level: LogLevel.Error); // Spammy. - await Future.delayed(Duration(milliseconds: _imageDelayInMs)); + await Future.delayed(Duration(milliseconds: _imageDelayInMs)); continue; } final img.Image? image = img.decodeImage(base64Decode(base64Image)); @@ -205,7 +213,7 @@ class _QrCodeScannerDialogState extends State { // > decoders, it is much slower than using an explicit decoder if (image == null) { Logging.instance.log("Failed to decode image", level: LogLevel.Error); - await Future.delayed(Duration(milliseconds: _imageDelayInMs)); + await Future.delayed(Duration(milliseconds: _imageDelayInMs)); continue; } @@ -233,8 +241,8 @@ class _QrCodeScannerDialogState extends State { // Spammy. } - await Future.delayed(Duration(milliseconds: _imageDelayInMs)); - } catch (e, s) { + await Future.delayed(Duration(milliseconds: _imageDelayInMs)); + } catch (e) { // Logging.instance.log("Failed to capture and scan image: $e\n$s", level: LogLevel.Error); // Spammy. @@ -266,7 +274,7 @@ class _QrCodeScannerDialogState extends State { return null; } return qrDecode.text; - } catch (e, s) { + } catch (e) { // Logging.instance.log("Failed to decode QR code: $e\n$s", level: LogLevel.Error); // Spammy. return null; @@ -322,61 +330,70 @@ class _QrCodeScannerDialogState extends State { allowedExtensions: ["png", "jpg", "jpeg"], ); - if (result == null || result.files.single.path == null) { - await showFloatingFlushBar( - type: FlushBarType.info, - message: "No file selected", - iconAsset: Assets.svg.file, - context: context, - ); - return; - } - - final filePath = result?.files.single.path!; - if (filePath == null) { - await showFloatingFlushBar( - type: FlushBarType.info, - message: "Error selecting file.", - iconAsset: Assets.svg.file, - context: context, - ); - return; - } - try { - final img.Image? image = - img.decodeImage(File(filePath!).readAsBytesSync()); - if (image == null) { + if (context.mounted) { + if (result == null || result.files.single.path == null) { await showFloatingFlushBar( type: FlushBarType.info, - message: "Failed to decode image.", + message: "No file selected", iconAsset: Assets.svg.file, context: context, ); return; } - final String? scanResult = await _scanImage(image); - if (scanResult != null && scanResult.isNotEmpty) { - widget.onQrCodeDetected(scanResult); - Navigator.of(context).pop(); - } else { + final filePath = result.files.single.path; + if (filePath == null) { await showFloatingFlushBar( type: FlushBarType.info, - message: "No QR code found in the image.", + message: "Error selecting file.", iconAsset: Assets.svg.file, context: context, ); + return; + } + + try { + final img.Image? image = + img.decodeImage(File(filePath).readAsBytesSync()); + if (image == null) { + await showFloatingFlushBar( + type: FlushBarType.info, + message: "Failed to decode image.", + iconAsset: Assets.svg.file, + context: context, + ); + return; + } + + final String? scanResult = await _scanImage(image); + if (context.mounted) { + if (scanResult != null && scanResult.isNotEmpty) { + widget.onQrCodeDetected(scanResult); + Navigator.of(context).pop(); + } else { + await showFloatingFlushBar( + type: FlushBarType.info, + message: "No QR code found in the image.", + iconAsset: Assets.svg.file, + context: context, + ); + } + } + } catch (e, s) { + Logging.instance.log( + "Failed to decode image: $e\n$s", + level: LogLevel.Error, + ); + if (context.mounted) { + await showFloatingFlushBar( + type: FlushBarType.info, + message: + "Error processing the image. Please try again.", + iconAsset: Assets.svg.file, + context: context, + ); + } } - } catch (e, s) { - Logging.instance.log("Failed to decode image: $e\n$s", - level: LogLevel.Error); - await showFloatingFlushBar( - type: FlushBarType.info, - message: - "Error processing the image. Please try again.", - iconAsset: Assets.svg.file, - context: context, - ); } }, ),