From 680c4f066174d0d78f28b74cb103b0581fdec601 Mon Sep 17 00:00:00 2001 From: Navaron Bracke Date: Mon, 23 Oct 2023 23:04:40 +0200 Subject: [PATCH 1/5] remove unused helpers from barcode utility --- lib/src/barcode_utility.dart | 174 ----------------------------------- 1 file changed, 174 deletions(-) diff --git a/lib/src/barcode_utility.dart b/lib/src/barcode_utility.dart index a246e006d..ae330701d 100644 --- a/lib/src/barcode_utility.dart +++ b/lib/src/barcode_utility.dart @@ -1,5 +1,3 @@ -import 'dart:math' as math; - import 'package:flutter/material.dart'; import 'package:mobile_scanner/mobile_scanner.dart'; @@ -55,175 +53,3 @@ BarcodeFormat toFormat(int value) { return BarcodeFormat.unknown; } } - -CalendarEvent? toCalendarEvent(Map? data) { - if (data != null) { - return CalendarEvent.fromNative(data); - } else { - return null; - } -} - -DateTime? toDateTime(Map? data) { - if (data != null) { - final year = data['year'] as int; - final month = data['month'] as int; - final day = data['day'] as int; - final hour = data['hours'] as int; - final minute = data['minutes'] as int; - final second = data['seconds'] as int; - return data['isUtc'] as bool - ? DateTime.utc(year, month, day, hour, minute, second) - : DateTime(year, month, day, hour, minute, second); - } else { - return null; - } -} - -ContactInfo? toContactInfo(Map? data) { - if (data != null) { - return ContactInfo.fromNative(data); - } else { - return null; - } -} - -PersonName? toName(Map? data) { - if (data != null) { - return PersonName.fromNative(data); - } else { - return null; - } -} - -DriverLicense? toDriverLicense(Map? data) { - if (data != null) { - return DriverLicense.fromNative(data); - } else { - return null; - } -} - -Email? toEmail(Map? data) { - if (data != null) { - return Email.fromNative(data); - } else { - return null; - } -} - -GeoPoint? toGeoPoint(Map? data) { - if (data != null) { - return GeoPoint.fromNative(data); - } else { - return null; - } -} - -Phone? toPhone(Map? data) { - if (data != null) { - return Phone.fromNative(data); - } else { - return null; - } -} - -SMS? toSMS(Map? data) { - if (data != null) { - return SMS.fromNative(data); - } else { - return null; - } -} - -UrlBookmark? toUrl(Map? data) { - if (data != null) { - return UrlBookmark.fromNative(data); - } else { - return null; - } -} - -WiFi? toWiFi(Map? data) { - if (data != null) { - return WiFi.fromNative(data); - } else { - return null; - } -} - -Size applyBoxFit(BoxFit fit, Size input, Size output) { - if (input.height <= 0.0 || - input.width <= 0.0 || - output.height <= 0.0 || - output.width <= 0.0) { - return Size.zero; - } - - Size destination; - - final inputAspectRatio = input.width / input.height; - final outputAspectRatio = output.width / output.height; - - switch (fit) { - case BoxFit.fill: - destination = output; - break; - case BoxFit.contain: - if (outputAspectRatio > inputAspectRatio) { - destination = Size( - input.width * output.height / input.height, - output.height, - ); - } else { - destination = Size( - output.width, - input.height * output.width / input.width, - ); - } - break; - - case BoxFit.cover: - if (outputAspectRatio > inputAspectRatio) { - destination = Size( - output.width, - input.height * (output.width / input.width), - ); - } else { - destination = Size( - input.width * (output.height / input.height), - output.height, - ); - } - break; - case BoxFit.fitWidth: - destination = Size( - output.width, - input.height * (output.width / input.width), - ); - break; - case BoxFit.fitHeight: - destination = Size( - input.width * (output.height / input.height), - output.height, - ); - break; - case BoxFit.none: - destination = Size( - math.min(input.width, output.width), - math.min(input.height, output.height), - ); - break; - case BoxFit.scaleDown: - destination = input; - if (destination.height > output.height) { - destination = Size(output.height * inputAspectRatio, output.height); - } - if (destination.width > output.width) { - destination = Size(output.width, output.width / inputAspectRatio); - } - break; - } - - return destination; -} From 4dc9bce56808f3b412e74396cf543e4bf62a1d27 Mon Sep 17 00:00:00 2001 From: Navaron Bracke Date: Mon, 23 Oct 2023 23:12:22 +0200 Subject: [PATCH 2/5] remove toSize() helper --- lib/src/barcode_utility.dart | 6 ------ lib/src/mobile_scanner_controller.dart | 24 ++++++++++++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/src/barcode_utility.dart b/lib/src/barcode_utility.dart index ae330701d..3ff3ce8ba 100644 --- a/lib/src/barcode_utility.dart +++ b/lib/src/barcode_utility.dart @@ -1,12 +1,6 @@ import 'package:flutter/material.dart'; import 'package:mobile_scanner/mobile_scanner.dart'; -Size toSize(Map data) { - final width = data['width'] as double; - final height = data['height'] as double; - return Size(width, height); -} - List? toCorners(List>? data) { if (data == null) { return null; diff --git a/lib/src/mobile_scanner_controller.dart b/lib/src/mobile_scanner_controller.dart index ed62dbc67..bcaf8d553 100644 --- a/lib/src/mobile_scanner_controller.dart +++ b/lib/src/mobile_scanner_controller.dart @@ -287,14 +287,26 @@ class MobileScannerController { torchState.value = TorchState.on; } + final Size size; + + if (kIsWeb) { + size = Size( + startResult['videoWidth'] as double? ?? 0, + startResult['videoHeight'] as double? ?? 0, + ); + } else { + final Map? sizeInfo = + startResult['size'] as Map?; + + size = Size( + sizeInfo?['width'] as double? ?? 0, + sizeInfo?['height'] as double? ?? 0, + ); + } + isStarting = false; return startArguments.value = MobileScannerArguments( - size: kIsWeb - ? Size( - startResult['videoWidth'] as double? ?? 0, - startResult['videoHeight'] as double? ?? 0, - ) - : toSize(startResult['size'] as Map? ?? {}), + size: size, hasTorch: hasTorch, textureId: kIsWeb ? null : startResult['textureId'] as int?, webId: kIsWeb ? startResult['ViewID'] as String? : null, From e373b43de1700d59d734c08e7c6811a858e78087 Mon Sep 17 00:00:00 2001 From: Navaron Bracke Date: Mon, 23 Oct 2023 23:17:24 +0200 Subject: [PATCH 3/5] remove toFormat helper --- lib/mobile_scanner_web_plugin.dart | 3 +-- lib/src/barcode_utility.dart | 36 -------------------------- lib/src/mobile_scanner_controller.dart | 8 ++++-- 3 files changed, 7 insertions(+), 40 deletions(-) diff --git a/lib/mobile_scanner_web_plugin.dart b/lib/mobile_scanner_web_plugin.dart index d30037c4f..328a6c04e 100644 --- a/lib/mobile_scanner_web_plugin.dart +++ b/lib/mobile_scanner_web_plugin.dart @@ -5,7 +5,6 @@ import 'dart:ui' as ui; import 'package:flutter/services.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; import 'package:mobile_scanner/mobile_scanner_web.dart'; -import 'package:mobile_scanner/src/barcode_utility.dart'; import 'package:mobile_scanner/src/enums/barcode_format.dart'; import 'package:mobile_scanner/src/enums/camera_facing.dart'; @@ -110,7 +109,7 @@ class MobileScannerWebPlugin { if (arguments.containsKey('formats')) { formats = (arguments['formats'] as List) .cast() - .map((e) => toFormat(e)) + .map(BarcodeFormat.fromRawValue) .toList(); } diff --git a/lib/src/barcode_utility.dart b/lib/src/barcode_utility.dart index 3ff3ce8ba..8241bc681 100644 --- a/lib/src/barcode_utility.dart +++ b/lib/src/barcode_utility.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:mobile_scanner/mobile_scanner.dart'; List? toCorners(List>? data) { if (data == null) { @@ -12,38 +11,3 @@ List? toCorners(List>? data) { }), ); } - -BarcodeFormat toFormat(int value) { - switch (value) { - case 0: - return BarcodeFormat.all; - case 1: - return BarcodeFormat.code128; - case 2: - return BarcodeFormat.code39; - case 4: - return BarcodeFormat.code93; - case 8: - return BarcodeFormat.codebar; - case 16: - return BarcodeFormat.dataMatrix; - case 32: - return BarcodeFormat.ean13; - case 64: - return BarcodeFormat.ean8; - case 128: - return BarcodeFormat.itf; - case 256: - return BarcodeFormat.qrCode; - case 512: - return BarcodeFormat.upcA; - case 1024: - return BarcodeFormat.upcE; - case 2048: - return BarcodeFormat.pdf417; - case 4096: - return BarcodeFormat.aztec; - default: - return BarcodeFormat.unknown; - } -} diff --git a/lib/src/mobile_scanner_controller.dart b/lib/src/mobile_scanner_controller.dart index bcaf8d553..727c76792 100644 --- a/lib/src/mobile_scanner_controller.dart +++ b/lib/src/mobile_scanner_controller.dart @@ -436,7 +436,9 @@ class MobileScannerController { barcodes: [ Barcode( rawValue: (data as Map)['payload'] as String?, - format: toFormat(data['symbology'] as int), + format: BarcodeFormat.fromRawValue( + data['symbology'] as int? ?? -1, + ), ), ], ), @@ -452,7 +454,9 @@ class MobileScannerController { Barcode( rawValue: barcode['rawValue'] as String?, rawBytes: barcode['rawBytes'] as Uint8List?, - format: toFormat(barcode['format'] as int), + format: BarcodeFormat.fromRawValue( + barcode['format'] as int? ?? -1, + ), corners: toCorners( (barcode['corners'] as List? ?? []) .cast>(), From 7f214504c2e7420c1ab1217d5b4c3f53bae13288 Mon Sep 17 00:00:00 2001 From: Navaron Bracke Date: Mon, 23 Oct 2023 23:33:35 +0200 Subject: [PATCH 4/5] inline the toCorners implementation --- lib/src/barcode_utility.dart | 13 ------------- lib/src/mobile_scanner_controller.dart | 15 +++++++++------ 2 files changed, 9 insertions(+), 19 deletions(-) delete mode 100644 lib/src/barcode_utility.dart diff --git a/lib/src/barcode_utility.dart b/lib/src/barcode_utility.dart deleted file mode 100644 index 8241bc681..000000000 --- a/lib/src/barcode_utility.dart +++ /dev/null @@ -1,13 +0,0 @@ -import 'package:flutter/material.dart'; - -List? toCorners(List>? data) { - if (data == null) { - return null; - } - - return List.unmodifiable( - data.map((Map e) { - return Offset(e['x']! as double, e['y']! as double); - }), - ); -} diff --git a/lib/src/mobile_scanner_controller.dart b/lib/src/mobile_scanner_controller.dart index 727c76792..6404ea7f1 100644 --- a/lib/src/mobile_scanner_controller.dart +++ b/lib/src/mobile_scanner_controller.dart @@ -6,7 +6,6 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:mobile_scanner/mobile_scanner.dart'; -import 'package:mobile_scanner/src/barcode_utility.dart'; /// The [MobileScannerController] holds all the logic of this plugin, /// where as the [MobileScanner] class is the frontend of this plugin. @@ -446,6 +445,8 @@ class MobileScannerController { break; case 'barcodeWeb': final barcode = data as Map?; + final corners = barcode?['corners'] as List? ?? []; + _barcodesController.add( BarcodeCapture( raw: data, @@ -457,11 +458,13 @@ class MobileScannerController { format: BarcodeFormat.fromRawValue( barcode['format'] as int? ?? -1, ), - corners: toCorners( - (barcode['corners'] as List? ?? []) - .cast>(), - ) ?? - const [], + corners: List.unmodifiable( + corners.cast>().map( + (Map e) { + return Offset(e['x']! as double, e['y']! as double); + }, + ), + ), ), ], ), From 822ecf20368d43067dafd2f851c82836e70b9d04 Mon Sep 17 00:00:00 2001 From: Navaron Bracke Date: Mon, 23 Oct 2023 23:33:57 +0200 Subject: [PATCH 5/5] move changelog note to breaking changes section --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ef9a7cc6..b4298cee9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ ## NEXT +Breaking changes: +* The internal `fromNative()` methods now accept a `Map` instead of `Map`. + Improvements: * The `type` of an `Address` is now non-null. * The `type` of an `Email` is now non-null. @@ -10,7 +13,6 @@ Improvements: * The `width` and `height` of `BarcodeCapture` are now non-null. * The `BarcodeCapture` class now exposes a `size`. * The list of `corners` of a `Barcode` is now non-null. -* The internal `fromNative()` methods now accept a `Map` instead of `Map`. Bugs fixed: * Fixed the default values for the `format` and `type` arguments of the Barcode constructor.