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: Consider ordering list of sensors alphabetically #2597 #2599

Open
wants to merge 5 commits into
base: development
Choose a base branch
from

Conversation

samruddhi-Rahegaonkar
Copy link

@samruddhi-Rahegaonkar samruddhi-Rahegaonkar commented Dec 27, 2024

Fix : Consider ordering list of sensors alphabetically #2597

Screenshot 2024-12-27 at 8 51 46 PM

Summary by Sourcery

Bug Fixes:

  • Fixed a bug where sensors with duplicate addresses were not displayed correctly.

Copy link

sourcery-ai bot commented Dec 27, 2024

Reviewer's Guide by Sourcery

This PR introduces alphabetical ordering to the list of displayed sensors in the SensorActivity. It also updates the Gradle dependencies to specific versions.

Sequence diagram for sensor detection and display flow

sequenceDiagram
    participant User
    participant SA as SensorActivity
    participant PS as PopulateSensors
    participant I2C

    User->>SA: Click Auto Scan
    SA->>PS: execute()
    PS->>I2C: scan()
    I2C-->>PS: detectedAddresses
    PS->>PS: Process detected sensors
    PS->>PS: Sort sensors alphabetically
    PS-->>SA: Update UI with sorted list
    SA-->>User: Display sorted sensor list
Loading

Class diagram showing changes to SensorActivity

classDiagram
    class SensorActivity {
        -I2C i2c
        -ScienceLab scienceLab
        -Map<Integer, List<String>> sensorAddr
        -List<String> dataName
        -ArrayAdapter<String> adapter
        -ListView lvSensor
        +onCreate(Bundle)
        -addSensorToMap(int address, String sensorName)
    }

    class PopulateSensors {
        -List<Integer> detectedAddresses
        +doInBackground()
        +onPostExecute()
    }

    SensorActivity *-- PopulateSensors

    note for SensorActivity "Changed sensorAddr from Map<Integer, String>
to Map<Integer, List<String>> to handle
duplicate addresses"
    note for PopulateSensors "Renamed 'data' to 'detectedAddresses'
and improved sensor detection logic"
Loading

File-Level Changes

Change Details Files
Sorts the sensor list alphabetically
  • Added Collections.sort(dataName) to sort the sensor list alphabetically after populating it.
  • Uses a LinkedHashMap to maintain the order of insertion of the sensors.
  • Added a new method addSensorToMap to handle adding sensors to the map, which creates a new list for duplicate addresses
app/src/main/java/io/pslab/activity/SensorActivity.java
Updates Gradle dependencies
  • Updated the Gradle version to 8.5.1
  • Added the IndicatorSeekBar dependency to app/build.gradle.kts
  • Removed the IndicatorSeekBar dependency from the root build.gradle.kts file
app/build.gradle.kts
build.gradle.kts

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.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a 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. You can also use
    this command to specify where the summary should be inserted.

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

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 @samruddhi-Rahegaonkar - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The gradle changes need attention: 1) Downgrading com.android.application from 8.7.3 to 8.5.1 should be justified or reverted 2) IndicatorSeekBar dependency is specified twice with different version formats
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

Comment on lines 180 to 189
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);

StringBuilder tvData = new StringBuilder();
if (data != null) {
for (Integer myInt : data) {
if (myInt != null && sensorAddr.get(myInt) != null) {
dataAddress.add(String.valueOf(myInt));
if (scienceLab.isConnected() && detectedAddresses != null) {
for (Integer address : detectedAddresses) {
if (sensorAddr.containsKey(address)) {
dataName.addAll(sensorAddr.get(address));
}
}

for (final String s : dataAddress) {
tvData.append(s).append(":").append(sensorAddr.get(Integer.parseInt(s))).append("\n");
}

} else {
tvData.append(getResources().getString(R.string.sensor_not_connected));
}
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): The connected state message is incorrect - it always shows 'not_connected' even when scienceLab.isConnected() is true

This could confuse users about the actual connection state of their device. Consider showing the list of detected sensors and their addresses when connected.

Comment on lines +191 to +199
// Add all sensors, even if not detected
for (List<String> sensors : sensorAddr.values()) {
for (String sensor : sensors) {
if (!dataName.contains(sensor)) {
dataName.add(sensor);
}
}
}
Collections.sort(dataName);
Copy link

Choose a reason for hiding this comment

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

suggestion: The UI doesn't distinguish between detected and available sensors, which could be misleading

Consider visually indicating which sensors are actually detected vs which ones are just potentially available, perhaps using different styles or grouping in the ListView.

Suggested implementation:

            // Create map of sensor names to their detection status
            Map<String, Boolean> sensorStatus = new HashMap<>();

            // First add all available sensors as undetected
            for (List<String> sensors : sensorAddr.values()) {
                for (String sensor : sensors) {
                    sensorStatus.put(sensor, false);
                }
            }

            // Mark detected sensors
            if (detectedAddresses != null) {
                for (int address : detectedAddresses) {
                    if (sensorAddr.containsKey(address)) {
                        for (String sensor : sensorAddr.get(address)) {
                            sensorStatus.put(sensor, true);
                        }
                    }
                }
            }

            // Create sorted list of sensor names
            List<String> sortedSensors = new ArrayList<>(sensorStatus.keySet());
            Collections.sort(sortedSensors);

            // Create and set custom adapter
            SensorListAdapter adapter = new SensorListAdapter(this, sortedSensors, sensorStatus);
            listView.setAdapter(adapter);

            if (scienceLab.isConnected()) {

You'll need to create a new SensorListAdapter class that extends ArrayAdapter<String>. Here's a template for it:

public class SensorListAdapter extends ArrayAdapter<String> {
    private final Map<String, Boolean> sensorStatus;

    public SensorListAdapter(Context context, List<String> sensors, Map<String, Boolean> status) {
        super(context, android.R.layout.simple_list_item_1, sensors);
        this.sensorStatus = status;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView text = view.findViewById(android.R.id.text1);
        String sensor = getItem(position);

        // Style based on detection status
        if (sensorStatus.get(sensor)) {
            text.setTextColor(ContextCompat.getColor(getContext(), android.R.color.black));
            text.setTypeface(null, Typeface.NORMAL);
        } else {
            text.setTextColor(ContextCompat.getColor(getContext(), android.R.color.darker_gray));
            text.setTypeface(null, Typeface.ITALIC);
        }

        return view;
    }
}

Also ensure you have a ListView with id listView in your layout XML file.

@AsCress
Copy link
Contributor

AsCress commented Dec 27, 2024

@samruddhi-Rahegaonkar If you have rebased your changes over the commits in #2589, then let's work on merging that one first and then work on this one. It's a nice practice though to always create new branches from a single branch that remains updated with the upstream (development in our case).
Is that PR ready for a final review from your side ?

@samruddhi-Rahegaonkar
Copy link
Author

@AsCress yes!

@marcnause marcnause changed the title Fix : Consider ordering list of sensors alphabetically #2597 fix : Consider ordering list of sensors alphabetically #2597 Dec 28, 2024
@marcnause marcnause changed the title fix : Consider ordering list of sensors alphabetically #2597 fix: Consider ordering list of sensors alphabetically #2597 Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants