Skip to content

fix: Refactor HomeScreen to use proper Provider pattern #1356

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

Conversation

nope3472
Copy link
Contributor

@nope3472 nope3472 commented Jun 30, 2025

fixes (#1358 )

Overview

This PR refactors state management in the BadgeMagic Flutter app to follow Flutter’s recommended Provider pattern.

Changes

  • Moved all provider creation to main.dart using MultiProvider
  • Removed direct provider instantiation from widgets
  • Widgets now access providers via context
  • Added missing wantKeepAlive getter to fix build errors

Benefits

  • Consistent, centralized state management
  • More efficient and reliable UI updates
  • Cleaner code that follows Flutter best practices

Summary by Sourcery

Refactor state management to follow Flutter’s Provider pattern by centralizing provider setup in main.dart and updating HomeScreen to consume providers via context

Enhancements:

  • Centralize all provider creation in main.dart using MultiProvider
  • Update HomeScreen to remove direct instantiation and access providers via context.watch/context.read
  • Move controller listener registration from initState to didChangeDependencies to prevent duplicate listeners
  • Add missing wantKeepAlive getter to HomeScreenState to resolve build errors

Copy link
Contributor

sourcery-ai bot commented Jun 30, 2025

Reviewer's Guide

This PR refactors state management to use Flutter’s Provider pattern by centralizing provider creation in main.dart and updating HomeScreen to consume providers via context, with lifecycle and listener improvements.

Sequence diagram for HomeScreen provider access after refactor

sequenceDiagram
    participant HomeScreen
    participant BuildContext
    participant AnimationBadgeProvider
    participant SpeedDialProvider
    participant BadgeMessageProvider
    participant InlineImageProvider
    HomeScreen->>BuildContext: build(context)
    HomeScreen->>AnimationBadgeProvider: context.watch<AnimationBadgeProvider>()
    HomeScreen->>SpeedDialProvider: context.watch<SpeedDialProvider>()
    HomeScreen->>BadgeMessageProvider: context.read<BadgeMessageProvider>()
    HomeScreen->>InlineImageProvider: context.watch<InlineImageProvider>()
Loading

Class diagram for HomeScreen state management refactor

classDiagram
    class HomeScreen {
        +State<HomeScreen> createState()
    }
    class _HomeScreenState {
        -TabController _tabController
        -ImageUtils imageUtils
        -InlineImageProvider inlineImageProvider
        -TextEditingController inlineimagecontroller
        -bool isPrefixIconClicked
        -int textfieldLength
        -String previousText
        -bool isDialInteracting
        -String errorVal
        -bool _controllerListenerAdded
        +void initState()
        +void didChangeDependencies()
        +void handleTextChange()
        +void _controllerListner()
        +void dispose()
        +void _setPortraitOrientation()
        +Future<void> _startImageCaching()
        +Widget build(BuildContext context)
        +bool get wantKeepAlive
    }
    HomeScreen -- _HomeScreenState : creates
    _HomeScreenState ..> AnimationBadgeProvider : uses via context
    _HomeScreenState ..> SpeedDialProvider : uses via context
    _HomeScreenState ..> BadgeMessageProvider : uses via context
    _HomeScreenState ..> InlineImageProvider : uses via context
    _HomeScreenState ..> TextEditingController : uses
    _HomeScreenState ..> TabController : uses
    _HomeScreenState ..> ImageUtils : uses
Loading

File-Level Changes

Change Details Files
Centralized provider setup in app entrypoint
  • Added AnimationBadgeProvider, SpeedDialProvider (via ChangeNotifierProxyProvider), and BadgeMessageProvider to MultiProvider in main.dart
  • Ensured InlineImageProvider is created through MultiProvider rather than in widgets
lib/main.dart
Refactored HomeScreen to consume providers via context
  • Removed direct instantiation of AnimationBadgeProvider, SpeedDialProvider, and BadgeMessageProvider in _HomeScreenState
  • Replaced nested MultiProvider in build with context.watch/Provider.of calls
  • Adjusted _controllerListner to obtain providers from context
lib/view/homescreen.dart
Moved controller listener to didChangeDependencies
  • Introduced _controllerListenerAdded flag to prevent duplicate listeners
  • Implemented didChangeDependencies to register text controller listener
lib/view/homescreen.dart
Enhanced widget lifecycle handling
  • Added missing wantKeepAlive override to preserve HomeScreen state
  • Streamlined dispose to stop animation and remove listeners via providers
lib/view/homescreen.dart

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

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

  • In main.dart, use ChangeNotifierProvider for BadgeMessageProvider instead of a plain Provider to ensure UI consumers rebuild when its state changes.
  • You’re still pulling InlineImageProvider via GetIt in HomeScreen—drop the GetIt calls and consistently use context.read/watch() now that it’s provided.
  • The DefaultTabController wrapper is redundant since you’re passing your own _tabController to TabBar/TabBarView—remove one to simplify the tab setup.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In main.dart, use ChangeNotifierProvider for BadgeMessageProvider instead of a plain Provider to ensure UI consumers rebuild when its state changes.
- You’re still pulling InlineImageProvider via GetIt in HomeScreen—drop the GetIt calls and consistently use context.read/watch<InlineImageProvider>() now that it’s provided.
- The DefaultTabController wrapper is redundant since you’re passing your own _tabController to TabBar/TabBarView—remove one to simplify the tab setup.

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
Copy link
Member

@nope3472 What is an issue have you created one ?

Copy link
Member

@samruddhi-Rahegaonkar samruddhi-Rahegaonkar left a comment

Choose a reason for hiding this comment

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

@nope3472 I saw you have removed didChangeAppLifecycleState Have you tested the Refactored HomeScreen where animations and texts resumes automatically when the app is in background ?

@nope3472
Copy link
Contributor Author

@samruddhi-Rahegaonkar yeah will reconfirm the applifecycle state

Copy link
Contributor

github-actions bot commented Jul 1, 2025

Build Status

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

Screenshots (Android)

Screenshots (iPhone)

Screenshots (iPad)

@nope3472 nope3472 requested a review from Jhalakupadhyay July 6, 2025 10:25
@hpdang hpdang requested review from kienvo and adityastic July 8, 2025 11:23
@mariobehling mariobehling removed the request for review from kienvo July 14, 2025 06:46
@@ -1,5 +1,8 @@
import 'package:badgemagic/providers/getitlocator.dart';
Copy link
Contributor

Choose a reason for hiding this comment

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

@nope3472 having all the providers global can make the app slower, and logically the providers should be setup and should be concentarted to files where they are being used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using global providers in our app ensures that shared state (like badge data and animations) stays consistent across all screens. It simplifies data flow, avoids duplication, and makes it easier to manage features that interact with each other. Plus, with Provider’s efficient rebuild mechanism, performance isn’t negatively impacted.

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.

4 participants