Skip to content

Commit 5cf3f8a

Browse files
fix: 7680 - removed dependency on torch_light (openfoodfacts#7686)
* fix: 7680 - removed dependency on torch_light Impacted files: * `attribute_extensions.dart`: minor refactoring * `camera_view.dart`: removed duplicated code * `edit_product_image_viewer.dart`: minor refactoring * `expandable_camera.dart`: minor refactoring * `homepage.dart`: minor refactoring * `main.dart`: minor refactoring * `mobile_scanner_controller.dart`: refactored around torch light * `preferences_page.dart`: minor refactoring * `pubspec.lock`: wtf * `apple_app_store/pubspec.yaml`: explicit upgrade to dart 3.12.2 * `google_play/pubspec.yaml`: explicit upgrade to dart 3.12.2 * `shared/pubspec.yaml`: explicit upgrade to dart 3.12.2 * `uri_store/pubspec.yaml`: explicit upgrade to dart 3.12.2 * `ml_kit/pubspec.yaml`: removed dependency on torch_light; explicit upgrade to dart 3.12.2 * `shared/pubspec.yaml`: ugly dependency on mobile_scanner; explicit upgrade to dart 3.12.2 * `zxing/pubspec.yaml`: explicit upgrade to dart 3.12.2 * `smooth_app/pubspec.yaml`: removed dependency on torch_light * `scan_carousel.dart`: minor refactoring * `scanner_buttons_bar.dart`: refactoring around torch light * `scanner_ml_kit.dart`: refactoring around torch light * `scanner_shared.dart`: added mobile_scanner_controller.dart * `smooth_expandable_floating_action_button.dart`: minor refactoring * `utils.dart`: minor refactoring * pubspec.lock without torch_light * MinimumOSVersion: 15.0 * minor pubspec upgrades
1 parent 17dd1ff commit 5cf3f8a

25 files changed

Lines changed: 73 additions & 189 deletions

packages/app_store/apple_app_store/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version: 0.0.1
44
publish_to: "none"
55

66
environment:
7-
sdk: ^3.10.4
7+
sdk: ^3.12.2
88

99
dependencies:
1010
flutter:

packages/app_store/google_play/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version: 0.0.1
44
publish_to: "none"
55

66
environment:
7-
sdk: ^3.10.4
7+
sdk: ^3.12.2
88

99
dependencies:
1010
flutter:

packages/app_store/shared/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version: 1.0.0
44
publish_to: "none"
55

66
environment:
7-
sdk: ^3.10.4
7+
sdk: ^3.12.2
88

99
dependencies:
1010
flutter:

packages/app_store/uri_store/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version: 0.0.1
44
publish_to: "none"
55

66
environment:
7-
sdk: ^3.10.4
7+
sdk: ^3.12.2
88

99
dependencies:
1010
flutter:

packages/scanner/ml_kit/lib/src/scanner_ml_kit.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'dart:async';
33
import 'package:flutter/material.dart';
44
import 'package:mobile_scanner/mobile_scanner.dart';
55
import 'package:provider/provider.dart';
6-
import 'package:scanner_ml_kit/src/mobile_scanner_controller.dart';
76
import 'package:scanner_shared/scanner_shared.dart';
87
import 'package:visibility_detector/visibility_detector.dart';
98

@@ -120,7 +119,7 @@ class _SmoothBarcodeScannerMLKitState extends State<_SmoothBarcodeScannerMLKit>
120119
super.initState();
121120

122121
_cameraController = CustomScannerController(
123-
controller: MobileScannerController(
122+
MobileScannerController(
124123
autoStart: false,
125124
torchEnabled: false,
126125
formats: _barcodeFormats,
@@ -160,7 +159,7 @@ class _SmoothBarcodeScannerMLKitState extends State<_SmoothBarcodeScannerMLKit>
160159
MobileScanner(
161160
controller: _cameraController.controller,
162161
fit: BoxFit.cover,
163-
errorBuilder: (_, _) => EMPTY_WIDGET,
162+
errorBuilder: (_, _) => SCANNER_EMPTY_WIDGET,
164163
onDetect: (final BarcodeCapture capture) async {
165164
for (final Barcode barcode in capture.barcodes) {
166165
final String? string = barcode.displayValue;
@@ -246,11 +245,11 @@ class _TorchIcon extends StatefulWidget {
246245
class _TorchIconState extends State<_TorchIcon> {
247246
@override
248247
Widget build(BuildContext context) {
249-
return ValueListenableBuilder<bool?>(
248+
return ValueListenableBuilder<TorchState?>(
250249
valueListenable: context.watch<CustomScannerController>().hasTorchState,
251-
builder: (BuildContext context, bool? hasTorch, _) {
250+
builder: (BuildContext context, TorchState? hasTorch, _) {
252251
if (hasTorch == null) {
253-
return EMPTY_WIDGET;
252+
return SCANNER_EMPTY_WIDGET;
254253
}
255254

256255
final CustomScannerController controller = context
@@ -300,7 +299,7 @@ class _ToggleCameraIcon extends StatelessWidget {
300299
valueListenable: controller.availableCameras,
301300
builder: (BuildContext context, int cameras, _) {
302301
if (cameras <= 1) {
303-
return EMPTY_WIDGET;
302+
return SCANNER_EMPTY_WIDGET;
304303
}
305304

306305
return VisorButton(

packages/scanner/ml_kit/pubspec.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version: 1.0.0
44
publish_to: "none"
55

66
environment:
7-
sdk: ^3.10.4
7+
sdk: ^3.12.2
88

99
dependencies:
1010
flutter:
@@ -15,7 +15,6 @@ dependencies:
1515
provider: ^6.1.5+1
1616

1717
mobile_scanner: 7.4.0
18-
torch_light: ^1.1.0
1918

2019
scanner_shared:
2120
path: ../shared

packages/scanner/shared/lib/scanner_shared.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
library;
22

3+
export 'src/mobile_scanner_controller.dart';
34
export 'src/scanner.dart';
45
export 'src/scanner_mocked.dart';
56
export 'src/scanner_visor.dart';

packages/scanner/ml_kit/lib/src/mobile_scanner_controller.dart renamed to packages/scanner/shared/lib/src/mobile_scanner_controller.dart

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
24
import 'package:mobile_scanner/mobile_scanner.dart';
3-
import 'package:torch_light/torch_light.dart';
45

56
class CustomScannerController {
6-
CustomScannerController({required MobileScannerController controller})
7-
: _controller = controller,
8-
_torchState = TorchState(),
7+
CustomScannerController(this._controller)
8+
: _torchState = ScannerTorchState(),
99
_availableCamerasState = AvailableCamerasState(),
10-
_cameraFacingState = CameraFacingState() {
11-
_detectTorch();
12-
}
10+
_cameraFacingState = CameraFacingState();
1311

1412
final MobileScannerController _controller;
15-
final TorchState _torchState;
13+
final ScannerTorchState _torchState;
1614
final AvailableCamerasState _availableCamerasState;
1715
final CameraFacingState _cameraFacingState;
1816

@@ -43,9 +41,17 @@ class CustomScannerController {
4341
} catch (_) {}
4442
}
4543

44+
void onPause() {
45+
_isStarted = false;
46+
_isStarting = false;
47+
_isClosing = false;
48+
_isClosed = false;
49+
}
50+
4651
void _onControllerChanged() {
4752
_availableCamerasState.value = _controller.value.availableCameras ?? 0;
4853
_cameraFacingState.value = _controller.facing;
54+
_torchState.value = _controller.value.torchState;
4955
}
5056

5157
MobileScannerController get controller => _controller;
@@ -74,21 +80,20 @@ class CustomScannerController {
7480

7581
bool get hasTorch => _torchState.value != null;
7682

77-
ValueNotifier<bool?> get hasTorchState => _torchState;
83+
ValueNotifier<TorchState?> get hasTorchState => _torchState;
7884

79-
bool get isTorchOn => _torchState.value == true;
85+
// ugly
86+
bool get isTorchOn => _torchState.value == TorchState.on;
8087

8188
void turnTorchOff() {
8289
if (isTorchOn) {
83-
_controller.toggleTorch();
84-
_torchState.value = false;
90+
unawaited(_controller.toggleTorch());
8591
}
8692
}
8793

8894
void turnTorchOn() {
8995
if (!isTorchOn) {
90-
_controller.toggleTorch();
91-
_torchState.value = true;
96+
unawaited(_controller.toggleTorch());
9297
}
9398
}
9499

@@ -101,6 +106,7 @@ class CustomScannerController {
101106
}
102107

103108
ValueNotifier<int> get availableCameras => _availableCamerasState;
109+
104110
ValueNotifier<CameraFacing> get cameraFacing => _cameraFacingState;
105111

106112
void toggleCamera() {
@@ -109,27 +115,16 @@ class CustomScannerController {
109115
_torchState.value = null;
110116
_cameraFacingState.value = CameraFacing.front;
111117
} else if (_controller.facing == CameraFacing.front) {
112-
_torchState.value = false;
118+
_torchState.value = TorchState.off;
113119
_cameraFacingState.value = CameraFacing.back;
114120
}
115121
}
116122

117-
Future<void> _detectTorch() async {
118-
try {
119-
final bool isTorchAvailable = await TorchLight.isTorchAvailable();
120-
if (isTorchAvailable) {
121-
_torchState.value = false;
122-
} else {
123-
_torchState.value = null;
124-
}
125-
} on Exception catch (_) {}
126-
}
127-
128123
Future<void> dispose() => _controller.dispose();
129124
}
130125

131-
class TorchState extends ValueNotifier<bool?> {
132-
TorchState({bool? value}) : super(value);
126+
class ScannerTorchState extends ValueNotifier<TorchState?> {
127+
ScannerTorchState({TorchState? value}) : super(value);
133128
}
134129

135130
class AvailableCamerasState extends ValueNotifier<int> {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import 'package:flutter/material.dart';
22

3-
const Widget EMPTY_WIDGET = SizedBox.shrink();
3+
const Widget SCANNER_EMPTY_WIDGET = SizedBox.shrink();

packages/scanner/shared/pubspec.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ version: 1.0.0
44
publish_to: "none"
55

66
environment:
7-
sdk: ^3.10.4
7+
sdk: ^3.12.2
88

99
dependencies:
1010
flutter:
1111
sdk: flutter
1212
flutter_svg: ^2.2.3
1313
visibility_detector: ^0.4.0+2
1414
provider: ^6.1.5+1
15+
# waiting for a solution with zxing
16+
mobile_scanner: 7.4.0
1517

1618
dev_dependencies:
1719
flutter_test:

0 commit comments

Comments
 (0)