Skip to content

Commit dd5c8b9

Browse files
committed
feat(feed): prime HeadlinesFeedBloc with initial saved filters
Refactors the `HeadlinesFeedBloc` to accept an optional `initialUserContentPreferences` parameter in its constructor. This change resolves a race condition where the BLoC would be created after the `AppBloc` had already loaded the user's data, causing the initial list of saved filters to be missed. By injecting the initial state, the `HeadlinesFeedBloc` is now immediately aware of the user's saved filters, ensuring the `SavedFiltersBar` is correctly populated for all users upon navigation to the feed.
1 parent a1944ca commit dd5c8b9

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/headlines-feed/bloc/headlines_feed_bloc.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,25 @@ class HeadlinesFeedBloc extends Bloc<HeadlinesFeedEvent, HeadlinesFeedState> {
3030
required FeedDecoratorService feedDecoratorService,
3131
required AppBloc appBloc,
3232
required InlineAdCacheService inlineAdCacheService,
33+
UserContentPreferences? initialUserContentPreferences,
3334
}) : _headlinesRepository = headlinesRepository,
3435
_feedDecoratorService = feedDecoratorService,
3536
_appBloc = appBloc,
3637
_inlineAdCacheService = inlineAdCacheService,
37-
super(const HeadlinesFeedState()) {
38+
super(
39+
HeadlinesFeedState(
40+
// Initialize the state with saved filters from the AppBloc's
41+
// current state. This prevents a race condition where the
42+
// feed bloc is created after the AppBloc has already loaded
43+
// the user's preferences, ensuring the UI has the data
44+
// from the very beginning.
45+
savedFilters:
46+
initialUserContentPreferences?.savedFilters ?? const [],
47+
),
48+
) {
3849
// Subscribe to AppBloc to receive updates on user preferences.
50+
// This handles subsequent changes to preferences (e.g., adding/deleting
51+
// a filter) while the feed page is active.
3952
_appBlocSubscription = _appBloc.stream.listen((appState) {
4053
// Check if userContentPreferences has changed and is not null.
4154
if (appState.userContentPreferences != null &&

0 commit comments

Comments
 (0)