Navaron Bracke

take into account the auto mode on iOS/MacOS

@@ -74,6 +74,7 @@ class MobileScannerHandler( @@ -74,6 +74,7 @@ class MobileScannerHandler(
74 private var mobileScanner: MobileScanner? = null 74 private var mobileScanner: MobileScanner? = null
75 75
76 private val torchStateCallback: TorchStateCallback = {state: Int -> 76 private val torchStateCallback: TorchStateCallback = {state: Int ->
  77 + // Off = 0, On = 1
77 barcodeHandler.publishEvent(mapOf("name" to "torchState", "data" to state)) 78 barcodeHandler.publishEvent(mapOf("name" to "torchState", "data" to state))
78 } 79 }
79 80
@@ -138,6 +138,17 @@ class ToggleFlashlightButton extends StatelessWidget { @@ -138,6 +138,17 @@ class ToggleFlashlightButton extends StatelessWidget {
138 } 138 }
139 139
140 switch (state.torchState) { 140 switch (state.torchState) {
  141 + case TorchState.auto:
  142 + return IconButton(
  143 + color: Colors.white,
  144 + iconSize: 32.0,
  145 + icon: const Icon(Icons.flash_auto),
  146 + onPressed: () async {
  147 + // This button only turns off the auto mode.
  148 + // Perhaps we can switch between on / off / auto?
  149 + await controller.toggleTorch();
  150 + },
  151 + );
141 case TorchState.off: 152 case TorchState.off:
142 return IconButton( 153 return IconButton(
143 color: Colors.white, 154 color: Colors.white,
@@ -347,7 +347,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -347,7 +347,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
347 public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 347 public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
348 switch keyPath { 348 switch keyPath {
349 case "torchMode": 349 case "torchMode":
350 - // off = 0; on = 1; auto = 2 350 + // Off = 0, On = 1, Auto = 2
351 let state = change?[.newKey] as? Int 351 let state = change?[.newKey] as? Int
352 torchModeChangeCallback(state) 352 torchModeChangeCallback(state)
353 case "videoZoomFactor": 353 case "videoZoomFactor":
1 /// The state of the flashlight. 1 /// The state of the flashlight.
2 enum TorchState { 2 enum TorchState {
  3 + /// The flashlight turns on automatically in low light conditions.
  4 + ///
  5 + /// This is currently only supported on iOS.
  6 + auto(2),
  7 +
3 /// The flashlight is off. 8 /// The flashlight is off.
4 off(0), 9 off(0),
5 10
@@ -7,18 +12,20 @@ enum TorchState { @@ -7,18 +12,20 @@ enum TorchState {
7 on(1), 12 on(1),
8 13
9 /// The flashlight is unavailable. 14 /// The flashlight is unavailable.
10 - unavailable(2); 15 + unavailable(-1);
11 16
12 const TorchState(this.rawValue); 17 const TorchState(this.rawValue);
13 18
14 factory TorchState.fromRawValue(int value) { 19 factory TorchState.fromRawValue(int value) {
15 switch (value) { 20 switch (value) {
  21 + case -1:
  22 + return TorchState.unavailable;
16 case 0: 23 case 0:
17 return TorchState.off; 24 return TorchState.off;
18 case 1: 25 case 1:
19 return TorchState.on; 26 return TorchState.on;
20 case 2: 27 case 2:
21 - return TorchState.unavailable; 28 + return TorchState.auto;
22 default: 29 default:
23 throw ArgumentError.value(value, 'value', 'Invalid raw value.'); 30 throw ArgumentError.value(value, 'value', 'Invalid raw value.');
24 } 31 }
@@ -410,7 +410,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, @@ -410,7 +410,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
410 public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 410 public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
411 switch keyPath { 411 switch keyPath {
412 case "torchMode": 412 case "torchMode":
413 - // off = 0 on = 1 auto = 2 413 + // Off = 0, On = 1, Auto = 2
414 let state = change?[.newKey] as? Int 414 let state = change?[.newKey] as? Int
415 let event: [String: Any?] = ["name": "torchState", "data": state] 415 let event: [String: Any?] = ["name": "torchState", "data": state]
416 sink?(event) 416 sink?(event)
@@ -7,7 +7,8 @@ void main() { @@ -7,7 +7,8 @@ void main() {
7 const values = <int, TorchState>{ 7 const values = <int, TorchState>{
8 0: TorchState.off, 8 0: TorchState.off,
9 1: TorchState.on, 9 1: TorchState.on,
10 - 2: TorchState.unavailable, 10 + 2: TorchState.auto,
  11 + -1: TorchState.unavailable,
11 }; 12 };
12 13
13 for (final MapEntry<int, TorchState> entry in values.entries) { 14 for (final MapEntry<int, TorchState> entry in values.entries) {
@@ -18,7 +19,7 @@ void main() { @@ -18,7 +19,7 @@ void main() {
18 }); 19 });
19 20
20 test('invalid raw value throws argument error', () { 21 test('invalid raw value throws argument error', () {
21 - const int negative = -1; 22 + const int negative = -2;
22 const int outOfRange = 3; 23 const int outOfRange = 3;
23 24
24 expect(() => TorchState.fromRawValue(negative), throwsArgumentError); 25 expect(() => TorchState.fromRawValue(negative), throwsArgumentError);
@@ -27,9 +28,10 @@ void main() { @@ -27,9 +28,10 @@ void main() {
27 28
28 test('can be converted to raw value', () { 29 test('can be converted to raw value', () {
29 const values = <TorchState, int>{ 30 const values = <TorchState, int>{
  31 + TorchState.unavailable: -1,
30 TorchState.off: 0, 32 TorchState.off: 0,
31 TorchState.on: 1, 33 TorchState.on: 1,
32 - TorchState.unavailable: 2, 34 + TorchState.auto: 2,
33 }; 35 };
34 36
35 for (final MapEntry<TorchState, int> entry in values.entries) { 37 for (final MapEntry<TorchState, int> entry in values.entries) {