Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 🐛Fixed gyroscope initialization issue
Browse files Browse the repository at this point in the history
Sahil Totala committed Jan 9, 2025

Verified

This commit was signed with the committer’s verified signature.
txgruppi Tarcisio Gruppi
1 parent 2aa4435 commit 9e4a31f
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# [4.0.2](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/tree/4.0.2) [UNRELEASED]

- Fixed floating event stream bad state exception [#157](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/issues/157).
- Fixed Gyroscope initialization issue [#173](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/issues/173)

# [4.0.1](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/tree/4.0.1)

Original file line number Diff line number Diff line change
@@ -15,17 +15,26 @@ internal class GyroscopeStreamHandler(
) : EventChannel.StreamHandler {
private var sensorEventListener: SensorEventListener? = null

private val sensor: Sensor by lazy {
private val sensor: Sensor? by lazy {
sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)
}

override fun onListen(arguments: Any?, events: EventSink) {
if (sensor == null) {
events.error("SENSOR_UNAVAILABLE", "Gyroscope sensor is not available on this device.", null)
return
}
sensorEventListener = createSensorEventListener(events)
// Gyroscope Event sample period set at 60 fps, specified in microseconds.
sensorManager.registerListener(sensorEventListener, sensor, 16666)
}

override fun onCancel(arguments: Any?) = sensorManager.unregisterListener(sensorEventListener)
override fun onCancel(arguments: Any?) {
if (sensorEventListener != null) {
sensorManager.unregisterListener(sensorEventListener)
sensorEventListener = null
}
}

private fun createSensorEventListener(events: EventSink): SensorEventListener {
return object : SensorEventListener {
5 changes: 4 additions & 1 deletion lib/src/plugin/flutter_credit_card_method_channel.dart
Original file line number Diff line number Diff line change
@@ -51,7 +51,6 @@ class MethodChannelFlutterCreditCard extends FlutterCreditCardPlatform {
);

if (Platform.isIOS || Platform.isAndroid) {
await initiateEvents();
_isGyroscopeAvailable = await _methodChannel!.invokeMethod<dynamic>(
AppConstants.isGyroAvailableMethod,
) ??
@@ -60,6 +59,10 @@ class MethodChannelFlutterCreditCard extends FlutterCreditCardPlatform {
// Other platforms should not use the gyroscope events.
_isGyroscopeAvailable = false;
}
// We will only initialize event if gyroScope is available.
if (_isGyroscopeAvailable) {
await initiateEvents();
}
}

@override

0 comments on commit 9e4a31f

Please sign in to comment.