Skip to content

Commit 7d32bc8

Browse files
committed
fix(headlines): prevent 'Custom' chip from showing when a saved filter is selected & apply and select newly saved filter automatically
This commit fixes a bug where creating and applying a new saved filter would not automatically select its corresponding chip in the filter bar. The `HeadlinesFeedFiltersApplied` event now optionally carries the `SavedFilter` that was just created. The `HeadlinesFeedBloc` uses this information to correctly identify and set the new filter as active, ensuring the UI immediately reflects the change. If no saved filter is passed, the logic correctly falls back to identifying a pre-existing saved filter or marking the state as 'custom'.
1 parent 000e653 commit 7d32bc8

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

lib/headlines-feed/bloc/headlines_feed_bloc.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,28 @@ class HeadlinesFeedBloc extends Bloc<HeadlinesFeedEvent, HeadlinesFeedState> {
248248
HeadlinesFeedFiltersApplied event,
249249
Emitter<HeadlinesFeedState> emit,
250250
) async {
251+
String? newActiveFilterId;
252+
253+
// If a saved filter was explicitly passed (e.g., just created), use its ID.
254+
if (event.savedFilter != null) {
255+
newActiveFilterId = event.savedFilter!.id;
256+
} else {
257+
// Otherwise, determine if this is a pre-existing saved filter being
258+
// re-applied or a custom one.
259+
final isSavedFilter =
260+
state.activeFilterId != 'all' &&
261+
state.activeFilterId != 'custom' &&
262+
state.activeFilterId != null;
263+
newActiveFilterId = isSavedFilter ? state.activeFilterId : 'custom';
264+
}
265+
251266
// When applying new filters, this is considered a major feed change,
252267
// so we clear the ad cache to get a fresh set of relevant ads.
253268
_inlineAdCacheService.clearAllAds();
254269
emit(
255270
state.copyWith(
256-
// Applying a filter from the filter page always results in a 'custom' state.
257271
filter: event.filter,
258-
activeFilterId: 'custom',
272+
activeFilterId: newActiveFilterId,
259273
status: HeadlinesFeedStatus.loading,
260274
feedItems: [],
261275
cursor: null,

lib/headlines-feed/bloc/headlines_feed_event.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,23 @@ final class HeadlinesFeedFiltersApplied extends HeadlinesFeedEvent {
5959
const HeadlinesFeedFiltersApplied({
6060
required this.filter,
6161
required this.adThemeStyle,
62+
this.savedFilter,
6263
});
6364

6465
/// The [HeadlineFilter] containing the selected categories, sources,
6566
/// and/or countries.
6667
final HeadlineFilter filter;
6768

69+
/// The optional [SavedFilter] that this filter corresponds to.
70+
/// This is used to correctly set the active filter ID when a new
71+
/// filter is saved and immediately applied.
72+
final SavedFilter? savedFilter;
73+
6874
/// The current ad theme style of the application.
6975
final AdThemeStyle adThemeStyle;
7076

7177
@override
72-
List<Object?> get props => [filter, adThemeStyle];
78+
List<Object?> get props => [filter, adThemeStyle, savedFilter];
7379
}
7480

7581
/// {@template headlines_feed_filters_cleared}

lib/headlines-feed/view/headlines_filter_page.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,17 @@ class HeadlinesFilterPage extends StatelessWidget {
5151
}
5252
}
5353

54-
class _HeadlinesFilterView extends StatelessWidget {
54+
class _HeadlinesFilterView extends StatefulWidget {
5555
const _HeadlinesFilterView();
5656

57+
@override
58+
State<_HeadlinesFilterView> createState() => _HeadlinesFilterViewState();
59+
}
60+
61+
class _HeadlinesFilterViewState extends State<_HeadlinesFilterView> {
62+
/// Track the most recently saved filter within this page's lifecycle.
63+
SavedFilter? _newlySavedFilter;
64+
5765
/// Builds a [ListTile] representing a filter criterion (e.g., Categories).
5866
///
5967
/// Displays the criterion [title], the number of currently selected items
@@ -144,6 +152,8 @@ class _HeadlinesFilterView extends StatelessWidget {
144152
sources: filterState.selectedSources.toList(),
145153
countries: filterState.selectedCountries.toList(),
146154
);
155+
// Keep track of the newly saved filter.
156+
setState(() => _newlySavedFilter = newFilter);
147157
context.read<AppBloc>().add(
148158
SavedFilterAdded(filter: newFilter),
149159
);
@@ -174,6 +184,8 @@ class _HeadlinesFilterView extends StatelessWidget {
174184
context.read<HeadlinesFeedBloc>().add(
175185
HeadlinesFeedFiltersApplied(
176186
filter: newFilter,
187+
// Pass the newly saved filter if it exists.
188+
savedFilter: _newlySavedFilter,
177189
adThemeStyle: AdThemeStyle.fromTheme(Theme.of(context)),
178190
),
179191
);

0 commit comments

Comments
 (0)