Skip to content

Commit

Permalink
fix: 🐛Fixed gyroscope initialization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Sahil Totala authored and vatsaltanna-simformsolutions committed Jan 9, 2025
1 parent 4929441 commit 1c62271
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# [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).
- Fixed Namespace Not Found issue [#176](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/issues/176).

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
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
Expand Up @@ -51,7 +51,6 @@ class MethodChannelFlutterCreditCard extends FlutterCreditCardPlatform {
);

if (Platform.isIOS || Platform.isAndroid) {
await initiateEvents();
_isGyroscopeAvailable = await _methodChannel!.invokeMethod<dynamic>(
AppConstants.isGyroAvailableMethod,
) ??
Expand All @@ -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
Expand Down

0 comments on commit 1c62271

Please sign in to comment.