Koji Wakamiya
Committed by Navaron Bracke

feat: Update extension type

... ... @@ -9,12 +9,10 @@ import 'dart:js_interop';
///
/// Object literals can be made using [jsify].
@JS('Map')
@staticInterop
class JSMap<K extends JSAny, V extends JSAny> {
extension type JSMap<K extends JSAny, V extends JSAny>._(JSObject _)
implements JSObject {
external factory JSMap();
}
extension JSMapExtension<K extends JSAny, V extends JSAny> on JSMap<K, V> {
external V? get(K key);
external JSVoid set(K key, V? value);
}
... ...
... ... @@ -10,32 +10,27 @@ import 'package:mobile_scanner/src/web/zxing/result_point.dart';
/// The JS static interop class for the Result class in the ZXing library.
///
/// See also: https://github.com/zxing-js/library/blob/master/src/core/Result.ts
@JS()
@anonymous
@staticInterop
abstract class Result {}
extension ResultExt on Result {
extension type Result(JSObject _) implements JSObject {
@JS('barcodeFormat')
external JSNumber? get _barcodeFormat;
external int? get _barcodeFormat;
@JS('text')
external JSString? get _text;
/// Get the text of the result.
external String? get text;
@JS('rawBytes')
external JSUint8Array? get _rawBytes;
@JS('resultPoints')
external JSArray? get _resultPoints;
external JSArray<ResultPoint>? get _resultPoints;
@JS('timestamp')
external JSNumber? get _timestamp;
/// Get the timestamp of the result.
external int? get timestamp;
/// Get the barcode format of the result.
///
/// See also https://github.com/zxing-js/library/blob/master/src/core/BarcodeFormat.ts
BarcodeFormat get barcodeFormat {
switch (_barcodeFormat?.toDartInt) {
switch (_barcodeFormat) {
case 0:
return BarcodeFormat.aztec;
case 1:
... ... @@ -79,28 +74,22 @@ extension ResultExt on Result {
}
}
/// Get the raw bytes of the result.
Uint8List? get rawBytes => _rawBytes?.toDart;
/// Get the corner points of the result.
List<Offset> get resultPoints {
final JSArray? points = _resultPoints;
final JSArray<ResultPoint>? points = _resultPoints;
if (points == null) {
return [];
return const [];
}
return points.toDart.cast<ResultPoint>().map((point) {
return points.toDart.map((point) {
return Offset(point.x, point.y);
}).toList();
}
/// Get the raw bytes of the result.
Uint8List? get rawBytes => _rawBytes?.toDart;
/// Get the text of the result.
String? get text => _text?.toDart;
/// Get the timestamp of the result.
int? get timestamp => _timestamp?.toDartInt;
/// Convert this result to a [Barcode].
Barcode get toBarcode {
return Barcode(
... ...
... ... @@ -3,21 +3,10 @@ import 'dart:js_interop';
/// The JS static interop class for the Result class in the ZXing library.
///
/// See also: https://github.com/zxing-js/library/blob/master/src/core/ResultPoint.ts
@JS()
@anonymous
@staticInterop
abstract class ResultPoint {}
extension ResultPointExt on ResultPoint {
@JS('x')
external JSNumber get _x;
@JS('y')
external JSNumber get _y;
extension type ResultPoint(JSObject _) implements JSObject {
/// The x coordinate of the point.
double get x => _x.toDartDouble;
external double get x;
/// The y coordinate of the point.
double get y => _y.toDartDouble;
external double get y;
}
... ...
... ... @@ -48,42 +48,6 @@ final class ZXingBarcodeReader extends BarcodeReader {
@override
String get scriptUrl => 'https://unpkg.com/@zxing/library@0.19.1';
/// Get the barcode format from the ZXing library, for the given [format].
static int getZXingBarcodeFormat(BarcodeFormat format) {
switch (format) {
case BarcodeFormat.aztec:
return 0;
case BarcodeFormat.codabar:
return 1;
case BarcodeFormat.code39:
return 2;
case BarcodeFormat.code93:
return 3;
case BarcodeFormat.code128:
return 4;
case BarcodeFormat.dataMatrix:
return 5;
case BarcodeFormat.ean8:
return 6;
case BarcodeFormat.ean13:
return 7;
case BarcodeFormat.itf:
return 8;
case BarcodeFormat.pdf417:
return 10;
case BarcodeFormat.qrCode:
return 11;
case BarcodeFormat.upcA:
return 14;
case BarcodeFormat.upcE:
return 15;
case BarcodeFormat.unknown:
case BarcodeFormat.all:
default:
return -1;
}
}
JSMap? _createReaderHints(List<BarcodeFormat> formats) {
if (formats.isEmpty || formats.contains(BarcodeFormat.all)) {
return null;
... ... @@ -96,8 +60,7 @@ final class ZXingBarcodeReader extends BarcodeReader {
hints.set(
2.toJS,
[
for (final BarcodeFormat format in formats)
getZXingBarcodeFormat(format).toJS,
for (final BarcodeFormat format in formats) format.toJS,
].toJS,
);
... ... @@ -185,7 +148,7 @@ final class ZXingBarcodeReader extends BarcodeReader {
_reader = ZXingBrowserMultiFormatReader(
_createReaderHints(formats),
detectionTimeoutMs.toJS,
detectionTimeoutMs,
);
await _prepareVideoElement(videoElement, videoStream);
... ... @@ -199,3 +162,24 @@ final class ZXingBarcodeReader extends BarcodeReader {
_reader = null;
}
}
extension on BarcodeFormat {
/// Get the barcode format from the ZXing library.
JSNumber get toJS => switch (this) {
BarcodeFormat.aztec => 0,
BarcodeFormat.codabar => 1,
BarcodeFormat.code39 => 2,
BarcodeFormat.code93 => 3,
BarcodeFormat.code128 => 4,
BarcodeFormat.dataMatrix => 5,
BarcodeFormat.ean8 => 6,
BarcodeFormat.ean13 => 7,
BarcodeFormat.itf => 8,
BarcodeFormat.pdf417 => 10,
BarcodeFormat.qrCode => 11,
BarcodeFormat.upcA => 14,
BarcodeFormat.upcE => 15,
BarcodeFormat.unknown || BarcodeFormat.all || _ => -1,
}
.toJS;
}
... ...
... ... @@ -7,8 +7,7 @@ import 'package:web/web.dart';
///
/// See https://github.com/zxing-js/library/blob/master/src/browser/BrowserMultiFormatReader.ts
@JS('ZXing.BrowserMultiFormatReader')
@staticInterop
class ZXingBrowserMultiFormatReader {
extension type ZXingBrowserMultiFormatReader._(JSObject _) implements JSObject {
/// Construct a new `ZXing.BrowserMultiFormatReader`.
///
/// The [hints] are the configuration options for the reader.
... ... @@ -17,11 +16,9 @@ class ZXingBrowserMultiFormatReader {
/// See also: https://github.com/zxing-js/library/blob/master/src/core/DecodeHintType.ts
external factory ZXingBrowserMultiFormatReader(
JSMap? hints,
JSNumber? timeBetweenScansMillis,
int timeBetweenScansMillis,
);
}
extension ZXingBrowserMultiFormatReaderExt on ZXingBrowserMultiFormatReader {
/// Attach a [MediaStream] to a [HTMLVideoElement].
///
/// This function accepts a [MediaStream] and a [HTMLVideoElement] as arguments,
... ...