Skip to content

feat: Add Support for Renaming (Alias) BLE Devices in App #1362

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 6 commits into
base: development
Choose a base branch
from

Conversation

samruddhi-Rahegaonkar
Copy link
Member

@samruddhi-Rahegaonkar samruddhi-Rahegaonkar commented Jul 6, 2025

Fixes #1359

Changes

  • Integrated alias name support for BLE devices using BadgeAliasProvider.
  • Updated ScanState to match devices using either real name or alias.
  • Modified ConnectState to accept and display displayName via context.
  • Persisted alias mapping from Settings > Badge Scan Mode screen.
  • Cleaned up logic to avoid undefined getter (context) issue in ToastUtils.

Screenshots / Recordings

Checklist:

  • No hard coding: I have used resources from constants.dart without hard coding any value.
  • No end of file edits: No modifications done at end of resource files.
  • Code reformatting: I have reformatted code and fixed indentation in every file included in this pull request.
  • Code analyzation: My code passes analyzations run in flutter analyze and tests run in flutter test.

Summary by Sourcery

Add support for aliasing BLE devices by introducing new providers for scan configuration and alias storage, update scanning logic to recognize aliases, and enhance settings and UI components to configure and persist these mappings

New Features:

  • Add BadgeScanProvider and BadgeAliasProvider to manage BLE scan modes and device alias mappings
  • Allow users to configure badge scan mode and assign custom aliases in SettingsScreen

Enhancements:

  • Update ScanState to match devices by real name or alias and forward displayName to ConnectState
  • Refactor BadgeMessageProvider to integrate scan/alias providers and unify transferData/checkAndTransfer signatures
  • Extend HomeScreen with lifecycle observers to cache inline image input and clean up controller listeners

Chores:

  • Add BadgeScanSettingsWidget for dedicated scan settings UI
  • Upgrade Flutter SDK constraint to 3.32.1 in pubspec.yaml
  • Remove unused imports and fix undefined context issue in ToastUtils

Summary by Sourcery

Add support for aliasing BLE devices by introducing new providers, updating scan and transfer logic to use real names or aliases, and enhancing the settings UI to configure and persist these mappings

New Features:

  • Introduce BadgeScanProvider and BadgeAliasProvider to manage BLE scan modes and custom device aliases
  • Add UI controls in SettingsScreen and a new BadgeScanSettingsWidget to configure scan mode and assign aliases

Bug Fixes:

  • Resolve undefined context issue in ToastUtils

Enhancements:

  • Update ScanState to recognize devices by real name or alias and pass a displayName to ConnectState
  • Refactor BadgeMessageProvider and transfer flows to integrate scan/alias providers and accept BuildContext
  • Enhance HomeScreen with lifecycle observers to cache input and clean up controllers
  • Unify dropdown rendering in SettingsScreen via a helper and dispose text controllers properly

Build:

  • Bump Flutter SDK constraint to 3.32.1 in pubspec.yaml

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

feat: add badge scan mode selection and filtering support

fix: save and apply scan mode and badge names via provider

fix: unused variables and imports

fix: added comments

fix: resolved conflicts
Copy link
Contributor

sourcery-ai bot commented Jul 6, 2025

Reviewer's Guide

This PR adds alias-based support for BLE devices by introducing two new ChangeNotifier providers (BadgeScanProvider and BadgeAliasProvider), extending the SettingsScreen to configure scan modes and custom aliases, and refactoring the BLE scanning and transfer flow to recognize and display aliases.

Sequence diagram for BLE scan and connect with alias support

sequenceDiagram
  actor User
  participant SettingsScreen
  participant BadgeScanProvider
  participant BadgeAliasProvider
  participant BadgeMessageProvider
  participant ScanState
  participant ConnectState

  User->>SettingsScreen: Configure scan mode and aliases
  SettingsScreen->>BadgeScanProvider: setMode(), setBadgeNames()
  SettingsScreen->>BadgeAliasProvider: setAlias()
  User->>BadgeMessageProvider: checkAndTransfer(..., context)
  BadgeMessageProvider->>ScanState: new ScanState(..., mode, allowedNames, aliasProvider)
  ScanState->>ScanState: Scan for BLE devices
  ScanState->>BadgeAliasProvider: getAlias(realName)
  ScanState->>ConnectState: On match (by name or alias), pass displayName
  ConnectState->>BadgeMessageProvider: Complete connection and transfer
Loading

Class diagram for new and updated providers (BadgeScanProvider, BadgeAliasProvider)

classDiagram
  class BadgeScanProvider {
    - BadgeScanMode _mode
    - List<String> _badgeNames
    + BadgeScanMode get mode
    + List<String> get badgeNames
    + void setMode(BadgeScanMode mode)
    + void setBadgeNames(List<String> names)
    + void addBadgeName(String name)
    + void removeBadgeNameAt(int index)
    + void updateBadgeName(int index, String newName)
  }
  class BadgeAliasProvider {
    - Map<String, String> _aliases
    + String? getAlias(String deviceName)
    + void setAlias(String deviceName, String alias)
    + void removeAlias(String deviceName)
    + void clearAll()
    + Map<String, String> get allAliases
  }
  BadgeScanProvider <|-- ChangeNotifier
  BadgeAliasProvider <|-- ChangeNotifier
Loading

Class diagram for updated ScanState and ConnectState BLE flow

classDiagram
  class ScanState {
    + DataTransferManager manager
    + BadgeScanMode mode
    + List<String> allowedNames
    + BadgeAliasProvider aliasProvider
    + Future<BleState?> processState()
  }
  class ConnectState {
    + ScanResult scanResult
    + DataTransferManager manager
    + String displayName
    + Future<BleState?> processState()
  }
  ScanState --|> NormalBleState
  ConnectState --|> RetryBleState
  ScanState --> ConnectState : on device found
Loading

File-Level Changes

Change Details Files
Introduce providers for scan mode and alias management
  • Add BadgeScanProvider and BadgeAliasProvider classes
  • Register both providers in main.dart
  • Expose mode and alias APIs for external use
lib/providers/BadgeScanProvider.dart
lib/providers/BadgeAliasProvider.dart
lib/main.dart
Extend SettingsScreen to configure scan mode and device aliases
  • Import and consume BadgeScanProvider and BadgeAliasProvider via Provider
  • Initialize and dispose dynamic TextEditingControllers for real names and aliases
  • Add RadioListTiles and dynamic TextFields to select scan mode and enter names/aliases
  • Persist mode, badgeNames, and aliasMap on Save via providers
lib/view/settings_screen.dart
Refactor badge messaging provider to use scan mode and alias support
  • Inject BadgeScanProvider and BadgeAliasProvider into transferData and checkAndTransfer
  • Update method signatures to accept BuildContext
  • Retrieve scan configuration and alias map from providers when transferring data
lib/providers/badge_message_provider.dart
Enhance ScanState to match devices by real name or alias and forward displayName
  • Add mode, allowedNames, and aliasProvider fields to ScanState
  • Normalize and match device names against real names or aliases
  • Stop scanning on match and resolve displayName for ConnectState
lib/bademagic_module/bluetooth/scan_state.dart
Integrate alias context in UI flows and perform cleanup
  • Pass BuildContext to checkAndTransfer calls in HomeScreen, SaveBadgeScreen, and SaveBadgeCard
  • Introduce BadgeScanSettingsWidget for reusable scan settings UI
  • Upgrade Flutter SDK constraint in pubspec.yaml
  • Remove unused imports and fix undefined context issue in ToastUtils
lib/view/homescreen.dart
lib/view/save_badge_screen.dart
lib/view/widgets/save_badge_card.dart
lib/view/badgeScanSettingsWidget.dart
pubspec.yaml

Assessment against linked issues

Issue Objective Addressed Explanation
#1359 Allow users to assign custom aliases to BLE devices within the app.
#1359 Persist the alias mappings locally so that the app remembers the aliases between sessions.
#1359 Display the alias instead of the BLE device name in the app UI.

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

@samruddhi-Rahegaonkar samruddhi-Rahegaonkar marked this pull request as ready for review July 7, 2025 13:24
Copy link
Contributor

@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:

  • The new ConnectState signature takes a displayName but doesn’t assign it to any field—add a member and initialize it in the constructor so the displayName is actually used.
  • You have nearly identical badge/alias form logic in SettingsScreen and BadgeScanSettingsWidget—consider extracting that into a shared widget or consolidating one to reduce duplication.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new ConnectState signature takes a displayName but doesn’t assign it to any field—add a member and initialize it in the constructor so the displayName is actually used.
- You have nearly identical badge/alias form logic in SettingsScreen and BadgeScanSettingsWidget—consider extracting that into a shared widget or consolidating one to reduce duplication.

## Individual Comments

### Comment 1
<location> `lib/view/settings_screen.dart:166` </location>
<code_context>
+                    }
+                  }
+                }
+                provider.setBadgeNames(badgeNames); // Save aliases
+                final aliasProvider =
+                    Provider.of<BadgeAliasProvider>(context, listen: false);
</code_context>

<issue_to_address>
The comment '// Save aliases' is misleading here.

setBadgeNames only updates badge names. Consider removing or clarifying the comment to prevent confusion.
</issue_to_address>

### Comment 2
<location> `lib/bademagic_module/bluetooth/scan_state.dart:63` </location>
<code_context>
+            );
+
+            isCompleted = true;
+            FlutterBluePlus.stopScan();
+            toast.showToast('Device found. Connecting...');
+
</code_context>

<issue_to_address>
Calling stopScan inside the scan result handler may cause race conditions.

If scan results arrive rapidly, stopScan could be called multiple times, disrupting the scan process. Use a flag or similar mechanism to ensure stopScan is only invoked once.
</issue_to_address>

### Comment 3
<location> `lib/bademagic_module/bluetooth/connect_state.dart:10` </location>
<code_context>
   final ScanResult scanResult;
   final DataTransferManager manager;

-  ConnectState({required this.manager, required this.scanResult});
+  ConnectState(
+      {required this.manager,
+      required this.scanResult,
</code_context>

<issue_to_address>
The displayName parameter is added but not stored or used in ConnectState.

Remove displayName from the constructor if it's unnecessary, or add it as a field if it will be used.
</issue_to_address>

### Comment 4
<location> `lib/providers/BadgeAliasProvider.dart:4` </location>
<code_context>
+import 'package:flutter/material.dart';
+
+class BadgeAliasProvider with ChangeNotifier {
+  final Map<String, String> _aliases = {}; // Key: deviceName, Value: alias
+
+  /// Get alias by badge/device name
</code_context>

<issue_to_address>
The alias map uses deviceName as the key, which may cause issues if device names are not unique.

If device names aren't guaranteed unique, use a unique identifier (e.g., MAC address) as the key to avoid collisions.
</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.

@samruddhi-Rahegaonkar samruddhi-Rahegaonkar force-pushed the issue1359 branch 4 times, most recently from 7869e69 to 5bc8ff4 Compare July 7, 2025 13:46
Copy link
Contributor

github-actions bot commented Jul 7, 2025

Build Status

Build successful. APKs to test: https://github.com/fossasia/badgemagic-app/actions/runs/16625217196/artifacts/3649383008.

Screenshots (Android)

Screenshots (iPhone)

Screenshots (iPad)

@hpdang hpdang requested a review from nope3472 July 8, 2025 11:55
@hpdang
Copy link
Member

hpdang commented Jul 8, 2025

@nope3472 please review this

Copy link
Contributor

@nope3472 nope3472 left a comment

Choose a reason for hiding this comment

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

Add a check in the settings UI to warn/prevent duplicate real badge names or duplicate aliases per user.
-Else Great work.

@samruddhi-Rahegaonkar
Copy link
Member Author

samruddhi-Rahegaonkar commented Jul 8, 2025

Add a check in the settings UI to warn/prevent duplicate real badge names or duplicate aliases per user. -Else Great work.

Good catch! I have added check for duplicate prevention 👍

@samruddhi-Rahegaonkar samruddhi-Rahegaonkar force-pushed the issue1359 branch 2 times, most recently from 7e35a08 to dc7f557 Compare July 8, 2025 15:55
@fossasia fossasia deleted a comment from mariobehling Jul 15, 2025
@hpdang
Copy link
Member

hpdang commented Jul 16, 2025

@nope3472 please review this again

Copy link
Contributor

@nope3472 nope3472 left a comment

Choose a reason for hiding this comment

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

-The implementation of alias support is well integrated (provider, settings, UI).

  • Would it be possible to add an option to reset/remove an alias and revert to the default device name?

@samruddhi-Rahegaonkar
Copy link
Member Author

@nope3472 I think we should add only one alias to badge name so it is okay if we dont have option to add or remove either way its optional.

fix: fixed the issues according to review

fix: format code to match Dart standards

fix: added check for duplicate checking

fix: show succesfully connected as alias name
@hpdang
Copy link
Member

hpdang commented Jul 30, 2025

@samruddhi-Rahegaonkar update the branch please

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.

Configuration feature : Add Support for Renaming (Alias) BLE Devices in App
3 participants