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

feat(sensors_plus)!: add barometer support for all platforms #3079

Merged
merged 18 commits into from
Jul 19, 2024

Conversation

aanas-sayed
Copy link
Contributor

@aanas-sayed aanas-sayed commented Jul 8, 2024

Description

This PR implements barometer support for all platforms (Android, iOS, and web) within the sensors_plus plugin. The barometer sensor measures atmospheric pressure and can be used for various applications such as altimeters.

Note: There are limitations for web platform, as according to MZN docs, there is no such support for barometer support. This has been implemented similarly to the magnetometer which is also not implemented on any modern browsers, so it handles and throws the right errors if used.

Changes Made:

  • Android Implementation (Tested): Updated Android specific methods and handlers for barometer sensor in:

    • ...sensors_plus\android\src\main\kotlin\dev\fluttercommunity\plus\sensors\SensorsPlugin.kt
    • ...sensors_plus\android\src\main\kotlin\dev\fluttercommunity\plus\sensors\StreamHandlerImpl.kt
  • iOS Implementation (Not Tested): Updated packages\sensors_plus\sensors_plus\ios\sensors_plus.podspec to include barometer reference.
    Added Android specific methods and handlers for barometer sensor in:

    • ...sensors_plus\ios\Classes\FPPSensorsPlusPlugin.swift
    • ...sensors_plus\ios\Classes\FPPStreamHandlerPlus.swift
  • Web Implementation (Tested): Added placeholder implementation and error handling for web platform. There is no plan for this to be implemented on the web from my understanding, but the implementation here will allow appropriate handling to be implemented by users.

  • Plugin Implementation (Tested): Added required implementations following the same pattern as used for other sensors for the barometer:

    • BarometerEvent class in ...sensors_plus_platform_interface\lib\src\barometer_event.dart
    • barometerEventStream methods in ...sensors_plus\lib\src\sensors_plus_web.dart, ...sensors_plus_platform_interface\lib\src\method_channel_sensors.dart, ...sensors_plus\lib\src\sensors.dart, ...sensors_plus\lib\src\web_sensors.dart, ...sensors_plus_platform_interface\lib\sensors_plus_platform_interface.dart and ...sensors_plus\lib\sensors_plus.dart
    • Deprecated barometerEvents (not sure if this is needed, but there for consistency) in ...sensors_plus_platform_interface\lib\sensors_plus_platform_interface.dart and ...sensors_plus\lib\sensors_plus.dart
    • JS Interoperability for non-existing Barometer API in ...sensors_plus\lib\src\web_sensors_interop.dart
  • Tests (Tested): Unit tests have been written for the barometer in:

    • ...sensors_plus\test\sensors_test.dart
    • ...sensors_plus_platform_interface\test\sensors_plus_platform_interface_test.dart
  • Example App (Tested): Updated to include barometer readings in table in ...sensors_plus\example\lib\main.dart.

  • README.md: Updated to show barometer in example and reference it.

Related Issues

Implements required functionality for barometer (#955)

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I titled the PR using Conventional Commits.
  • I did not modify the CHANGELOG.md nor the plugin version in pubspec.yaml files.
  • All existing and new tests are passing.
  • The analyzer (flutter analyze) does not report any problems on my PR.

Note: flutter analyze reports errors for deprecated constants for network_info_plus. Did not touch or modify these as it is not related to sensor_plus.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate that with a ! in the title as explained in Conventional Commits).
  • No, this is not a breaking change.

@aanas-sayed
Copy link
Contributor Author

aanas-sayed commented Jul 8, 2024

For quick reference, this is what the example app looks like now:
Screenshot (8 Jul 2024 21_14_15)

And for web platform you get similar messages as that for magnetometer which is also not implemented on modern browsers:
sensors_plus web devtools

@miquelbeltran
Copy link
Member

Oh wow, that's a lot! It will probably take a bit to review everything. But I think there should be no issues to incorporate these.

I did a quick read, and I'd say we shouldn't include the deprecated methods, even if the other sensors have them. Removing them will also reduce the size of the PR.

@aanas-sayed
Copy link
Contributor Author

Started with thinking of just adding the android implementation and went down the rabbit hole haha after reading the contribution guide to try to get all 3.

Thanks for the quick response! Great if it can be added 😄 but of course not a rush for me.

Regarding the deprecated methods, they've been removed now.

@aanas-sayed
Copy link
Contributor Author

Should I also update the root REAMDE.md?

From :

Flutter plugin for accessing accelerometer, gyroscope, and magnetometer sensors.

To :

Flutter plugin for accessing accelerometer, gyroscope, magnetometer and barometer sensors.

@miquelbeltran
Copy link
Member

Should I also update the root REAMDE.md?

From :

Flutter plugin for accessing accelerometer, gyroscope, and magnetometer sensors.

To :

Flutter plugin for accessing accelerometer, gyroscope, magnetometer and barometer sensors.

Yep!

@miquelbeltran miquelbeltran self-assigned this Jul 12, 2024
Copy link
Member

@miquelbeltran miquelbeltran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to test the iOS implementation on my test iPhone eventually.

I left a comment on the web implementation, which I think should be removed since it does nothing.

I have also pushed a small README change.

Comment on lines +107 to +114

/// Returns a broadcast stream of events from the device barometer at the
/// given sampling frequency.
Stream<BarometerEvent> barometerEventStream({
Duration samplingPeriod = SensorInterval.normalInterval,
}) {
throw UnimplementedError('barometerEvents has not been implemented.');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we modify the platform interface, this change has to be marked as a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding was this will not affecting current users' implementations using the library and so not a breaking change. I'm happy to mark it as such if this is how its maintained, but out of curiosity, could you please elaborate why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sensor_plus and sensor_plus_platform_interface are in two different packages. If the signature of the method changes, but the client project doesn't pull the correct version, this leads to a compilation error.

This means that we either need to pin the exact version or do a major release.

We had this issue before with other packages.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @vbuberen is this assumption correct? Does Melos support pinning and updating the exact version of *_platform_interface ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not really sure here, but during our usual release routine I will check it myself that the constraints for *_platform_interface are updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks both. I've flagged it as a breaking change as @miquelbeltran suggested. Is there anything further you need me to do in regards to this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, nothing else needed. Thanks

@aanas-sayed aanas-sayed changed the title feat(sensors_plus): add barometer support for all platforms feat(sensors_plus)!: add barometer support for all platforms Jul 14, 2024
Copy link
Collaborator

@vbuberen vbuberen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly good to me. Thanks a lot for this contribution.

@vbuberen
Copy link
Collaborator

From my side everything is good.
@miquelbeltran Can we merge this?

@miquelbeltran
Copy link
Member

I want to test on iOS still. @aanas-sayed mentioned that the iOS implementation is not tested either.

@miquelbeltran
Copy link
Member

I want to test on iOS still. aanas-sayed mentioned that the iOS implementation is not tested either.

Well scratch that, I realized I no longer have an iOS dev account, so I cannot test this.

@vbuberen
Copy link
Collaborator

Well scratch that, I realized I no longer have an iOS dev account, so I cannot test this.

I will test the PR on the iPhone.

@vbuberen
Copy link
Collaborator

Did some testing on iOS and indeed there is a need to do some additional work.

  1. There is a requirement to include NSMotionUsageDescription into Info.plist of the app (in this case example app) to explain why access to motion data is required. We need to add both this entry into Info.plist and mention this requirement in the documentation.
  2. On example app start there is a request to access motion data (see the screenshot attached), but even after providing such permission I still see a message saying that no barometer detected, so there is definitely a missing piece in implementation.

photo_2024-07-16 18 34 40
photo_2024-07-16 18 34 38

@aanas-sayed
Copy link
Contributor Author

Thanks for testing it. I'll look into these and try to fix them.

@vbuberen
Copy link
Collaborator

Thanks for testing it. I'll look into these and try to fix them.

In case it is hard to do due to not having a real iOS device I could try and fix everything myself. But will be able to do it not earlier than on weekend.

@aanas-sayed
Copy link
Contributor Author

I don't have an apple dev account but got Mac (fairly new to it) and iOS devices. Never tried to test an iOS app tbh. Heard you can get it going without an apple dev account so going to try that out. If it doesn't work, I might need your support to fix the issues. I'll let you know how I get along. Thanks!

@aanas-sayed
Copy link
Contributor Author

Fixed both the issues now I believe:

  1. The missing entry in Info.plist has been added.
  2. The missing barometer sensor issue was not a missing barometer issue it seems. It was an issue with incorrect type casting when sending the data which was being caught in the dart code which handled all errors as missing barometer errors. Should be fixed now.

IMG_0154

@vbuberen
Copy link
Collaborator

vbuberen commented Jul 18, 2024

I will be able to retest everything on a real device in ~6-8 hours, so will get back with feedback soon.

@vbuberen
Copy link
Collaborator

vbuberen commented Jul 19, 2024

I have done testing on both Android and iOS devices. Everything seems to work fine. There are a few improvement points, which can be done in a follow-up PR:

  1. Update README to mention that now on iOS devs need to provide NSMotionUsageDescription string in their Info.plist (I will make sure it is highlighted in changelogs as well when we release this feature).
  2. Add units to values in the example app and show that it is hPa numbers.

@miquelbeltran Do you have something to add or I can merge this one?

@miquelbeltran
Copy link
Member

Nope, we can go ahead!

@vbuberen vbuberen merged commit 5fa797d into fluttercommunity:main Jul 19, 2024
17 checks passed
@aanas-sayed aanas-sayed deleted the dev/barometer-955 branch July 19, 2024 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants