@@ -8,10 +8,6 @@ import 'package:uuid/uuid.dart';
8
8
part 'create_headline_event.dart' ;
9
9
part 'create_headline_state.dart' ;
10
10
11
- final class _FetchNextCountryPage extends CreateHeadlineEvent {
12
- const _FetchNextCountryPage ();
13
- }
14
-
15
11
/// A BLoC to manage the state of creating a new headline.
16
12
class CreateHeadlineBloc
17
13
extends Bloc <CreateHeadlineEvent , CreateHeadlineState > {
@@ -20,12 +16,11 @@ class CreateHeadlineBloc
20
16
required DataRepository <Headline > headlinesRepository,
21
17
required DataRepository <Source > sourcesRepository,
22
18
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)) {
29
24
on < CreateHeadlineDataLoaded > (_onDataLoaded);
30
25
on < CreateHeadlineTitleChanged > (_onTitleChanged);
31
26
on < CreateHeadlineExcerptChanged > (_onExcerptChanged);
@@ -36,13 +31,11 @@ class CreateHeadlineBloc
36
31
on < CreateHeadlineCountryChanged > (_onCountryChanged);
37
32
on < CreateHeadlineStatusChanged > (_onStatusChanged);
38
33
on < CreateHeadlineSubmitted > (_onSubmitted);
39
- on < _FetchNextCountryPage > (_onFetchNextCountryPage);
40
34
}
41
35
42
36
final DataRepository <Headline > _headlinesRepository;
43
37
final DataRepository <Source > _sourcesRepository;
44
38
final DataRepository <Topic > _topicsRepository;
45
- final DataRepository <Country > _countriesRepository;
46
39
final _uuid = const Uuid ();
47
40
48
41
Future <void > _onDataLoaded (
@@ -65,26 +58,13 @@ class CreateHeadlineBloc
65
58
final sources = (sourcesResponse as PaginatedResponse <Source >).items;
66
59
final topics = (topicsResponse as PaginatedResponse <Topic >).items;
67
60
68
- final countriesResponse = await _countriesRepository.readAll (
69
- sort: [const SortOption ('name' , SortOrder .asc)],
70
- );
71
-
72
61
emit (
73
62
state.copyWith (
74
63
status: CreateHeadlineStatus .initial,
75
64
sources: sources,
76
65
topics: topics,
77
- countries: countriesResponse.items,
78
- countriesCursor: countriesResponse.cursor,
79
- countriesHasMore: countriesResponse.hasMore,
80
66
),
81
67
);
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
- }
88
68
} on HttpException catch (e) {
89
69
emit (state.copyWith (status: CreateHeadlineStatus .failure, exception: e));
90
70
} catch (e) {
@@ -159,49 +139,6 @@ class CreateHeadlineBloc
159
139
}
160
140
161
141
// --- 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
-
205
142
Future <void > _onSubmitted (
206
143
CreateHeadlineSubmitted event,
207
144
Emitter <CreateHeadlineState > emit,
0 commit comments