p-mazhnik

refactor: abstract torch support

@@ -34,9 +34,6 @@ class MobileScannerWebPlugin { @@ -34,9 +34,6 @@ class MobileScannerWebPlugin {
34 // ID of the video feed 34 // ID of the video feed
35 String viewID = 'WebScanner-${DateTime.now().millisecondsSinceEpoch}'; 35 String viewID = 'WebScanner-${DateTime.now().millisecondsSinceEpoch}';
36 36
37 - // Determine wether device has flas  
38 - bool hasFlash = false;  
39 -  
40 final html.DivElement vidDiv = html.DivElement(); 37 final html.DivElement vidDiv = html.DivElement();
41 38
42 late final WebBarcodeReaderBase _barCodeReader = 39 late final WebBarcodeReaderBase _barCodeReader =
@@ -63,15 +60,7 @@ class MobileScannerWebPlugin { @@ -63,15 +60,7 @@ class MobileScannerWebPlugin {
63 60
64 /// Can enable or disable the flash if available 61 /// Can enable or disable the flash if available
65 Future<void> _torch(arguments) async { 62 Future<void> _torch(arguments) async {
66 - // if (hasFlash) {  
67 - // final track = _localStream?.getVideoTracks();  
68 - // await track!.first.applyConstraints({  
69 - // 'advanced': {'torch': arguments == 1}  
70 - // });  
71 - // } else {  
72 - // controller.addError('Device has no flash');  
73 - // }  
74 - controller.addError('Device has no flash'); 63 + _barCodeReader.toggleTorch(enabled: arguments == 1);
75 } 64 }
76 65
77 /// Starts the video stream and the scanner 66 /// Starts the video stream and the scanner
@@ -97,7 +86,8 @@ class MobileScannerWebPlugin { @@ -97,7 +86,8 @@ class MobileScannerWebPlugin {
97 return { 86 return {
98 'ViewID': viewID, 87 'ViewID': viewID,
99 'videoWidth': _barCodeReader.videoWidth, 88 'videoWidth': _barCodeReader.videoWidth,
100 - 'videoHeight': _barCodeReader.videoHeight 89 + 'videoHeight': _barCodeReader.videoHeight,
  90 + 'torchable': _barCodeReader.hasTorch,
101 }; 91 };
102 } 92 }
103 try { 93 try {
@@ -115,7 +105,7 @@ class MobileScannerWebPlugin { @@ -115,7 +105,7 @@ class MobileScannerWebPlugin {
115 'ViewID': viewID, 105 'ViewID': viewID,
116 'videoWidth': _barCodeReader.videoWidth, 106 'videoWidth': _barCodeReader.videoWidth,
117 'videoHeight': _barCodeReader.videoHeight, 107 'videoHeight': _barCodeReader.videoHeight,
118 - 'torchable': hasFlash 108 + 'torchable': _barCodeReader.hasTorch,
119 }; 109 };
120 } catch (e) { 110 } catch (e) {
121 throw PlatformException(code: 'MobileScannerWeb', message: '$e'); 111 throw PlatformException(code: 'MobileScannerWeb', message: '$e');
@@ -29,6 +29,12 @@ abstract class WebBarcodeReaderBase { @@ -29,6 +29,12 @@ abstract class WebBarcodeReaderBase {
29 29
30 /// Stops streaming video 30 /// Stops streaming video
31 Future<void> stop(); 31 Future<void> stop();
  32 +
  33 + /// Can enable or disable the flash if available
  34 + Future<void> toggleTorch({required bool enabled});
  35 +
  36 + /// Determine whether device has flash
  37 + bool get hasTorch;
32 } 38 }
33 39
34 mixin InternalStreamCreation on WebBarcodeReaderBase { 40 mixin InternalStreamCreation on WebBarcodeReaderBase {
@@ -86,3 +92,30 @@ mixin InternalStreamCreation on WebBarcodeReaderBase { @@ -86,3 +92,30 @@ mixin InternalStreamCreation on WebBarcodeReaderBase {
86 videoContainer.children = []; 92 videoContainer.children = [];
87 } 93 }
88 } 94 }
  95 +
  96 +/// Mixin for libraries that don't have built-in torch support
  97 +mixin InternalTorchDetection on InternalStreamCreation {
  98 + @override
  99 + bool get hasTorch {
  100 + // TODO: fix flash light. See https://github.com/dart-lang/sdk/issues/48533
  101 + // final track = _localStream?.getVideoTracks();
  102 + // if (track != null) {
  103 + // final imageCapture = html.ImageCapture(track.first);
  104 + // final photoCapabilities = await imageCapture.getPhotoCapabilities();
  105 + // }
  106 + return false;
  107 + }
  108 +
  109 + @override
  110 + Future<void> toggleTorch({required bool enabled}) async {
  111 + if (hasTorch) {
  112 + final track = localMediaStream?.getVideoTracks();
  113 + await track?.first.applyConstraints({
  114 + 'advanced': [
  115 + {'torch': enabled}
  116 + ]
  117 + });
  118 + }
  119 + }
  120 +}
  121 +
@@ -20,7 +20,8 @@ class Code { @@ -20,7 +20,8 @@ class Code {
20 } 20 }
21 21
22 22
23 -class JsQrCodeReader extends WebBarcodeReaderBase with InternalStreamCreation { 23 +class JsQrCodeReader extends WebBarcodeReaderBase
  24 + with InternalStreamCreation, InternalTorchDetection {
24 JsQrCodeReader({required super.videoContainer}); 25 JsQrCodeReader({required super.videoContainer});
25 26
26 @override 27 @override
@@ -34,13 +35,6 @@ class JsQrCodeReader extends WebBarcodeReaderBase with InternalStreamCreation { @@ -34,13 +35,6 @@ class JsQrCodeReader extends WebBarcodeReaderBase with InternalStreamCreation {
34 35
35 final stream = await initMediaStream(cameraFacing); 36 final stream = await initMediaStream(cameraFacing);
36 37
37 - // TODO: fix flash light. See https://github.com/dart-lang/sdk/issues/48533  
38 - // final track = _localStream?.getVideoTracks();  
39 - // if (track != null) {  
40 - // final imageCapture = html.ImageCapture(track.first);  
41 - // final photoCapabilities = await imageCapture.getPhotoCapabilities();  
42 - // }  
43 -  
44 prepareVideoElement(video); 38 prepareVideoElement(video);
45 if (stream != null) { 39 if (stream != null) {
46 await attachStreamToVideo(stream, video); 40 await attachStreamToVideo(stream, video);