Skip to content

Commit 2aa0462

Browse files
committed
docs: clarify source filter page logic
- Explain source initialization - Describe checkbox behavior - Clarify apply button logic
1 parent 3c9d079 commit 2aa0462

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

lib/headlines-feed/view/source_filter_page.dart

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ class SourceFilterPage extends StatefulWidget {
3030
/// Manages the local selection state ([_pageSelectedSources]) and interacts
3131
/// with [SourcesFilterBloc] for data fetching and pagination.
3232
class _SourceFilterPageState extends State<SourceFilterPage> {
33-
/// Stores the sources selected by the user on this page.
34-
/// Initialized from the `extra` parameter passed during navigation.
33+
/// Stores the sources selected by the user *on this specific page*.
34+
/// This state is local to the `SourceFilterPage` lifecycle.
35+
/// It's initialized in `initState` using the list of previously selected
36+
/// sources 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<Source> _pageSelectedSources;
3640

3741
/// Scroll controller to detect when the user reaches the end of the list
@@ -41,13 +45,27 @@ class _SourceFilterPageState extends State<SourceFilterPage> {
4145
@override
4246
void initState() {
4347
super.initState();
44-
// Initialize local selections from the data passed via 'extra'
48+
// Initialization needs to happen after the first frame to safely access
49+
// GoRouterState.of(context).
4550
WidgetsBinding.instance.addPostFrameCallback((_) {
51+
// 1. Retrieve the list of sources 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.
4654
final initialSelection = GoRouterState.of(context).extra as List<Source>?;
55+
56+
// 2. Initialize the local selection state (`_pageSelectedSources`) for this
57+
// page. Use a Set for efficient add/remove/contains operations.
58+
// This ensures the checkboxes on this page are initially checked
59+
// correctly based on the selections made previously.
4760
_pageSelectedSources = Set.from(initialSelection ?? []);
48-
// Request initial sources from the BLoC
61+
62+
// 3. Trigger the page-specific BLoC (SourcesFilterBloc) to start
63+
// fetching the list of *all available* sources that the user can
64+
// potentially select from. The BLoC handles fetching, pagination,
65+
// loading states, and errors for the *list of options*.
4966
context.read<SourcesFilterBloc>().add(SourcesFilterRequested());
5067
});
68+
// Add listener for pagination logic.
5169
_scrollController.addListener(_onScroll);
5270
}
5371

@@ -88,6 +106,10 @@ class _SourceFilterPageState extends State<SourceFilterPage> {
88106
icon: const Icon(Icons.check),
89107
tooltip: l10n.headlinesFeedFilterApplyButton,
90108
onPressed: () {
109+
// When the user taps 'Apply' (checkmark), pop the current route
110+
// and return the final list of selected sources (`_pageSelectedSources`)
111+
// from this page back to the previous page (`HeadlinesFilterPage`).
112+
// `HeadlinesFilterPage` receives this list in its `onResult` callback.
91113
context.pop(_pageSelectedSources.toList());
92114
},
93115
),
@@ -185,10 +207,14 @@ class _SourceFilterPageState extends State<SourceFilterPage> {
185207
: null,
186208
value: isSelected,
187209
onChanged: (bool? value) {
210+
// When a checkbox state changes, update the local selection set
211+
// (`_pageSelectedSources`) for this page.
188212
setState(() {
189213
if (value == true) {
214+
// Add the source if checked.
190215
_pageSelectedSources.add(source);
191216
} else {
217+
// Remove the source if unchecked.
192218
_pageSelectedSources.remove(source);
193219
}
194220
});

0 commit comments

Comments
 (0)