Skip to content

Commit

Permalink
🥅 Catch exceptions when obtain/subscribe to the accelerometer stream (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored Dec 9, 2023
1 parent 012c156 commit 414b7dc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
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

0 comments on commit 414b7dc

Please sign in to comment.