Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🥅 Catch exceptions when obtain/subscribe to the accelerometer stream #230

Merged
merged 3 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions lib/src/states/camera_picker_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class CameraPickerState extends State<CameraPicker>
bool retriedAfterInvalidInitialize = false;

/// Subscribe to the accelerometer.
late final StreamSubscription<AccelerometerEvent> accelerometerSubscription;
StreamSubscription<AccelerometerEvent>? accelerometerSubscription;

/// The locked capture orientation of the current camera instance.
DeviceOrientation? lockedCaptureOrientation;
Expand All @@ -228,9 +228,7 @@ class CameraPickerState extends State<CameraPicker>
Constants.textDelegate = widget.pickerConfig.textDelegate ??
cameraPickerTextDelegateFromLocale(widget.locale);
initCameras();
accelerometerSubscription = accelerometerEvents.listen(
handleAccelerometerEvent,
);
initAccelerometerSubscription();
}

@override
Expand All @@ -249,7 +247,7 @@ class CameraPickerState extends State<CameraPicker>
exposureFadeOutTimer?.cancel();
recordDetectTimer?.cancel();
recordCountdownTimer?.cancel();
accelerometerSubscription.cancel();
accelerometerSubscription?.cancel();
super.dispose();
}

Expand Down Expand Up @@ -491,6 +489,20 @@ class CameraPickerState extends State<CameraPicker>
});
}

/// 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) {
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
Loading