Skip to content

feat: Enhance Badge with Adjustable Brightness Support #1271

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 1 commit into
base: flutter_app
Choose a base branch
from

Conversation

samruddhi-Rahegaonkar
Copy link
Contributor

@samruddhi-Rahegaonkar samruddhi-Rahegaonkar commented Apr 8, 2025

Fixes #866

Changes

  • Added toggle to enable/disable brightness control for virtual and connected badge
  • Implemented brightness slider to set LED intensity
  • Integrated brightness rendering directly on the virtual badge using CustomPainter
  • Synced brightness level with provider state management
  • Refactored BadgePaint to adapt color intensity based on brightness value

Screenshots / Recordings

video.mp4

Checklist:

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

Summary by Sourcery

Add brightness control support for virtual and connected badges, enabling users to adjust LED intensity through a slider and toggle

New Features:

  • Implement brightness control for both virtual and physical badges
  • Add a toggle to enable/disable brightness adjustment
  • Integrate brightness slider with LED rendering

Enhancements:

  • Refactor badge painting to support dynamic brightness rendering
  • Create a dedicated Bluetooth brightness service for device interaction

Copy link
Contributor

sourcery-ai bot commented Apr 8, 2025

Reviewer's Guide by Sourcery

This pull request introduces brightness control functionality for both virtual and connected badges. It includes a toggle to enable/disable brightness control, a slider to set the LED intensity, and integration of brightness rendering on the virtual badge. The brightness level is managed using a provider, and the badge data transfer process has been updated to include brightness information.

Sequence diagram for setting brightness

Loading
sequenceDiagram
    participant User
    participant HomeScreen
    participant BadgeBrightnessProvider
    participant BleBrightnessService
    participant BluetoothDevice

    User->HomeScreen: Adjusts brightness slider
    HomeScreen->BadgeBrightnessProvider: setBrightness(brightnessLevel)
    activate BadgeBrightnessProvider
    BadgeBrightnessProvider->BleBrightnessService: setBrightness(brightnessLevel)
    activate BleBrightnessService
    alt isConnected == true
        BleBrightnessService->BluetoothDevice: Writes brightness value
        activate BluetoothDevice
        BluetoothDevice-->>BleBrightnessService: Acknowledgment
        deactivate BluetoothDevice
    else isConnected == false
        BleBrightnessService-->>BadgeBrightnessProvider: Returns false
    end
    BleBrightnessService-->>BadgeBrightnessProvider: Returns success
    deactivate BleBrightnessService
    BadgeBrightnessProvider-->>HomeScreen: Notifies listeners
    deactivate BadgeBrightnessProvider
    HomeScreen->HomeScreen: Rebuilds UI with new brightness

Updated class diagram for BadgeBrightnessProvider

Loading
classDiagram
    class BadgeBrightnessProvider {
        -BleBrightnessService _bleBrightnessService
        -double _brightness
        -bool _isConnected
        -String _errorMessage
        -bool _isBrightnessVisible
        +double brightness
        +bool isConnected
        +String errorMessage
        +bool isBrightnessVisible
        +BadgeBrightnessProvider(BleBrightnessService bleBrightnessService)
        -Future<void> _initialize()
        +void toggleBrightnessVisibility(bool value)
        +Future<bool> connectToDevice(String deviceId)
        +Future<bool> setBrightness(double brightnessLevel)
        +Future<void> getCurrentBrightness()
        +Future<void> disconnect()
        +void dispose()
    }
    class BleBrightnessService {
        +Future<bool> connectToDevice(String deviceId)
        +Future<bool> setBrightness(int brightnessLevel)
        +Future<int?> getCurrentBrightness()
        +Future<void> disconnect()
    }

    BadgeBrightnessProvider -- BleBrightnessService : depends on

Updated class diagram for BadgePaint

Loading
classDiagram
    class BadgePaint {
        -BadgeUtils badgeUtils
        -List~List~bool~~ grid
        -double brightness
        +BadgePaint(List~List~bool~~ grid, double brightness)
        +void paint(Canvas canvas, Size size)
        +bool shouldRepaint(BadgePaint oldDelegate)
    }

File-Level Changes

Change Details Files
Introduced a BadgeBrightnessProvider to manage the brightness level of the badge, including connection status and error handling.
  • Added BleBrightnessService for handling Bluetooth Low Energy (BLE) communication to control badge brightness.
  • Implemented methods for connecting to a device, discovering services, setting brightness, and reading the current brightness.
  • Created a provider to manage the brightness state, connection status, and error messages.
  • Added methods to toggle brightness visibility and set the brightness level.
  • Integrated the provider into the UI to display connection status, brightness controls, and error messages.
lib/view/homescreen.dart
lib/main.dart
lib/virtualbadge/view/draw_badge.dart
lib/virtualbadge/view/badge_paint.dart
lib/virtualbadge/view/animated_badge.dart
lib/services/BleBrightnessService.dart
lib/providers/BadgeBrightnessProvider.dart
Enabled brightness control for both virtual and connected badges.
  • Added a toggle to enable/disable brightness control.
  • Implemented a slider to set the LED intensity.
  • Integrated brightness rendering directly on the virtual badge using CustomPainter.
  • Synced the brightness level with the provider state management.
  • Refactored BadgePaint to adapt color intensity based on the brightness value.
lib/view/homescreen.dart
lib/virtualbadge/view/draw_badge.dart
lib/virtualbadge/view/badge_paint.dart
lib/virtualbadge/view/animated_badge.dart
Modified the badge data transfer process to include brightness information.
  • Updated the badge data transfer logic to include the current brightness level.
  • Ensured that the brightness value is passed as part of the transfer parameters when sending data to the badge.
  • Included the connection status in the transfer parameters.
lib/view/homescreen.dart

Assessment against linked issues

Issue Objective Addressed Explanation
#866 Add a setting to the application to control the display brightness.
#866 Allow users to adjust the brightness to save battery, especially at night.

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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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

Overall Comments:

  • Consider adding a loading state to the UI while connecting to the badge, to improve user experience.
  • The YOUR_DEVICE_ID string should be replaced with a persistent storage lookup.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue 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.

@@ -304,8 +442,11 @@ class _HomeScreenState extends State<HomeScreen>
speedDialProvider.getOuterValue(),
modeValueMap[animationProvider
.getAnimationIndex()],
null,
false);
brightnessProvider.brightness.round()
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Suspicious cast from int to Map<String, dynamic>.

The call to badgeData.checkAndTransfer is passing brightnessProvider.brightness.round() casted to Map<String, dynamic>? even though brightness.round() produces an int. If the intent is to pass brightness as a parameter, consider wrapping it in a map (e.g., {"brightness": brightnessProvider.brightness.round()}) rather than casting an int. Ensure the parameter type expected by checkAndTransfer is correctly respected.

Copy link
Contributor

github-actions bot commented Apr 8, 2025

Build Status

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

Screenshots (Android)

Screenshots (iPhone)

Screenshots (iPad)

@samruddhi-Rahegaonkar samruddhi-Rahegaonkar changed the title Enhance Badge with Adjustable Brightness Support feat: Enhance Badge with Adjustable Brightness Support Apr 8, 2025
@samruddhi-Rahegaonkar
Copy link
Contributor Author

samruddhi-Rahegaonkar commented Apr 9, 2025

@adityastic Just to confirm, is this change meant to address the issue referenced? because i guess for this we need Live polling option in our App to connect badge to app for long hours.

@samruddhi-Rahegaonkar
Copy link
Contributor Author

@adityastic Just to confirm, is this change meant to address the issue referenced? because i guess for this we need Live polling option in our App to connect badge to app for long hours.

@Jhalakupadhyay what do you think on this ?

@Jhalakupadhyay
Copy link
Contributor

@adityastic Just to confirm, is this change meant to address the issue referenced? because i guess for this we need Live polling option in our App to connect badge to app for long hours.

@Jhalakupadhyay what do you think on this ?

The brightness is controlled by the badge itself by using the existing button so we don't have to do anything on the app

@samruddhi-Rahegaonkar
Copy link
Contributor Author

@adityastic Just to confirm, is this change meant to address the issue referenced? because i guess for this we need Live polling option in our App to connect badge to app for long hours.

@Jhalakupadhyay what do you think on this ?

The brightness is controlled by the badge itself by using the existing button so we don't have to do anything on the app

Ohkay! so there is no need to add brightness control to badge it already has that feature ?

@samruddhi-Rahegaonkar
Copy link
Contributor Author

@Jhalakupadhyay should i close PR then ?

@Jhalakupadhyay
Copy link
Contributor

@Jhalakupadhyay should i close PR then ?

Yes because managing brightness at the app is unnecessary and badge brightness is handled by the firmware.

@samruddhi-Rahegaonkar
Copy link
Contributor Author

samruddhi-Rahegaonkar commented Apr 10, 2025

@Jhalakupadhyay should i close PR then ?

Yes because managing brightness at the app is unnecessary and badge brightness is handled by the firmware.

Ohkay! i did it cause i saw the issue #866 does this refer to anything different that i solved ?

@Jhalakupadhyay
Copy link
Contributor

@Jhalakupadhyay should i close PR then ?

Yes because managing brightness at the app is unnecessary and badge brightness is handled by the firmware.

Ohkay! i did it cause i saw the issue #866 does this refer to anything different that i solved ?

Ahh I see we would need to controll it from the app according to the new firmware docs but before that we would need to have the streaming feature ready let's focus on that first.

@samruddhi-Rahegaonkar
Copy link
Contributor Author

@Jhalakupadhyay should i close PR then ?

Yes because managing brightness at the app is unnecessary and badge brightness is handled by the firmware.

Ohkay! i did it cause i saw the issue #866 does this refer to anything different that i solved ?

Ahh I see we would need to controll it from the app according to the new firmware docs but before that we would need to have the streaming feature ready let's focus on that first.

Yeah! we need Live rendering! and our app should connect to the badge for long hours right ?

@Jhalakupadhyay
Copy link
Contributor

@Jhalakupadhyay should i close PR then ?

Yes because managing brightness at the app is unnecessary and badge brightness is handled by the firmware.

Ohkay! i did it cause i saw the issue #866 does this refer to anything different that i solved ?

Ahh I see we would need to controll it from the app according to the new firmware docs but before that we would need to have the streaming feature ready let's focus on that first.

Yeah! we need Live rendering! and our app should connect to the badge for long hours right ?

Yeah kind of let's just focus on that first then we can have the rest of the features in place

@samruddhi-Rahegaonkar
Copy link
Contributor Author

@Jhalakupadhyay The work i have did it aligns to the issue ?

@samruddhi-Rahegaonkar
Copy link
Contributor Author

samruddhi-Rahegaonkar commented Apr 10, 2025

@Jhalakupadhyay should i close PR then ?

Yes because managing brightness at the app is unnecessary and badge brightness is handled by the firmware.

Ohkay! i did it cause i saw the issue #866 does this refer to anything different that i solved ?

Ahh I see we would need to controll it from the app according to the new firmware docs but before that we would need to have the streaming feature ready let's focus on that first.

Yeah! we need Live rendering! and our app should connect to the badge for long hours right ?

Yeah kind of let's just focus on that first then we can have the rest of the features in place

Sure! So i will be continuing to work on it ! so i guess #1242

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.

Please add setting for the display brightness

None yet

2 participants