Skip to content

Commit e665de4

Browse files
committed
feat(filter): support complex source filters
- Pass map to source filter - Handle map result from source filter
1 parent 617a61b commit e665de4

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

lib/headlines-feed/view/headlines_filter_page.dart

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// ignore_for_file: lines_longer_than_80_chars
2+
// ignore_for_file: lines_longer_than_80_chars, public_member_api_docs
33

44
import 'package:flutter/material.dart';
55
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -10,7 +10,12 @@ import 'package:ht_main/l10n/l10n.dart';
1010
import 'package:ht_main/router/routes.dart';
1111
import 'package:ht_main/shared/constants/constants.dart';
1212
import 'package:ht_shared/ht_shared.dart'
13-
show Category, Source; // Import models from ht_shared, Country removed
13+
show Category, Source, SourceType;
14+
15+
// Keys for passing data to/from SourceFilterPage
16+
const String keySelectedSources = 'selectedSources';
17+
const String keySelectedCountryIsoCodes = 'selectedCountryIsoCodes';
18+
const String keySelectedSourceTypes = 'selectedSourceTypes';
1419

1520
/// {@template headlines_filter_page}
1621
/// A full-screen dialog page for selecting headline filters.
@@ -35,7 +40,9 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
3540
/// and are only applied back to the BLoC when the user taps 'Apply'.
3641
late List<Category> _tempSelectedCategories;
3742
late List<Source> _tempSelectedSources;
38-
// late List<Country> _tempSelectedCountries; // Removed
43+
// State for source filter capsules, to be passed to and from SourceFilterPage
44+
late Set<String> _tempSelectedSourceCountryIsoCodes;
45+
late Set<SourceType> _tempSelectedSourceSourceTypes;
3946

4047
@override
4148
void initState() {
@@ -48,15 +55,18 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
4855
// Create copies of the lists to avoid modifying the BLoC state directly.
4956
_tempSelectedCategories = List.from(currentState.filter.categories ?? []);
5057
_tempSelectedSources = List.from(currentState.filter.sources ?? []);
51-
// _tempSelectedCountries = List.from(
52-
// currentState.filter.eventCountries ?? [],
53-
// ); // Removed
58+
// Initialize source capsule states. These are persisted here.
59+
// For now, let's assume they start empty and are populated by SourceFilterPage's result.
60+
// Or, if HeadlineFilter could store them, we'd get them from there.
61+
// For this iteration, they start empty. SourceFilterPage will manage its own capsule
62+
// state and return it.
63+
_tempSelectedSourceCountryIsoCodes = {};
64+
_tempSelectedSourceSourceTypes = {};
5465
} else {
55-
// Default to empty lists if the feed isn't loaded yet. This might happen
56-
// if the filter page is accessed before the initial feed load completes.
5766
_tempSelectedCategories = [];
5867
_tempSelectedSources = [];
59-
// _tempSelectedCountries = []; // Removed
68+
_tempSelectedSourceCountryIsoCodes = {};
69+
_tempSelectedSourceSourceTypes = {};
6070
}
6171
}
6272

@@ -76,8 +86,9 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
7686
required String title,
7787
required int selectedCount,
7888
required String routeName,
79-
required List<dynamic> currentSelection, // Pass current temp selection
80-
required void Function(List<dynamic>?) onResult,
89+
// For sources, currentSelection will be a Map
90+
required dynamic currentSelectionData,
91+
required void Function(dynamic)? onResult, // Result can also be a Map
8192
}) {
8293
final l10n = context.l10n;
8394
final allLabel = l10n.headlinesFeedFilterAllLabel;
@@ -92,21 +103,12 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
92103
subtitle: Text(subtitle),
93104
trailing: const Icon(Icons.chevron_right),
94105
onTap: () async {
95-
// Navigate to the specific filter selection page (e.g., SourceFilterPage).
96-
// Use pushNamed to wait for a result when the page is popped.
97-
final result = await context.pushNamed<List<dynamic>>(
98-
routeName, // The route name for the specific filter page.
99-
// Pass the current temporary selection for this filter type
100-
// (e.g., _tempSelectedSources) to the next page. This allows
101-
// the next page to initialize its UI reflecting the current state.
102-
extra: currentSelection,
106+
final result = await context.pushNamed<dynamic>(
107+
routeName,
108+
extra: currentSelectionData, // Pass the map or list
103109
);
104-
// When the filter selection page pops (usually via its 'Apply' button),
105-
// it returns the potentially modified list of selected items.
106-
// If the result is not null (meaning the user didn't just cancel/go back),
107-
// update the corresponding temporary state list on *this* page.
108-
if (result != null) {
109-
onResult(result); // Calls setState to update the UI here.
110+
if (result != null && onResult != null) {
111+
onResult(result);
110112
}
111113
},
112114
);
@@ -175,7 +177,7 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
175177
title: l10n.headlinesFeedFilterCategoryLabel,
176178
selectedCount: _tempSelectedCategories.length,
177179
routeName: Routes.feedFilterCategoriesName,
178-
currentSelection: _tempSelectedCategories,
180+
currentSelectionData: _tempSelectedCategories,
179181
onResult: (result) {
180182
if (result is List<Category>) {
181183
setState(() => _tempSelectedCategories = result);
@@ -187,10 +189,21 @@ class _HeadlinesFilterPageState extends State<HeadlinesFilterPage> {
187189
title: l10n.headlinesFeedFilterSourceLabel,
188190
selectedCount: _tempSelectedSources.length,
189191
routeName: Routes.feedFilterSourcesName,
190-
currentSelection: _tempSelectedSources,
192+
currentSelectionData: {
193+
keySelectedSources: _tempSelectedSources,
194+
keySelectedCountryIsoCodes: _tempSelectedSourceCountryIsoCodes,
195+
keySelectedSourceTypes: _tempSelectedSourceSourceTypes,
196+
},
191197
onResult: (result) {
192-
if (result is List<Source>) {
193-
setState(() => _tempSelectedSources = result);
198+
if (result is Map<String, dynamic>) {
199+
setState(() {
200+
_tempSelectedSources =
201+
result[keySelectedSources] as List<Source>? ?? [];
202+
_tempSelectedSourceCountryIsoCodes =
203+
result[keySelectedCountryIsoCodes] as Set<String>? ?? {};
204+
_tempSelectedSourceSourceTypes =
205+
result[keySelectedSourceTypes] as Set<SourceType>? ?? {};
206+
});
194207
}
195208
},
196209
),

0 commit comments

Comments
 (0)