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
…230)

(cherry picked from commit 414b7dc)
  • Loading branch information
AlexV525 committed Dec 9, 2023
1 parent 441d1c3 commit c37e276
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ that can be found in the LICENSE file. -->

# Changelog

See the [Migration Guide](guides/migration_guide.md) for the details of breaking changes between versions.
See the [Migration Guide](guides/migration_guide.md) for breaking changes between versions.

## 4.1.2

- Roll `sensors_plus`.
- Catch exceptions when obtain/subscribe to the accelerometer stream.

## 4.1.1

Expand All @@ -21,7 +26,7 @@ See the [Migration Guide](guides/migration_guide.md) for the details of breaking
### Fixes

- Handle exceptions after all flows.
- Fix various of problems with the capture button.
- Fix various problems with the capture button.

## 4.0.3

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.1.1
version: 4.1.2
description: |
A camera picker for Flutter projects based on WeChat's UI,
which is also a separate runnable extension to the
Expand All @@ -26,7 +26,7 @@ dependencies:
camera_platform_interface: ^2.1.5
path: ^1.8.0
photo_manager: ^2.7.0
sensors_plus: ^3.1.0
sensors_plus: ^4.0.1
video_player: ^2.7.0

dev_dependencies:
Expand Down

0 comments on commit c37e276

Please sign in to comment.