Showing
1 changed file
with
78 additions
and
0 deletions
lib/src/web/barcode_reader.dart
0 → 100644
| 1 | +import 'dart:async'; | ||
| 2 | +import 'dart:js_interop'; | ||
| 3 | + | ||
| 4 | +import 'package:js/js.dart'; | ||
| 5 | +import 'package:mobile_scanner/src/enums/mobile_scanner_error_code.dart'; | ||
| 6 | +import 'package:mobile_scanner/src/enums/torch_state.dart'; | ||
| 7 | +import 'package:mobile_scanner/src/mobile_scanner_exception.dart'; | ||
| 8 | +import 'package:web/web.dart'; | ||
| 9 | + | ||
| 10 | +/// This class represents the base interface for a barcode reader implementation. | ||
| 11 | +abstract class BarcodeReader { | ||
| 12 | + const BarcodeReader(); | ||
| 13 | + | ||
| 14 | + /// The id for the script tag that loads the barcode library. | ||
| 15 | + /// | ||
| 16 | + /// If a script tag with this id already exists, | ||
| 17 | + /// the library will not be loaded again. | ||
| 18 | + String get scriptId => 'mobile-scanner-barcode-reader'; | ||
| 19 | + | ||
| 20 | + /// The script url for the barcode library. | ||
| 21 | + String get scriptUrl; | ||
| 22 | + | ||
| 23 | + /// Load the barcode reader library. | ||
| 24 | + /// | ||
| 25 | + /// Does nothing if the library is already loaded. | ||
| 26 | + Future<void> maybeLoadLibrary() async { | ||
| 27 | + // Script already exists. | ||
| 28 | + if (document.querySelector('script#$scriptId') != null) { | ||
| 29 | + return; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + final Completer<void> completer = Completer(); | ||
| 33 | + | ||
| 34 | + final HTMLScriptElement script = (document.createElement('script') as HTMLScriptElement) | ||
| 35 | + ..id = scriptId | ||
| 36 | + ..async = true | ||
| 37 | + ..defer = false | ||
| 38 | + ..type = 'application/javascript' | ||
| 39 | + ..lang = 'javascript' | ||
| 40 | + ..crossOrigin = 'anonymous' | ||
| 41 | + ..src = scriptUrl | ||
| 42 | + ..onload = allowInterop((JSAny _) { | ||
| 43 | + if (!completer.isCompleted) { | ||
| 44 | + completer.complete(); | ||
| 45 | + } | ||
| 46 | + }).toJS; | ||
| 47 | + | ||
| 48 | + script.onerror = allowInterop((JSAny _) { | ||
| 49 | + if (!completer.isCompleted) { | ||
| 50 | + // Remove the script if it did not load. | ||
| 51 | + document.head!.removeChild(script); | ||
| 52 | + | ||
| 53 | + completer.completeError( | ||
| 54 | + const MobileScannerException( | ||
| 55 | + errorCode: MobileScannerErrorCode.genericError, | ||
| 56 | + errorDetails: MobileScannerErrorDetails( | ||
| 57 | + message: 'Could not load the BarcodeReader script due to a network error.', | ||
| 58 | + ), | ||
| 59 | + ), | ||
| 60 | + ); | ||
| 61 | + } | ||
| 62 | + }).toJS; | ||
| 63 | + | ||
| 64 | + document.head!.appendChild(script); | ||
| 65 | + | ||
| 66 | + await completer.future; | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + /// Set the flashlight state for the device. | ||
| 70 | + Future<void> setTorchState(TorchState torchState) { | ||
| 71 | + throw UnimplementedError('setTorchState() has not been implemented.'); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + /// Stop the barcode reader and dispose of the video stream. | ||
| 75 | + Future<void> stop() { | ||
| 76 | + throw UnimplementedError('stop() has not been implemented.'); | ||
| 77 | + } | ||
| 78 | +} |
-
Please register or login to post a comment