Skip to content

feat: implementation of the Compass page #2710

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

Open
wants to merge 8 commits into
base: flutter
Choose a base branch
from

Conversation

Yugesh-Kumar-S
Copy link
Collaborator

@Yugesh-Kumar-S Yugesh-Kumar-S commented May 31, 2025

Fixes #2707

Changes

  • Added the Compass instrument.
  • Implemented compass when ground parallel to X,Y,Z direction.

Screenshots / Recordings

Checklist:

  • No hard coding: I have used resources from strings.xml, dimens.xml and colors.xml without hard coding any value.
  • No end of file edits: No modifications done at end of resource files strings.xml, dimens.xml or colors.xml.
  • Code reformatting: I have reformatted code and fixed indentation in every file included in this pull request.
  • No extra space: My code does not contain any extra lines or extra spaces than the ones that are necessary.

Summary by Sourcery

Add a new Compass instrument page that displays a live, rotatable compass based on magnetometer and accelerometer data with selectable ground-parallel axes.

New Features:

  • Introduce CompassScreen with a rotating compass UI and selectable X/Y/Z axes for orientation measurement
  • Implement CompassProvider to stream sensor data, compute and smooth heading, and expose axis-specific degree readings

Enhancements:

  • Register '/compass' route and integrate it into the instrument navigation flow
  • Add motion sensor usage description to iOS Info.plist
  • Define new string resources for compass UI labels and sensor error messages

Copy link

sourcery-ai bot commented May 31, 2025

Reviewer's Guide

This PR adds a new Compass instrument page powered by device magnetometer and accelerometer streams, with real-time heading display, selectable ground-parallel axes, and integrates it into the app’s navigation.

Class diagram for CompassProvider and CompassScreen

classDiagram
  class CompassProvider {
    - MagnetometerEvent _magnetometerEvent
    - AccelerometerEvent _accelerometerEvent
    - StreamSubscription? _magnetometerSubscription
    - StreamSubscription? _accelerometerSubscription
    - String _selectedAxis
    - double _currentDegree
    - int _direction
    - double _smoothedHeading
    + MagnetometerEvent get magnetometerEvent
    + AccelerometerEvent get accelerometerEvent
    + String get selectedAxis
    + double get currentDegree
    + int get direction
    + double get smoothedHeading
    + void initializeSensors()
    + void disposeSensors()
    + void onAxisSelected(String axis)
    + double getDegreeForAxis(String axis)
  }
  class CompassScreen {
    + Widget build(BuildContext context)
  }
  class CompassScreenContent {
    + Widget build(BuildContext context)
  }
  CompassScreen --> CompassScreenContent
  CompassScreenContent --> CompassProvider
Loading

File-Level Changes

Change Details Files
Implement CompassScreen UI and navigation integration
  • Create CompassScreen and stateful UI layout with rotating compass and axis selectors
  • Wrap screen in ChangeNotifierProvider for CompassProvider
  • Register '/compass' route in main.dart
  • Add navigation case in instruments_screen.dart to open Compass page
lib/view/compass_screen.dart
lib/main.dart
lib/view/instruments_screen.dart
Add CompassProvider for sensor handling and heading computation
  • Subscribe to magnetometer and accelerometer streams
  • Compute orientation per selected axis with trigonometry and smoothing filter
  • Expose current heading, axis data, and selection logic
  • Handle sensor errors with logger and constant messages
lib/providers/compass_provider.dart
Extend constants and platform permissions for compass
  • Add compassTitle, axis-parallel label, and error message strings
  • Insert NSMotionUsageDescription in iOS Info.plist
  • Update AndroidManifest for flutter embedding metadata
lib/constants.dart
ios/Runner/Info.plist
android/app/src/main/AndroidManifest.xml
Integrate sensors_plus dependency
  • Import sensors_plus in CompassProvider
  • Configure sensor streams for live data access
lib/providers/compass_provider.dart

Assessment against linked issues

Issue Objective Addressed Explanation
#2707 Maintain consistency with the existing PSLab Android app’s Compass behavior.
#2707 Ensure smooth transition and rotation of the compass needle based on device orientation.
#2707 Integrate real-time directional updates using magnetometer and accelerometer sensors.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Yugesh-Kumar-S Yugesh-Kumar-S force-pushed the Compass branch 2 times, most recently from 8eb04be to 896fb86 Compare June 2, 2025 16:23
Copy link

github-actions bot commented Jun 2, 2025

@AsCress AsCress added the Status: Review Required Requested reviews from peers and maintainers label Jun 5, 2025
@mariobehling mariobehling requested a review from marcnause June 12, 2025 08:42
@marcnause
Copy link
Contributor

Is this PR still work in progress?

@Yugesh-Kumar-S
Copy link
Collaborator Author

Yugesh-Kumar-S commented Jun 16, 2025

Is this PR still work in progress?

I’ve tested this on Android, and the compass functionality works well. However, since iOS handles magnetometer data differently, we should test it on an iOS device to ensure consistent behavior and make changes to it accordingly .

@marcnause
Copy link
Contributor

@AsCress, can you test this on an iOS device?

@AsCress
Copy link
Collaborator

AsCress commented Jun 22, 2025

@marcnause Unfortunately, currently we can only test on an iOS device after merging the PR.

@AsCress
Copy link
Collaborator

AsCress commented Jun 22, 2025

@Yugesh-Kumar-S Resolve conflicts here. Let's merge this.

@Yugesh-Kumar-S Yugesh-Kumar-S marked this pull request as ready for review June 22, 2025 16:30
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Yugesh-Kumar-S - I've reviewed your changes - here's some feedback:

  • The degree display uses .round().toStringAsFixed(1), but .round() returns an int—either remove the round or call .toStringAsFixed(1) directly on the double to avoid a type mismatch.
  • Axis selector labels like “X axis” and “Y axis” are hardcoded in the widget—extract them into constants or localization strings to keep your UI text consistent.
  • You’re calling notifyListeners() on every magnetometer and accelerometer event, causing two rebuilds per sensor tick—consider throttling or batching sensor updates to reduce unnecessary rebuilds.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The degree display uses `.round().toStringAsFixed(1)`, but `.round()` returns an int—either remove the round or call `.toStringAsFixed(1)` directly on the double to avoid a type mismatch.
- Axis selector labels like “X axis” and “Y axis” are hardcoded in the widget—extract them into constants or localization strings to keep your UI text consistent.
- You’re calling `notifyListeners()` on every magnetometer and accelerometer event, causing two rebuilds per sensor tick—consider throttling or batching sensor updates to reduce unnecessary rebuilds.

## Individual Comments

### Comment 1
<location> `lib/providers/compass_provider.dart:30` </location>
<code_context>
+  double get smoothedHeading => _smoothedHeading;
+
+  void initializeSensors() {
+    _magnetometerSubscription = magnetometerEventStream().listen(
+      (event) {
+        _magnetometerEvent = event;
+        _updateCompassDirection();
+        notifyListeners();
+      },
+      onError: (error) {
+        logger.e("$magnetometerError: $error");
+      },
+      cancelOnError: true,
+    );
+
</code_context>

<issue_to_address>
Using cancelOnError: true may stop sensor updates permanently after a single error.

Consider handling errors within the stream without setting cancelOnError: true, or implement a mechanism to re-initialize the sensors if the stream is cancelled.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Yugesh-Kumar-S
Copy link
Collaborator Author

@Yugesh-Kumar-S Resolve conflicts here. Let's merge this.

@AsCress Resolved conflicts and refactored existing hardcoded values. Please merge and test for ios, if it looks good.

@marcnause marcnause requested a review from AsCress June 30, 2025 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flutter Status: Review Required Requested reviews from peers and maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compass UI with real-time directional updates
3 participants