From 414b7dc1e447c72d371d30a79e5c2404e0f4ea20 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sat, 9 Dec 2023 08:43:17 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20Catch=20exceptions=20when=20obta?= =?UTF-8?q?in/subscribe=20to=20the=20accelerometer=20stream=20(#230)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 7 +++++++ lib/src/states/camera_picker_state.dart | 22 +++++++++++++++++----- pubspec.yaml | 4 ++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9742981..d54cce2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ that can be found in the LICENSE file. --> See the [Migration Guide](guides/migration_guide.md) for breaking changes between versions. +## 4.2.0-dev.3 + +### Improvements + +- Roll `sensors_plus`. +- Catch exceptions when obtain/subscribe to the accelerometer stream. + ## 4.2.0-dev.2 ### Breaking changes diff --git a/lib/src/states/camera_picker_state.dart b/lib/src/states/camera_picker_state.dart index 149f815..27ab990 100644 --- a/lib/src/states/camera_picker_state.dart +++ b/lib/src/states/camera_picker_state.dart @@ -216,7 +216,7 @@ class CameraPickerState extends State bool retriedAfterInvalidInitialize = false; /// Subscribe to the accelerometer. - late final StreamSubscription accelerometerSubscription; + StreamSubscription? accelerometerSubscription; /// The locked capture orientation of the current camera instance. DeviceOrientation? lockedCaptureOrientation; @@ -228,9 +228,7 @@ class CameraPickerState extends State Constants.textDelegate = widget.pickerConfig.textDelegate ?? cameraPickerTextDelegateFromLocale(widget.locale); initCameras(); - accelerometerSubscription = accelerometerEvents.listen( - handleAccelerometerEvent, - ); + initAccelerometerSubscription(); } @override @@ -249,7 +247,7 @@ class CameraPickerState extends State exposureFadeOutTimer?.cancel(); recordDetectTimer?.cancel(); recordCountdownTimer?.cancel(); - accelerometerSubscription.cancel(); + accelerometerSubscription?.cancel(); super.dispose(); } @@ -491,6 +489,20 @@ class CameraPickerState extends State }); } + /// Starts to listen on accelerometer events. + void initAccelerometerSubscription() { + try { + final stream = accelerometerEventStream(); + accelerometerSubscription = stream.listen(handleAccelerometerEvent); + } catch (e, s) { + realDebugPrint( + 'The device does not seem to support accelerometer. ' + 'The captured files orientation might be incorrect.', + ); + handleErrorWithHandler(e, s, pickerConfig.onError); + } + } + /// Lock capture orientation according to the current status of the device, /// which enables the captured file stored the correct orientation. void handleAccelerometerEvent(AccelerometerEvent event) { diff --git a/pubspec.yaml b/pubspec.yaml index 20a563b..ca06b60 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: wechat_camera_picker -version: 4.2.0-dev.2 +version: 4.2.0-dev.3 description: | A camera picker for Flutter projects based on WeChat's UI, which is also a separate runnable extension to the @@ -27,7 +27,7 @@ dependencies: path: ^1.8.0 photo_manager: ^3.0.0-dev.1 photo_manager_image_provider: ^2.0.0 - sensors_plus: ^3.1.0 + sensors_plus: ^4.0.1 video_player: ^2.7.0 dev_dependencies: