Showing
5 changed files
with
57 additions
and
24 deletions
| @@ -96,7 +96,9 @@ abstract class BarcodeReader { | @@ -96,7 +96,9 @@ abstract class BarcodeReader { | ||
| 96 | } | 96 | } |
| 97 | 97 | ||
| 98 | /// Set a listener for the media stream settings. | 98 | /// Set a listener for the media stream settings. |
| 99 | - void setMediaTrackSettingsListener(void Function(MediaTrackSettings) listener) { | 99 | + void setMediaTrackSettingsListener( |
| 100 | + void Function(MediaTrackSettings) listener, | ||
| 101 | + ) { | ||
| 100 | throw UnimplementedError( | 102 | throw UnimplementedError( |
| 101 | 'setMediaTrackConstraintsListener() has not been implemented.', | 103 | 'setMediaTrackConstraintsListener() has not been implemented.', |
| 102 | ); | 104 | ); |
| @@ -10,9 +10,9 @@ final class MediaTrackConstraintsDelegate { | @@ -10,9 +10,9 @@ final class MediaTrackConstraintsDelegate { | ||
| 10 | 10 | ||
| 11 | /// Get the settings for the given [mediaStream]. | 11 | /// Get the settings for the given [mediaStream]. |
| 12 | MediaTrackSettings? getSettings(MediaStream? mediaStream) { | 12 | MediaTrackSettings? getSettings(MediaStream? mediaStream) { |
| 13 | - final List<JSAny?> tracks = mediaStream?.getVideoTracks().toDart ?? const []; | 13 | + final List<JSAny?>? tracks = mediaStream?.getVideoTracks().toDart; |
| 14 | 14 | ||
| 15 | - if (tracks.isEmpty) { | 15 | + if (tracks == null || tracks.isEmpty) { |
| 16 | return null; | 16 | return null; |
| 17 | } | 17 | } |
| 18 | 18 | ||
| @@ -24,7 +24,9 @@ final class MediaTrackConstraintsDelegate { | @@ -24,7 +24,9 @@ final class MediaTrackConstraintsDelegate { | ||
| 24 | /// Returns a list of supported flashlight modes for the given [mediaStream]. | 24 | /// Returns a list of supported flashlight modes for the given [mediaStream]. |
| 25 | /// | 25 | /// |
| 26 | /// The [TorchState.off] mode is always supported, regardless of the return value. | 26 | /// The [TorchState.off] mode is always supported, regardless of the return value. |
| 27 | - Future<List<TorchState>> getSupportedFlashlightModes(MediaStream? mediaStream) async { | 27 | + Future<List<TorchState>> getSupportedFlashlightModes( |
| 28 | + MediaStream? mediaStream, | ||
| 29 | + ) async { | ||
| 28 | if (mediaStream == null) { | 30 | if (mediaStream == null) { |
| 29 | return []; | 31 | return []; |
| 30 | } | 32 | } |
| @@ -61,7 +63,10 @@ final class MediaTrackConstraintsDelegate { | @@ -61,7 +63,10 @@ final class MediaTrackConstraintsDelegate { | ||
| 61 | } | 63 | } |
| 62 | 64 | ||
| 63 | /// Set the flashlight state of the given [mediaStream] to the given [value]. | 65 | /// Set the flashlight state of the given [mediaStream] to the given [value]. |
| 64 | - Future<void> setFlashlightState(MediaStream? mediaStream, TorchState value) async { | 66 | + Future<void> setFlashlightState( |
| 67 | + MediaStream? mediaStream, | ||
| 68 | + TorchState value, | ||
| 69 | + ) async { | ||
| 65 | if (mediaStream == null) { | 70 | if (mediaStream == null) { |
| 66 | return; | 71 | return; |
| 67 | } | 72 | } |
| @@ -23,7 +23,8 @@ class MobileScannerWeb extends MobileScannerPlatform { | @@ -23,7 +23,8 @@ class MobileScannerWeb extends MobileScannerPlatform { | ||
| 23 | final BarcodeReader _barcodeReader = ZXingBarcodeReader(); | 23 | final BarcodeReader _barcodeReader = ZXingBarcodeReader(); |
| 24 | 24 | ||
| 25 | /// The stream controller for the barcode stream. | 25 | /// The stream controller for the barcode stream. |
| 26 | - final StreamController<BarcodeCapture> _barcodesController = StreamController.broadcast(); | 26 | + final StreamController<BarcodeCapture> _barcodesController = |
| 27 | + StreamController.broadcast(); | ||
| 27 | 28 | ||
| 28 | /// The subscription for the barcode stream. | 29 | /// The subscription for the barcode stream. |
| 29 | StreamSubscription<Object?>? _barcodesSubscription; | 30 | StreamSubscription<Object?>? _barcodesSubscription; |
| @@ -34,7 +35,8 @@ class MobileScannerWeb extends MobileScannerPlatform { | @@ -34,7 +35,8 @@ class MobileScannerWeb extends MobileScannerPlatform { | ||
| 34 | HTMLDivElement? _divElement; | 35 | HTMLDivElement? _divElement; |
| 35 | 36 | ||
| 36 | /// The stream controller for the media track settings stream. | 37 | /// The stream controller for the media track settings stream. |
| 37 | - final StreamController<MediaTrackSettings> _settingsController = StreamController.broadcast(); | 38 | + final StreamController<MediaTrackSettings> _settingsController = |
| 39 | + StreamController.broadcast(); | ||
| 38 | 40 | ||
| 39 | /// The view type for the platform view factory. | 41 | /// The view type for the platform view factory. |
| 40 | final String _viewType = 'MobileScannerWeb'; | 42 | final String _viewType = 'MobileScannerWeb'; |
| @@ -70,7 +72,8 @@ class MobileScannerWeb extends MobileScannerPlatform { | @@ -70,7 +72,8 @@ class MobileScannerWeb extends MobileScannerPlatform { | ||
| 70 | throw const MobileScannerException( | 72 | throw const MobileScannerException( |
| 71 | errorCode: MobileScannerErrorCode.controllerUninitialized, | 73 | errorCode: MobileScannerErrorCode.controllerUninitialized, |
| 72 | errorDetails: MobileScannerErrorDetails( | 74 | errorDetails: MobileScannerErrorDetails( |
| 73 | - message: 'The controller was not yet initialized. Call start() before calling buildCameraView().', | 75 | + message: |
| 76 | + 'The controller was not yet initialized. Call start() before calling buildCameraView().', | ||
| 74 | ), | 77 | ), |
| 75 | ); | 78 | ); |
| 76 | } | 79 | } |
| @@ -98,7 +101,8 @@ class MobileScannerWeb extends MobileScannerPlatform { | @@ -98,7 +101,8 @@ class MobileScannerWeb extends MobileScannerPlatform { | ||
| 98 | throw const MobileScannerException( | 101 | throw const MobileScannerException( |
| 99 | errorCode: MobileScannerErrorCode.controllerAlreadyInitialized, | 102 | errorCode: MobileScannerErrorCode.controllerAlreadyInitialized, |
| 100 | errorDetails: MobileScannerErrorDetails( | 103 | errorDetails: MobileScannerErrorDetails( |
| 101 | - message: 'The scanner was already started. Call stop() before calling start() again.', | 104 | + message: |
| 105 | + 'The scanner was already started. Call stop() before calling start() again.', | ||
| 102 | ), | 106 | ), |
| 103 | ); | 107 | ); |
| 104 | } | 108 | } |
| @@ -108,7 +112,9 @@ class MobileScannerWeb extends MobileScannerPlatform { | @@ -108,7 +112,9 @@ class MobileScannerWeb extends MobileScannerPlatform { | ||
| 108 | _barcodesController.add(const BarcodeCapture()); | 112 | _barcodesController.add(const BarcodeCapture()); |
| 109 | 113 | ||
| 110 | // Listen for changes to the media track settings. | 114 | // Listen for changes to the media track settings. |
| 111 | - _barcodeReader.setMediaTrackSettingsListener(_handleMediaTrackSettingsChange); | 115 | + _barcodeReader.setMediaTrackSettingsListener( |
| 116 | + _handleMediaTrackSettingsChange, | ||
| 117 | + ); | ||
| 112 | 118 | ||
| 113 | await _barcodeReader.start( | 119 | await _barcodeReader.start( |
| 114 | startOptions, | 120 | startOptions, |
| @@ -120,7 +126,8 @@ class MobileScannerWeb extends MobileScannerPlatform { | @@ -120,7 +126,8 @@ class MobileScannerWeb extends MobileScannerPlatform { | ||
| 120 | MobileScannerErrorCode errorCode = MobileScannerErrorCode.genericError; | 126 | MobileScannerErrorCode errorCode = MobileScannerErrorCode.genericError; |
| 121 | 127 | ||
| 122 | if (error is DOMException) { | 128 | if (error is DOMException) { |
| 123 | - if (errorMessage.contains('NotFoundError') || errorMessage.contains('NotSupportedError')) { | 129 | + if (errorMessage.contains('NotFoundError') || |
| 130 | + errorMessage.contains('NotSupportedError')) { | ||
| 124 | errorCode = MobileScannerErrorCode.unsupported; | 131 | errorCode = MobileScannerErrorCode.unsupported; |
| 125 | } else if (errorMessage.contains('NotAllowedError')) { | 132 | } else if (errorMessage.contains('NotAllowedError')) { |
| 126 | errorCode = MobileScannerErrorCode.permissionDenied; | 133 | errorCode = MobileScannerErrorCode.permissionDenied; |
| @@ -137,13 +144,15 @@ class MobileScannerWeb extends MobileScannerPlatform { | @@ -137,13 +144,15 @@ class MobileScannerWeb extends MobileScannerPlatform { | ||
| 137 | } | 144 | } |
| 138 | 145 | ||
| 139 | try { | 146 | try { |
| 140 | - _barcodesSubscription = _barcodeReader.detectBarcodes().listen((BarcodeCapture barcode) { | 147 | + _barcodesSubscription = _barcodeReader.detectBarcodes().listen( |
| 148 | + (BarcodeCapture barcode) { | ||
| 141 | if (_barcodesController.isClosed) { | 149 | if (_barcodesController.isClosed) { |
| 142 | return; | 150 | return; |
| 143 | } | 151 | } |
| 144 | 152 | ||
| 145 | _barcodesController.add(barcode); | 153 | _barcodesController.add(barcode); |
| 146 | - }); | 154 | + }, |
| 155 | + ); | ||
| 147 | 156 | ||
| 148 | final bool hasTorch = await _barcodeReader.hasTorch(); | 157 | final bool hasTorch = await _barcodeReader.hasTorch(); |
| 149 | 158 |
| @@ -95,7 +95,8 @@ extension ResultExt on Result { | @@ -95,7 +95,8 @@ extension ResultExt on Result { | ||
| 95 | 95 | ||
| 96 | /// Get the raw bytes of the result. | 96 | /// Get the raw bytes of the result. |
| 97 | Uint8List? get rawBytes { | 97 | Uint8List? get rawBytes { |
| 98 | - final JSUint8Array? rawBytes = getRawBytes.callAsFunction() as JSUint8Array?; | 98 | + final JSUint8Array? rawBytes = |
| 99 | + getRawBytes.callAsFunction() as JSUint8Array?; | ||
| 99 | 100 | ||
| 100 | return rawBytes?.toDart; | 101 | return rawBytes?.toDart; |
| 101 | } | 102 | } |
| @@ -22,7 +22,8 @@ final class ZXingBarcodeReader extends BarcodeReader { | @@ -22,7 +22,8 @@ final class ZXingBarcodeReader extends BarcodeReader { | ||
| 22 | void Function(web.MediaTrackSettings)? _onMediaTrackSettingsChanged; | 22 | void Function(web.MediaTrackSettings)? _onMediaTrackSettingsChanged; |
| 23 | 23 | ||
| 24 | /// The internal media stream track constraints delegate. | 24 | /// The internal media stream track constraints delegate. |
| 25 | - final MediaTrackConstraintsDelegate _mediaTrackConstraintsDelegate = const MediaTrackConstraintsDelegate(); | 25 | + final MediaTrackConstraintsDelegate _mediaTrackConstraintsDelegate = |
| 26 | + const MediaTrackConstraintsDelegate(); | ||
| 26 | 27 | ||
| 27 | /// The internal barcode reader. | 28 | /// The internal barcode reader. |
| 28 | ZXingBrowserMultiFormatReader? _reader; | 29 | ZXingBrowserMultiFormatReader? _reader; |
| @@ -93,7 +94,8 @@ final class ZXingBarcodeReader extends BarcodeReader { | @@ -93,7 +94,8 @@ final class ZXingBarcodeReader extends BarcodeReader { | ||
| 93 | return null; | 94 | return null; |
| 94 | } | 95 | } |
| 95 | 96 | ||
| 96 | - final capabilities = web.window.navigator.mediaDevices.getSupportedConstraints(); | 97 | + final capabilities = |
| 98 | + web.window.navigator.mediaDevices.getSupportedConstraints(); | ||
| 97 | 99 | ||
| 98 | final web.MediaStreamConstraints constraints; | 100 | final web.MediaStreamConstraints constraints; |
| 99 | 101 | ||
| @@ -112,7 +114,9 @@ final class ZXingBarcodeReader extends BarcodeReader { | @@ -112,7 +114,9 @@ final class ZXingBarcodeReader extends BarcodeReader { | ||
| 112 | ); | 114 | ); |
| 113 | } | 115 | } |
| 114 | 116 | ||
| 115 | - final JSAny? mediaStream = await web.window.navigator.mediaDevices.getUserMedia(constraints).toDart; | 117 | + final JSAny? mediaStream = await web.window.navigator.mediaDevices |
| 118 | + .getUserMedia(constraints) | ||
| 119 | + .toDart; | ||
| 116 | 120 | ||
| 117 | return mediaStream as web.MediaStream?; | 121 | return mediaStream as web.MediaStream?; |
| 118 | } | 122 | } |
| @@ -135,11 +139,13 @@ final class ZXingBarcodeReader extends BarcodeReader { | @@ -135,11 +139,13 @@ final class ZXingBarcodeReader extends BarcodeReader { | ||
| 135 | final web.MediaStream? stream = await _prepareMediaStream(cameraDirection); | 139 | final web.MediaStream? stream = await _prepareMediaStream(cameraDirection); |
| 136 | 140 | ||
| 137 | if (stream != null) { | 141 | if (stream != null) { |
| 138 | - final JSPromise? result = _reader?.attachStreamToVideo.callAsFunction(null, stream, videoElement) as JSPromise?; | 142 | + final JSPromise? result = _reader?.attachStreamToVideo |
| 143 | + .callAsFunction(null, stream, videoElement) as JSPromise?; | ||
| 139 | 144 | ||
| 140 | await result?.toDart; | 145 | await result?.toDart; |
| 141 | 146 | ||
| 142 | - final web.MediaTrackSettings? settings = _mediaTrackConstraintsDelegate.getSettings(stream); | 147 | + final web.MediaTrackSettings? settings = |
| 148 | + _mediaTrackConstraintsDelegate.getSettings(stream); | ||
| 143 | 149 | ||
| 144 | if (settings != null) { | 150 | if (settings != null) { |
| 145 | _onMediaTrackSettingsChanged?.call(settings); | 151 | _onMediaTrackSettingsChanged?.call(settings); |
| @@ -193,7 +199,9 @@ final class ZXingBarcodeReader extends BarcodeReader { | @@ -193,7 +199,9 @@ final class ZXingBarcodeReader extends BarcodeReader { | ||
| 193 | } | 199 | } |
| 194 | 200 | ||
| 195 | @override | 201 | @override |
| 196 | - void setMediaTrackSettingsListener(void Function(web.MediaTrackSettings) listener) { | 202 | + void setMediaTrackSettingsListener( |
| 203 | + void Function(web.MediaTrackSettings) listener, | ||
| 204 | + ) { | ||
| 197 | _onMediaTrackSettingsChanged ??= listener; | 205 | _onMediaTrackSettingsChanged ??= listener; |
| 198 | } | 206 | } |
| 199 | 207 | ||
| @@ -210,9 +218,13 @@ final class ZXingBarcodeReader extends BarcodeReader { | @@ -210,9 +218,13 @@ final class ZXingBarcodeReader extends BarcodeReader { | ||
| 210 | return Future<void>.value(); | 218 | return Future<void>.value(); |
| 211 | } | 219 | } |
| 212 | 220 | ||
| 213 | - await _mediaTrackConstraintsDelegate.setFlashlightState(mediaStream, value); | 221 | + await _mediaTrackConstraintsDelegate.setFlashlightState( |
| 222 | + mediaStream, | ||
| 223 | + value, | ||
| 224 | + ); | ||
| 214 | 225 | ||
| 215 | - final web.MediaTrackSettings? settings = _mediaTrackConstraintsDelegate.getSettings(mediaStream); | 226 | + final web.MediaTrackSettings? settings = |
| 227 | + _mediaTrackConstraintsDelegate.getSettings(mediaStream); | ||
| 216 | 228 | ||
| 217 | if (settings != null) { | 229 | if (settings != null) { |
| 218 | _onMediaTrackSettingsChanged?.call(settings); | 230 | _onMediaTrackSettingsChanged?.call(settings); |
| @@ -221,7 +233,10 @@ final class ZXingBarcodeReader extends BarcodeReader { | @@ -221,7 +233,10 @@ final class ZXingBarcodeReader extends BarcodeReader { | ||
| 221 | } | 233 | } |
| 222 | 234 | ||
| 223 | @override | 235 | @override |
| 224 | - Future<void> start(StartOptions options, {required web.HTMLElement containerElement}) async { | 236 | + Future<void> start( |
| 237 | + StartOptions options, { | ||
| 238 | + required web.HTMLElement containerElement, | ||
| 239 | + }) async { | ||
| 225 | final int detectionTimeoutMs = options.detectionTimeoutMs; | 240 | final int detectionTimeoutMs = options.detectionTimeoutMs; |
| 226 | final List<BarcodeFormat> formats = options.formats; | 241 | final List<BarcodeFormat> formats = options.formats; |
| 227 | 242 | ||
| @@ -243,7 +258,8 @@ final class ZXingBarcodeReader extends BarcodeReader { | @@ -243,7 +258,8 @@ final class ZXingBarcodeReader extends BarcodeReader { | ||
| 243 | 258 | ||
| 244 | _reader = ZXingBrowserMultiFormatReader(hints.jsify(), detectionTimeoutMs); | 259 | _reader = ZXingBrowserMultiFormatReader(hints.jsify(), detectionTimeoutMs); |
| 245 | 260 | ||
| 246 | - final web.HTMLVideoElement videoElement = web.document.createElement('video') as web.HTMLVideoElement; | 261 | + final web.HTMLVideoElement videoElement = |
| 262 | + web.document.createElement('video') as web.HTMLVideoElement; | ||
| 247 | 263 | ||
| 248 | await _prepareVideoElement( | 264 | await _prepareVideoElement( |
| 249 | videoElement, | 265 | videoElement, |
-
Please register or login to post a comment