Skip to content

Commit 70cb038

Browse files
committed
feat(filter): Persist filter state and apply
- Added isFromFollowedItems to HeadlineFilter - Persist the flag in filter state - Use flag to filter followed items
1 parent d18b74b commit 70cb038

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

lib/headlines-feed/view/headlines_filter_page.dart

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
7070
final currentFilter = headlinesFeedState.filter;
7171
_tempSelectedCategories = List.from(currentFilter.categories ?? []);
7272
_tempSelectedSources = List.from(currentFilter.sources ?? []);
73-
_tempSelectedSourceCountryIsoCodes =
74-
Set.from(currentFilter.selectedSourceCountryIsoCodes ?? {});
75-
_tempSelectedSourceSourceTypes =
76-
Set.from(currentFilter.selectedSourceSourceTypes ?? {});
73+
_tempSelectedSourceCountryIsoCodes = Set.from(
74+
currentFilter.selectedSourceCountryIsoCodes ?? {},
75+
);
76+
_tempSelectedSourceSourceTypes = Set.from(
77+
currentFilter.selectedSourceSourceTypes ?? {},
78+
);
7779

7880
// Use the new flag from the filter to set the checkbox state
7981
initialUseFollowedFilters = currentFilter.isFromFollowedItems;
@@ -161,12 +163,14 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
161163
}
162164
} on NotFoundException {
163165
setState(() {
164-
_currentUserPreferences =
165-
UserContentPreferences(id: currentUser.id); // Empty prefs
166+
_currentUserPreferences = UserContentPreferences(
167+
id: currentUser.id,
168+
); // Empty prefs
166169
_tempSelectedCategories = [];
167170
_tempSelectedSources = [];
168171
_isLoadingFollowedFilters = false;
169-
_useFollowedFilters = false; // Uncheck as no prefs found (implies no followed)
172+
_useFollowedFilters =
173+
false; // Uncheck as no prefs found (implies no followed)
170174
});
171175
if (mounted) {
172176
ScaffoldMessenger.of(context)
@@ -182,8 +186,7 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
182186
setState(() {
183187
_isLoadingFollowedFilters = false;
184188
_useFollowedFilters = false; // Uncheck the box
185-
_loadFollowedFiltersError =
186-
e.message; // Or a generic "Failed to load"
189+
_loadFollowedFiltersError = e.message; // Or a generic "Failed to load"
187190
});
188191
} catch (e) {
189192
setState(() {
@@ -238,17 +241,18 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
238241
subtitle: Text(subtitle),
239242
trailing: const Icon(Icons.chevron_right),
240243
enabled: enabled, // Use the enabled parameter
241-
onTap: enabled // Only allow tap if enabled
242-
? () async {
243-
final result = await context.pushNamed<dynamic>(
244-
routeName,
245-
extra: currentSelectionData, // Pass the map or list
246-
);
247-
if (result != null && onResult != null) {
248-
onResult(result);
244+
onTap:
245+
enabled // Only allow tap if enabled
246+
? () async {
247+
final result = await context.pushNamed<dynamic>(
248+
routeName,
249+
extra: currentSelectionData, // Pass the map or list
250+
);
251+
if (result != null && onResult != null) {
252+
onResult(result);
253+
}
249254
}
250-
}
251-
: null,
255+
: null,
252256
);
253257
}
254258

@@ -271,8 +275,8 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
271275
tooltip: l10n.headlinesFeedFilterResetButton,
272276
onPressed: () {
273277
context.read<HeadlinesFeedBloc>().add(
274-
HeadlinesFeedFiltersCleared(),
275-
);
278+
HeadlinesFeedFiltersCleared(),
279+
);
276280
// Also reset local state for the checkbox
277281
setState(() {
278282
_useFollowedFilters = false;
@@ -288,12 +292,14 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
288292
tooltip: l10n.headlinesFeedFilterApplyButton,
289293
onPressed: () {
290294
final newFilter = HeadlineFilter(
291-
categories: _tempSelectedCategories.isNotEmpty
292-
? _tempSelectedCategories
293-
: null,
294-
sources: _tempSelectedSources.isNotEmpty
295-
? _tempSelectedSources
296-
: null,
295+
categories:
296+
_tempSelectedCategories.isNotEmpty
297+
? _tempSelectedCategories
298+
: null,
299+
sources:
300+
_tempSelectedSources.isNotEmpty
301+
? _tempSelectedSources
302+
: null,
297303
selectedSourceCountryIsoCodes:
298304
_tempSelectedSourceCountryIsoCodes.isNotEmpty
299305
? _tempSelectedSourceCountryIsoCodes
@@ -302,12 +308,11 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
302308
_tempSelectedSourceSourceTypes.isNotEmpty
303309
? _tempSelectedSourceSourceTypes
304310
: null,
305-
isFromFollowedItems:
306-
_useFollowedFilters, // Set the new flag
311+
isFromFollowedItems: _useFollowedFilters, // Set the new flag
307312
);
308313
context.read<HeadlinesFeedBloc>().add(
309-
HeadlinesFeedFiltersApplied(filter: newFilter),
310-
);
314+
HeadlinesFeedFiltersApplied(filter: newFilter),
315+
);
311316
context.pop();
312317
},
313318
),
@@ -335,13 +340,14 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
335340
}
336341
});
337342
},
338-
secondary: _isLoadingFollowedFilters
339-
? const SizedBox(
340-
width: 24,
341-
height: 24,
342-
child: CircularProgressIndicator(strokeWidth: 2),
343-
)
344-
: null,
343+
secondary:
344+
_isLoadingFollowedFilters
345+
? const SizedBox(
346+
width: 24,
347+
height: 24,
348+
child: CircularProgressIndicator(strokeWidth: 2),
349+
)
350+
: null,
345351
controlAffinity: ListTileControlAffinity.leading,
346352
),
347353
),

0 commit comments

Comments
 (0)