Skip to content

Commit

Permalink
test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
g123k committed Jul 2, 2022
1 parent e421bcc commit b32e385
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 28 deletions.
19 changes: 19 additions & 0 deletions packages/smooth_app/lib/helpers/camera_helper.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'package:camera/camera.dart';
import 'package:flutter/foundation.dart';
import 'package:smooth_app/pages/scan/camera_controller.dart';

class CameraHelper {
const CameraHelper._();

static final CameraControllerNotifier _cameraControllerWrapper =
CameraControllerNotifier();

static List<CameraDescription>? _cameras;

/// Ensure we have a single instance of this controller
Expand Down Expand Up @@ -67,11 +71,26 @@ class CameraHelper {
/// And prevents the redefinition of it
static void initController(SmoothCameraController controller) {
_controller ??= controller;
_cameraControllerWrapper.updateValue();
}

static void destroyControllerInstance() {
_controller = null;
_cameraControllerWrapper.updateValue();
}

static SmoothCameraController? get controller => _controller;

static CameraControllerNotifier get cameraControllerNotifier =>
_cameraControllerWrapper;
}

class CameraControllerNotifier extends ChangeNotifier {
void updateValue() {
try {
notifyListeners();
} catch (err) {
// If no Widget listen to this, it will throw an error
}
}
}
4 changes: 3 additions & 1 deletion packages/smooth_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ class _SmoothAppState extends State<SmoothApp> {
provide<ContinuousScanModel>(_continuousScanModel),
provide<SmoothAppDataImporter>(_appDataImporter),
provide<UpToDateProductProvider>(_upToDateProductProvider),
provide<PermissionListener>(_permissionListener)
provide<PermissionListener>(_permissionListener),
provide<CameraControllerNotifier>(
CameraHelper.cameraControllerNotifier),
],
builder: _buildApp,
);
Expand Down
6 changes: 6 additions & 0 deletions packages/smooth_app/lib/pages/scan/camera_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ class SmoothCameraController extends CameraController {
bool get isBeingInitialized => _state == _CameraState.beingInitialized;

bool get isPauseResumePreviewSupported => !Platform.isIOS;

bool compare(Object? other) =>
identical(this, other) ||
other is SmoothCameraController &&
runtimeType == other.runtimeType &&
value == other.value;
}

extension CameraValueExtension on CameraValue {
Expand Down
21 changes: 13 additions & 8 deletions packages/smooth_app/lib/pages/scan/camera_image_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@ import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/helpers/camera_helper.dart';
import 'package:smooth_app/pages/scan/camera_controller.dart';

/// Forked Widget from the [camera] library, but with a simpler Widget
class CameraStreamPreview extends StatelessWidget {
const CameraStreamPreview({
/// Forked Widget from the [camera] library, but with a simpler content
class SmoothCameraStreamPreview extends StatelessWidget {
const SmoothCameraStreamPreview({
super.key,
});

@override
Widget build(BuildContext context) {
final SmoothCameraController? controller = CameraHelper.controller;
// Ensure this Widget is rebuild when necessary
Provider.of<CameraControllerNotifier>(
context,
listen: true,
);

if (controller == null || !controller.isInitialized) {
if (controller == null || controller?.isInitialized != true) {
return const SizedBox.shrink();
}

return AspectRatio(
aspectRatio: _isLandscape()
? controller.value.aspectRatio
: (1.0 / controller.value.aspectRatio),
? controller!.value.aspectRatio
: (1.0 / controller!.value.aspectRatio),
child: _wrapInRotatedBox(
child: controller.buildPreview(),
child: controller!.buildPreview(),
),
);
}
Expand Down
46 changes: 31 additions & 15 deletions packages/smooth_app/lib/pages/scan/ml_kit_scan_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,14 @@ class MLKitScannerPageState extends LifecycleAwareState<MLKitScannerPage>
}

// Relaunch the feed after a hot reload
if (_controller == null) {
_startLiveFeed();
} else {
_controller!.updateFocusPointAlgorithm(
_userPreferences.cameraFocusPointAlgorithm,
);
if (_isScreenVisible()) {
if (_controller == null) {
_startLiveFeed();
} else {
_controller!.updateFocusPointAlgorithm(
_userPreferences.cameraFocusPointAlgorithm,
);
}
}
}

Expand All @@ -128,9 +130,7 @@ class MLKitScannerPageState extends LifecycleAwareState<MLKitScannerPage>
Widget build(BuildContext context) {
return Consumer<BottomNavigationTab>(
builder: (BuildContext context, BottomNavigationTab tab, Widget? child) {
if (pendingResume &&
tab == BottomNavigationTab.Scan &&
Navigator.of(context).canPop()) {
if (pendingResume && _isScreenVisible(tab: tab)) {
pendingResume = false;
_onResumeImageStream();
}
Expand All @@ -150,6 +150,12 @@ class MLKitScannerPageState extends LifecycleAwareState<MLKitScannerPage>
);
}

bool _isScreenVisible({BottomNavigationTab? tab}) {
return (tab ?? Provider.of<BottomNavigationTab>(context, listen: false)) ==
BottomNavigationTab.Scan &&
!Navigator.of(context).canPop();
}

Widget _buildScannerWidget() {
// Showing a black scanner background when the camera is not initialized
if (!isCameraReady) {
Expand All @@ -168,7 +174,7 @@ class MLKitScannerPageState extends LifecycleAwareState<MLKitScannerPage>
scale: _previewScale,
child: Center(
key: ValueKey<bool>(stoppingCamera),
child: const CameraStreamPreview(),
child: const SmoothCameraStreamPreview(),
),
);
},
Expand Down Expand Up @@ -297,8 +303,10 @@ class MLKitScannerPageState extends LifecycleAwareState<MLKitScannerPage>
});
}
} on CameraException catch (e) {
// TODO(M123): Show error message
Logs.d('On camera error', ex: e);

// The camera may not be ready. Wait a short time and try again.
return _stopImageStream();
} on FlutterError catch (e) {
Logs.d('On camera (Flutter part) error', ex: e);
}
Expand Down Expand Up @@ -388,7 +396,12 @@ class MLKitScannerPageState extends LifecycleAwareState<MLKitScannerPage>
}

if (_controller?.isPauseResumePreviewSupported == true) {
await _controller?.resumePreviewIfNecessary();
try {
await _controller?.resumePreviewIfNecessary();
} on CameraException catch (_) {
// Dart Controller is OK, but native part is KO
return _stopImageStream();
}
}
stoppingCamera = false;
}
Expand All @@ -407,11 +420,14 @@ class MLKitScannerPageState extends LifecycleAwareState<MLKitScannerPage>
_redrawScreen();

_controller?.removeListener(_cameraListener);
await _streamSubscription?.cancel();

await _controller?.dispose();
// Don't wait for the controller to be disposed,
// a new one will be created in parallel
_controller?.dispose();
CameraHelper.destroyControllerInstance();

await _streamSubscription?.cancel();

await _barcodeDecoder?.dispose();
_barcodeDecoder = null;

Expand Down Expand Up @@ -445,7 +461,7 @@ class MLKitScannerPageState extends LifecycleAwareState<MLKitScannerPage>
DateTime.now().difference(referentialTime).inMilliseconds;

// The screen is still visible, we should restart the camera
if (diff < _minPostFrameCallbackDelay) {
if (diff < _minPostFrameCallbackDelay && _isScreenVisible()) {
_startLiveFeed();
}
});
Expand Down
6 changes: 5 additions & 1 deletion packages/smooth_app/lib/widgets/lifecycle_aware_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ abstract class LifecycleAwareState<T extends StatefulWidget> extends State<T> {
/// Will call [setState] only if the current lifecycle state allows it
void setStateSafe(VoidCallback fn) {
if (_debugLifecycleState != StateLifecycle.defunct) {
setState(fn);
try {
setState(fn);
} catch (ignored) {
// ignore
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/smooth_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ packages:
description:
path: "packages/camera/camera"
ref: smooth_camera
resolved-ref: e9707613cd69cc8fd38416d90bc1db8e3d05d9e8
resolved-ref: d85e1a0df41547cb48c912afc48915dc81a6370a
url: "https://github.com/g123k/plugins.git"
source: git
version: "0.9.6"
Expand All @@ -134,7 +134,7 @@ packages:
description:
path: "packages/camera/camera_platform_interface"
ref: smooth_camera
resolved-ref: e9707613cd69cc8fd38416d90bc1db8e3d05d9e8
resolved-ref: d85e1a0df41547cb48c912afc48915dc81a6370a
url: "https://github.com/g123k/plugins.git"
source: git
version: "2.1.6"
Expand All @@ -143,7 +143,7 @@ packages:
description:
path: "packages/camera/camera_web"
ref: smooth_camera
resolved-ref: e9707613cd69cc8fd38416d90bc1db8e3d05d9e8
resolved-ref: d85e1a0df41547cb48c912afc48915dc81a6370a
url: "https://github.com/g123k/plugins.git"
source: git
version: "0.2.1+6"
Expand Down

0 comments on commit b32e385

Please sign in to comment.