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

fix: 🐛 Fixed gyroscope initialisation issue #180

Merged
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
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
Loading