Navaron Bracke

take into account the auto mode on iOS/MacOS

... ... @@ -74,6 +74,7 @@ class MobileScannerHandler(
private var mobileScanner: MobileScanner? = null
private val torchStateCallback: TorchStateCallback = {state: Int ->
// Off = 0, On = 1
barcodeHandler.publishEvent(mapOf("name" to "torchState", "data" to state))
}
... ...
... ... @@ -138,6 +138,17 @@ class ToggleFlashlightButton extends StatelessWidget {
}
switch (state.torchState) {
case TorchState.auto:
return IconButton(
color: Colors.white,
iconSize: 32.0,
icon: const Icon(Icons.flash_auto),
onPressed: () async {
// This button only turns off the auto mode.
// Perhaps we can switch between on / off / auto?
await controller.toggleTorch();
},
);
case TorchState.off:
return IconButton(
color: Colors.white,
... ...
... ... @@ -347,7 +347,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
switch keyPath {
case "torchMode":
// off = 0; on = 1; auto = 2
// Off = 0, On = 1, Auto = 2
let state = change?[.newKey] as? Int
torchModeChangeCallback(state)
case "videoZoomFactor":
... ...
/// The state of the flashlight.
enum TorchState {
/// The flashlight turns on automatically in low light conditions.
///
/// This is currently only supported on iOS.
auto(2),
/// The flashlight is off.
off(0),
... ... @@ -7,18 +12,20 @@ enum TorchState {
on(1),
/// The flashlight is unavailable.
unavailable(2);
unavailable(-1);
const TorchState(this.rawValue);
factory TorchState.fromRawValue(int value) {
switch (value) {
case -1:
return TorchState.unavailable;
case 0:
return TorchState.off;
case 1:
return TorchState.on;
case 2:
return TorchState.unavailable;
return TorchState.auto;
default:
throw ArgumentError.value(value, 'value', 'Invalid raw value.');
}
... ...
... ... @@ -410,7 +410,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
switch keyPath {
case "torchMode":
// off = 0 on = 1 auto = 2
// Off = 0, On = 1, Auto = 2
let state = change?[.newKey] as? Int
let event: [String: Any?] = ["name": "torchState", "data": state]
sink?(event)
... ...
... ... @@ -7,7 +7,8 @@ void main() {
const values = <int, TorchState>{
0: TorchState.off,
1: TorchState.on,
2: TorchState.unavailable,
2: TorchState.auto,
-1: TorchState.unavailable,
};
for (final MapEntry<int, TorchState> entry in values.entries) {
... ... @@ -18,7 +19,7 @@ void main() {
});
test('invalid raw value throws argument error', () {
const int negative = -1;
const int negative = -2;
const int outOfRange = 3;
expect(() => TorchState.fromRawValue(negative), throwsArgumentError);
... ... @@ -27,9 +28,10 @@ void main() {
test('can be converted to raw value', () {
const values = <TorchState, int>{
TorchState.unavailable: -1,
TorchState.off: 0,
TorchState.on: 1,
TorchState.unavailable: 2,
TorchState.auto: 2,
};
for (final MapEntry<TorchState, int> entry in values.entries) {
... ...