Navaron Bracke

remove the requirement on having the library present beforehand

@@ -22,6 +22,7 @@ BREAKING CHANGES: @@ -22,6 +22,7 @@ BREAKING CHANGES:
22 22
23 Improvements: 23 Improvements:
24 * The `MobileScannerController` is now a ChangeNotifier, with `MobileScannerState` as its model. 24 * The `MobileScannerController` is now a ChangeNotifier, with `MobileScannerState` as its model.
  25 +* The web implementation now supports alternate URLs for loading the barcode library.
25 26
26 ## 4.0.1 27 ## 4.0.1
27 Bugs fixed: 28 Bugs fixed:
@@ -61,14 +61,22 @@ Ensure that you granted camera permission in XCode -> Signing & Capabilities: @@ -61,14 +61,22 @@ Ensure that you granted camera permission in XCode -> Signing & Capabilities:
61 61
62 ## Web 62 ## Web
63 63
64 -Include the `ZXing` library in the `<head>` of your `index.html` as a script. 64 +As of version 4.0.0 adding the library to the `index.html` is no longer required,
  65 +as the library is automatically loaded on first use.
65 66
66 -```html  
67 -<head>  
68 - <!-- other things in the tag --> 67 +### Providing a mirror for the barcode scanning library
69 68
70 - <script src="https://unpkg.com/@zxing/library@0.19.1" type="application/javascript"></script>  
71 -</head> 69 +If a different mirror is needed to load the barcode scanning library,
  70 +the source URL can be set beforehand.
  71 +
  72 +```dart
  73 +import 'package:flutter/foundation.dart';
  74 +
  75 +final String scriptUrl = // ...
  76 +
  77 +if (kIsWeb) {
  78 + MobileScannerPlatform.instance.setBarcodeLibraryScriptUrl(scriptUrl);
  79 +}
72 ``` 80 ```
73 81
74 ## Usage 82 ## Usage
@@ -38,9 +38,6 @@ @@ -38,9 +38,6 @@
38 </script> 38 </script>
39 <!-- This script adds the flutter initialization JS code --> 39 <!-- This script adds the flutter initialization JS code -->
40 <script src="flutter.js" defer></script> 40 <script src="flutter.js" defer></script>
41 -  
42 - <!-- Add the ZXing library to allow `mobile_scanner` to work on the web. -->  
43 - <script src="https://unpkg.com/@zxing/library@0.19.1" type="application/javascript"></script>  
44 </head> 41 </head>
45 <body> 42 <body>
46 <script> 43 <script>
@@ -62,6 +62,11 @@ abstract class MobileScannerPlatform extends PlatformInterface { @@ -62,6 +62,11 @@ abstract class MobileScannerPlatform extends PlatformInterface {
62 throw UnimplementedError('resetZoomScale() has not been implemented.'); 62 throw UnimplementedError('resetZoomScale() has not been implemented.');
63 } 63 }
64 64
  65 + /// Set the source url for the barcode library.
  66 + ///
  67 + /// This is only supported on the web.
  68 + void setBarcodeLibraryScriptUrl(String scriptUrl) {}
  69 +
65 /// Set the torch state of the active camera. 70 /// Set the torch state of the active camera.
66 Future<void> setTorchState(TorchState torchState) { 71 Future<void> setTorchState(TorchState torchState) {
67 throw UnimplementedError('setTorchState() has not been implemented.'); 72 throw UnimplementedError('setTorchState() has not been implemented.');
@@ -49,8 +49,11 @@ abstract class BarcodeReader { @@ -49,8 +49,11 @@ abstract class BarcodeReader {
49 49
50 /// Load the barcode reader library. 50 /// Load the barcode reader library.
51 /// 51 ///
  52 + /// If [alternateScriptUrl] is provided,
  53 + /// the script is loaded from that url instead.
  54 + ///
52 /// Does nothing if the library is already loaded. 55 /// Does nothing if the library is already loaded.
53 - Future<void> maybeLoadLibrary() async { 56 + Future<void> maybeLoadLibrary({String? alternateScriptUrl}) async {
54 // Script already exists. 57 // Script already exists.
55 if (document.querySelector('script#$scriptId') != null) { 58 if (document.querySelector('script#$scriptId') != null) {
56 return; 59 return;
@@ -66,7 +69,7 @@ abstract class BarcodeReader { @@ -66,7 +69,7 @@ abstract class BarcodeReader {
66 ..type = 'application/javascript' 69 ..type = 'application/javascript'
67 ..lang = 'javascript' 70 ..lang = 'javascript'
68 ..crossOrigin = 'anonymous' 71 ..crossOrigin = 'anonymous'
69 - ..src = scriptUrl 72 + ..src = alternateScriptUrl ?? scriptUrl
70 ..onload = allowInterop((JSAny _) { 73 ..onload = allowInterop((JSAny _) {
71 if (!completer.isCompleted) { 74 if (!completer.isCompleted) {
72 completer.complete(); 75 completer.complete();
@@ -19,6 +19,9 @@ class MobileScannerWeb extends MobileScannerPlatform { @@ -19,6 +19,9 @@ class MobileScannerWeb extends MobileScannerPlatform {
19 /// Constructs a [MobileScannerWeb] instance. 19 /// Constructs a [MobileScannerWeb] instance.
20 MobileScannerWeb(); 20 MobileScannerWeb();
21 21
  22 + /// The alternate script url for the barcode library.
  23 + String? _alternateScriptUrl;
  24 +
22 /// The internal barcode reader. 25 /// The internal barcode reader.
23 final BarcodeReader _barcodeReader = ZXingBarcodeReader(); 26 final BarcodeReader _barcodeReader = ZXingBarcodeReader();
24 27
@@ -76,8 +79,15 @@ class MobileScannerWeb extends MobileScannerPlatform { @@ -76,8 +79,15 @@ class MobileScannerWeb extends MobileScannerPlatform {
76 } 79 }
77 80
78 @override 81 @override
  82 + void setBarcodeLibraryScriptUrl(String scriptUrl) {
  83 + _alternateScriptUrl ??= scriptUrl;
  84 + }
  85 +
  86 + @override
79 Future<MobileScannerViewAttributes> start(StartOptions startOptions) async { 87 Future<MobileScannerViewAttributes> start(StartOptions startOptions) async {
80 - await _barcodeReader.maybeLoadLibrary(); 88 + await _barcodeReader.maybeLoadLibrary(
  89 + alternateScriptUrl: _alternateScriptUrl,
  90 + );
81 91
82 // Setup the view factory & container element. 92 // Setup the view factory & container element.
83 if (_divElement == null) { 93 if (_divElement == null) {