Koji Wakamiya
Committed by Navaron Bracke

feat: Update extension type

@@ -9,12 +9,10 @@ import 'dart:js_interop'; @@ -9,12 +9,10 @@ import 'dart:js_interop';
9 /// 9 ///
10 /// Object literals can be made using [jsify]. 10 /// Object literals can be made using [jsify].
11 @JS('Map') 11 @JS('Map')
12 -@staticInterop  
13 -class JSMap<K extends JSAny, V extends JSAny> { 12 +extension type JSMap<K extends JSAny, V extends JSAny>._(JSObject _)
  13 + implements JSObject {
14 external factory JSMap(); 14 external factory JSMap();
15 -}  
16 15
17 -extension JSMapExtension<K extends JSAny, V extends JSAny> on JSMap<K, V> {  
18 external V? get(K key); 16 external V? get(K key);
19 external JSVoid set(K key, V? value); 17 external JSVoid set(K key, V? value);
20 } 18 }
@@ -10,32 +10,27 @@ import 'package:mobile_scanner/src/web/zxing/result_point.dart'; @@ -10,32 +10,27 @@ import 'package:mobile_scanner/src/web/zxing/result_point.dart';
10 /// The JS static interop class for the Result class in the ZXing library. 10 /// The JS static interop class for the Result class in the ZXing library.
11 /// 11 ///
12 /// See also: https://github.com/zxing-js/library/blob/master/src/core/Result.ts 12 /// See also: https://github.com/zxing-js/library/blob/master/src/core/Result.ts
13 -@JS()  
14 -@anonymous  
15 -@staticInterop  
16 -abstract class Result {}  
17 -  
18 -extension ResultExt on Result { 13 +extension type Result(JSObject _) implements JSObject {
19 @JS('barcodeFormat') 14 @JS('barcodeFormat')
20 - external JSNumber? get _barcodeFormat; 15 + external int? get _barcodeFormat;
21 16
22 - @JS('text')  
23 - external JSString? get _text; 17 + /// Get the text of the result.
  18 + external String? get text;
24 19
25 @JS('rawBytes') 20 @JS('rawBytes')
26 external JSUint8Array? get _rawBytes; 21 external JSUint8Array? get _rawBytes;
27 22
28 @JS('resultPoints') 23 @JS('resultPoints')
29 - external JSArray? get _resultPoints; 24 + external JSArray<ResultPoint>? get _resultPoints;
30 25
31 - @JS('timestamp')  
32 - external JSNumber? get _timestamp; 26 + /// Get the timestamp of the result.
  27 + external int? get timestamp;
33 28
34 /// Get the barcode format of the result. 29 /// Get the barcode format of the result.
35 /// 30 ///
36 /// See also https://github.com/zxing-js/library/blob/master/src/core/BarcodeFormat.ts 31 /// See also https://github.com/zxing-js/library/blob/master/src/core/BarcodeFormat.ts
37 BarcodeFormat get barcodeFormat { 32 BarcodeFormat get barcodeFormat {
38 - switch (_barcodeFormat?.toDartInt) { 33 + switch (_barcodeFormat) {
39 case 0: 34 case 0:
40 return BarcodeFormat.aztec; 35 return BarcodeFormat.aztec;
41 case 1: 36 case 1:
@@ -79,28 +74,22 @@ extension ResultExt on Result { @@ -79,28 +74,22 @@ extension ResultExt on Result {
79 } 74 }
80 } 75 }
81 76
  77 + /// Get the raw bytes of the result.
  78 + Uint8List? get rawBytes => _rawBytes?.toDart;
  79 +
82 /// Get the corner points of the result. 80 /// Get the corner points of the result.
83 List<Offset> get resultPoints { 81 List<Offset> get resultPoints {
84 - final JSArray? points = _resultPoints; 82 + final JSArray<ResultPoint>? points = _resultPoints;
85 83
86 if (points == null) { 84 if (points == null) {
87 - return []; 85 + return const [];
88 } 86 }
89 87
90 - return points.toDart.cast<ResultPoint>().map((point) { 88 + return points.toDart.map((point) {
91 return Offset(point.x, point.y); 89 return Offset(point.x, point.y);
92 }).toList(); 90 }).toList();
93 } 91 }
94 92
95 - /// Get the raw bytes of the result.  
96 - Uint8List? get rawBytes => _rawBytes?.toDart;  
97 -  
98 - /// Get the text of the result.  
99 - String? get text => _text?.toDart;  
100 -  
101 - /// Get the timestamp of the result.  
102 - int? get timestamp => _timestamp?.toDartInt;  
103 -  
104 /// Convert this result to a [Barcode]. 93 /// Convert this result to a [Barcode].
105 Barcode get toBarcode { 94 Barcode get toBarcode {
106 return Barcode( 95 return Barcode(
@@ -3,21 +3,10 @@ import 'dart:js_interop'; @@ -3,21 +3,10 @@ import 'dart:js_interop';
3 /// The JS static interop class for the Result class in the ZXing library. 3 /// The JS static interop class for the Result class in the ZXing library.
4 /// 4 ///
5 /// See also: https://github.com/zxing-js/library/blob/master/src/core/ResultPoint.ts 5 /// See also: https://github.com/zxing-js/library/blob/master/src/core/ResultPoint.ts
6 -@JS()  
7 -@anonymous  
8 -@staticInterop  
9 -abstract class ResultPoint {}  
10 -  
11 -extension ResultPointExt on ResultPoint {  
12 - @JS('x')  
13 - external JSNumber get _x;  
14 -  
15 - @JS('y')  
16 - external JSNumber get _y;  
17 - 6 +extension type ResultPoint(JSObject _) implements JSObject {
18 /// The x coordinate of the point. 7 /// The x coordinate of the point.
19 - double get x => _x.toDartDouble; 8 + external double get x;
20 9
21 /// The y coordinate of the point. 10 /// The y coordinate of the point.
22 - double get y => _y.toDartDouble; 11 + external double get y;
23 } 12 }
@@ -48,42 +48,6 @@ final class ZXingBarcodeReader extends BarcodeReader { @@ -48,42 +48,6 @@ final class ZXingBarcodeReader extends BarcodeReader {
48 @override 48 @override
49 String get scriptUrl => 'https://unpkg.com/@zxing/library@0.19.1'; 49 String get scriptUrl => 'https://unpkg.com/@zxing/library@0.19.1';
50 50
51 - /// Get the barcode format from the ZXing library, for the given [format].  
52 - static int getZXingBarcodeFormat(BarcodeFormat format) {  
53 - switch (format) {  
54 - case BarcodeFormat.aztec:  
55 - return 0;  
56 - case BarcodeFormat.codabar:  
57 - return 1;  
58 - case BarcodeFormat.code39:  
59 - return 2;  
60 - case BarcodeFormat.code93:  
61 - return 3;  
62 - case BarcodeFormat.code128:  
63 - return 4;  
64 - case BarcodeFormat.dataMatrix:  
65 - return 5;  
66 - case BarcodeFormat.ean8:  
67 - return 6;  
68 - case BarcodeFormat.ean13:  
69 - return 7;  
70 - case BarcodeFormat.itf:  
71 - return 8;  
72 - case BarcodeFormat.pdf417:  
73 - return 10;  
74 - case BarcodeFormat.qrCode:  
75 - return 11;  
76 - case BarcodeFormat.upcA:  
77 - return 14;  
78 - case BarcodeFormat.upcE:  
79 - return 15;  
80 - case BarcodeFormat.unknown:  
81 - case BarcodeFormat.all:  
82 - default:  
83 - return -1;  
84 - }  
85 - }  
86 -  
87 JSMap? _createReaderHints(List<BarcodeFormat> formats) { 51 JSMap? _createReaderHints(List<BarcodeFormat> formats) {
88 if (formats.isEmpty || formats.contains(BarcodeFormat.all)) { 52 if (formats.isEmpty || formats.contains(BarcodeFormat.all)) {
89 return null; 53 return null;
@@ -96,8 +60,7 @@ final class ZXingBarcodeReader extends BarcodeReader { @@ -96,8 +60,7 @@ final class ZXingBarcodeReader extends BarcodeReader {
96 hints.set( 60 hints.set(
97 2.toJS, 61 2.toJS,
98 [ 62 [
99 - for (final BarcodeFormat format in formats)  
100 - getZXingBarcodeFormat(format).toJS, 63 + for (final BarcodeFormat format in formats) format.toJS,
101 ].toJS, 64 ].toJS,
102 ); 65 );
103 66
@@ -185,7 +148,7 @@ final class ZXingBarcodeReader extends BarcodeReader { @@ -185,7 +148,7 @@ final class ZXingBarcodeReader extends BarcodeReader {
185 148
186 _reader = ZXingBrowserMultiFormatReader( 149 _reader = ZXingBrowserMultiFormatReader(
187 _createReaderHints(formats), 150 _createReaderHints(formats),
188 - detectionTimeoutMs.toJS, 151 + detectionTimeoutMs,
189 ); 152 );
190 153
191 await _prepareVideoElement(videoElement, videoStream); 154 await _prepareVideoElement(videoElement, videoStream);
@@ -199,3 +162,24 @@ final class ZXingBarcodeReader extends BarcodeReader { @@ -199,3 +162,24 @@ final class ZXingBarcodeReader extends BarcodeReader {
199 _reader = null; 162 _reader = null;
200 } 163 }
201 } 164 }
  165 +
  166 +extension on BarcodeFormat {
  167 + /// Get the barcode format from the ZXing library.
  168 + JSNumber get toJS => switch (this) {
  169 + BarcodeFormat.aztec => 0,
  170 + BarcodeFormat.codabar => 1,
  171 + BarcodeFormat.code39 => 2,
  172 + BarcodeFormat.code93 => 3,
  173 + BarcodeFormat.code128 => 4,
  174 + BarcodeFormat.dataMatrix => 5,
  175 + BarcodeFormat.ean8 => 6,
  176 + BarcodeFormat.ean13 => 7,
  177 + BarcodeFormat.itf => 8,
  178 + BarcodeFormat.pdf417 => 10,
  179 + BarcodeFormat.qrCode => 11,
  180 + BarcodeFormat.upcA => 14,
  181 + BarcodeFormat.upcE => 15,
  182 + BarcodeFormat.unknown || BarcodeFormat.all || _ => -1,
  183 + }
  184 + .toJS;
  185 +}
@@ -7,8 +7,7 @@ import 'package:web/web.dart'; @@ -7,8 +7,7 @@ import 'package:web/web.dart';
7 /// 7 ///
8 /// See https://github.com/zxing-js/library/blob/master/src/browser/BrowserMultiFormatReader.ts 8 /// See https://github.com/zxing-js/library/blob/master/src/browser/BrowserMultiFormatReader.ts
9 @JS('ZXing.BrowserMultiFormatReader') 9 @JS('ZXing.BrowserMultiFormatReader')
10 -@staticInterop  
11 -class ZXingBrowserMultiFormatReader { 10 +extension type ZXingBrowserMultiFormatReader._(JSObject _) implements JSObject {
12 /// Construct a new `ZXing.BrowserMultiFormatReader`. 11 /// Construct a new `ZXing.BrowserMultiFormatReader`.
13 /// 12 ///
14 /// The [hints] are the configuration options for the reader. 13 /// The [hints] are the configuration options for the reader.
@@ -17,11 +16,9 @@ class ZXingBrowserMultiFormatReader { @@ -17,11 +16,9 @@ class ZXingBrowserMultiFormatReader {
17 /// See also: https://github.com/zxing-js/library/blob/master/src/core/DecodeHintType.ts 16 /// See also: https://github.com/zxing-js/library/blob/master/src/core/DecodeHintType.ts
18 external factory ZXingBrowserMultiFormatReader( 17 external factory ZXingBrowserMultiFormatReader(
19 JSMap? hints, 18 JSMap? hints,
20 - JSNumber? timeBetweenScansMillis, 19 + int timeBetweenScansMillis,
21 ); 20 );
22 -}  
23 21
24 -extension ZXingBrowserMultiFormatReaderExt on ZXingBrowserMultiFormatReader {  
25 /// Attach a [MediaStream] to a [HTMLVideoElement]. 22 /// Attach a [MediaStream] to a [HTMLVideoElement].
26 /// 23 ///
27 /// This function accepts a [MediaStream] and a [HTMLVideoElement] as arguments, 24 /// This function accepts a [MediaStream] and a [HTMLVideoElement] as arguments,