@@ -8,10 +8,6 @@ import 'package:uuid/uuid.dart';
88part 'create_headline_event.dart' ;
99part 'create_headline_state.dart' ;
1010
11- final class _FetchNextCountryPage extends CreateHeadlineEvent {
12- const _FetchNextCountryPage ();
13- }
14-
1511/// A BLoC to manage the state of creating a new headline.
1612class CreateHeadlineBloc
1713 extends Bloc <CreateHeadlineEvent , CreateHeadlineState > {
@@ -20,12 +16,11 @@ class CreateHeadlineBloc
2016 required DataRepository <Headline > headlinesRepository,
2117 required DataRepository <Source > sourcesRepository,
2218 required DataRepository <Topic > topicsRepository,
23- required DataRepository <Country > countriesRepository,
24- }) : _headlinesRepository = headlinesRepository,
25- _sourcesRepository = sourcesRepository,
26- _topicsRepository = topicsRepository,
27- _countriesRepository = countriesRepository,
28- super (const CreateHeadlineState ()) {
19+ required List <Country > countries,
20+ }) : _headlinesRepository = headlinesRepository,
21+ _sourcesRepository = sourcesRepository,
22+ _topicsRepository = topicsRepository,
23+ super (CreateHeadlineState (countries: countries)) {
2924 on < CreateHeadlineDataLoaded > (_onDataLoaded);
3025 on < CreateHeadlineTitleChanged > (_onTitleChanged);
3126 on < CreateHeadlineExcerptChanged > (_onExcerptChanged);
@@ -36,13 +31,11 @@ class CreateHeadlineBloc
3631 on < CreateHeadlineCountryChanged > (_onCountryChanged);
3732 on < CreateHeadlineStatusChanged > (_onStatusChanged);
3833 on < CreateHeadlineSubmitted > (_onSubmitted);
39- on < _FetchNextCountryPage > (_onFetchNextCountryPage);
4034 }
4135
4236 final DataRepository <Headline > _headlinesRepository;
4337 final DataRepository <Source > _sourcesRepository;
4438 final DataRepository <Topic > _topicsRepository;
45- final DataRepository <Country > _countriesRepository;
4639 final _uuid = const Uuid ();
4740
4841 Future <void > _onDataLoaded (
@@ -65,26 +58,13 @@ class CreateHeadlineBloc
6558 final sources = (sourcesResponse as PaginatedResponse <Source >).items;
6659 final topics = (topicsResponse as PaginatedResponse <Topic >).items;
6760
68- final countriesResponse = await _countriesRepository.readAll (
69- sort: [const SortOption ('name' , SortOrder .asc)],
70- );
71-
7261 emit (
7362 state.copyWith (
7463 status: CreateHeadlineStatus .initial,
7564 sources: sources,
7665 topics: topics,
77- countries: countriesResponse.items,
78- countriesCursor: countriesResponse.cursor,
79- countriesHasMore: countriesResponse.hasMore,
8066 ),
8167 );
82-
83- // After the initial page of countries is loaded, start a background
84- // process to fetch all remaining pages.
85- if (state.countriesHasMore) {
86- add (const _FetchNextCountryPage ());
87- }
8868 } on HttpException catch (e) {
8969 emit (state.copyWith (status: CreateHeadlineStatus .failure, exception: e));
9070 } catch (e) {
@@ -159,49 +139,6 @@ class CreateHeadlineBloc
159139 }
160140
161141 // --- Background Data Fetching for Dropdown ---
162- // The DropdownButtonFormField widget does not natively support on-scroll
163- // pagination. To preserve UI consistency across the application, this BLoC
164- // employs an event-driven background fetching mechanism.
165- //
166- // After the first page of items is loaded, a chain of events is initiated
167- // to progressively fetch all remaining pages. This process is throttled
168- // and runs in the background, ensuring the UI remains responsive while the
169- // full list of dropdown options is populated over time.
170- Future <void > _onFetchNextCountryPage (
171- _FetchNextCountryPage event,
172- Emitter <CreateHeadlineState > emit,
173- ) async {
174- if (! state.countriesHasMore || state.countriesIsLoadingMore) return ;
175-
176- try {
177- emit (state.copyWith (countriesIsLoadingMore: true ));
178-
179- // ignore: inference_failure_on_instance_creation
180- await Future .delayed (const Duration (milliseconds: 400 ));
181-
182- final nextCountries = await _countriesRepository.readAll (
183- pagination: PaginationOptions (cursor: state.countriesCursor),
184- sort: [const SortOption ('name' , SortOrder .asc)],
185- );
186-
187- emit (
188- state.copyWith (
189- countries: List .of (state.countries)..addAll (nextCountries.items),
190- countriesCursor: nextCountries.cursor,
191- countriesHasMore: nextCountries.hasMore,
192- countriesIsLoadingMore: false ,
193- ),
194- );
195-
196- if (nextCountries.hasMore) {
197- add (const _FetchNextCountryPage ());
198- }
199- } catch (e) {
200- emit (state.copyWith (countriesIsLoadingMore: false ));
201- // Optionally log the error without disrupting the user
202- }
203- }
204-
205142 Future <void > _onSubmitted (
206143 CreateHeadlineSubmitted event,
207144 Emitter <CreateHeadlineState > emit,
0 commit comments