Skip to content

Commit 1c5ac59

Browse files
committed
docs: clarify category filter page state mgmt
- Explain local selection state - Clarify init from GoRouterState - Detail apply button pop action
1 parent 2aa0462 commit 1c5ac59

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

lib/headlines-feed/view/category_filter_page.dart

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ class CategoryFilterPage extends StatefulWidget {
3030
/// Manages the local selection state ([_pageSelectedCategories]) and interacts
3131
/// with [CategoriesFilterBloc] for data fetching and pagination.
3232
class _CategoryFilterPageState extends State<CategoryFilterPage> {
33-
/// Stores the categories selected by the user on this page.
34-
/// Initialized from the `extra` parameter passed during navigation.
33+
/// Stores the categories selected by the user *on this specific page*.
34+
/// This state is local to the `CategoryFilterPage` lifecycle.
35+
/// It's initialized in `initState` using the list of previously selected
36+
/// categories passed via the `extra` parameter during navigation from
37+
/// `HeadlinesFilterPage`. This ensures the checkboxes reflect the state
38+
/// from the main filter page when this page loads.
3539
late Set<Category> _pageSelectedCategories;
3640

3741
/// Scroll controller to detect when the user reaches the end of the list
@@ -41,15 +45,28 @@ class _CategoryFilterPageState extends State<CategoryFilterPage> {
4145
@override
4246
void initState() {
4347
super.initState();
44-
// Initialize local selections from the data passed via 'extra'
45-
// Need to access GoRouterState *after* the first frame
48+
// Initialization needs to happen after the first frame to safely access
49+
// GoRouterState.of(context).
4650
WidgetsBinding.instance.addPostFrameCallback((_) {
51+
// 1. Retrieve the list of categories that were already selected on the
52+
// previous page (HeadlinesFilterPage). This list is passed dynamically
53+
// via the `extra` parameter in the `context.pushNamed` call.
4754
final initialSelection =
4855
GoRouterState.of(context).extra as List<Category>?;
56+
57+
// 2. Initialize the local selection state (`_pageSelectedCategories`) for this
58+
// page. Use a Set for efficient add/remove/contains operations.
59+
// This ensures the checkboxes on this page are initially checked
60+
// correctly based on the selections made previously.
4961
_pageSelectedCategories = Set.from(initialSelection ?? []);
50-
// Request initial categories from the BLoC
62+
63+
// 3. Trigger the page-specific BLoC (CategoriesFilterBloc) to start
64+
// fetching the list of *all available* categories that the user can
65+
// potentially select from. The BLoC handles fetching, pagination,
66+
// loading states, and errors for the *list of options*.
5167
context.read<CategoriesFilterBloc>().add(CategoriesFilterRequested());
5268
});
69+
// Add listener for pagination logic.
5370
_scrollController.addListener(_onScroll);
5471
}
5572

@@ -92,7 +109,10 @@ class _CategoryFilterPageState extends State<CategoryFilterPage> {
92109
icon: const Icon(Icons.check),
93110
tooltip: l10n.headlinesFeedFilterApplyButton,
94111
onPressed: () {
95-
// Pop with the final selection from this page
112+
// When the user taps 'Apply' (checkmark), pop the current route
113+
// and return the final list of selected categories (`_pageSelectedCategories`)
114+
// from this page back to the previous page (`HeadlinesFilterPage`).
115+
// `HeadlinesFilterPage` receives this list in its `onResult` callback.
96116
context.pop(_pageSelectedCategories.toList());
97117
},
98118
),
@@ -205,10 +225,14 @@ class _CategoryFilterPageState extends State<CategoryFilterPage> {
205225
: null,
206226
value: isSelected,
207227
onChanged: (bool? value) {
228+
// When a checkbox state changes, update the local selection set
229+
// (`_pageSelectedCategories`) for this page.
208230
setState(() {
209231
if (value == true) {
232+
// Add the category if checked.
210233
_pageSelectedCategories.add(category);
211234
} else {
235+
// Remove the category if unchecked.
212236
_pageSelectedCategories.remove(category);
213237
}
214238
});

0 commit comments

Comments
 (0)