@@ -7,25 +7,20 @@ import 'package:flutter/foundation.dart';
7
7
part 'edit_headline_event.dart' ;
8
8
part 'edit_headline_state.dart' ;
9
9
10
- final class _FetchNextCountryPage extends EditHeadlineEvent {
11
- const _FetchNextCountryPage ();
12
- }
13
-
14
10
/// A BLoC to manage the state of editing a single headline.
15
11
class EditHeadlineBloc extends Bloc <EditHeadlineEvent , EditHeadlineState > {
16
12
/// {@macro edit_headline_bloc}
17
13
EditHeadlineBloc ({
18
14
required DataRepository <Headline > headlinesRepository,
19
15
required DataRepository <Source > sourcesRepository,
20
16
required DataRepository <Topic > topicsRepository,
21
- required DataRepository <Country > countriesRepository ,
17
+ required List <Country > countries ,
22
18
required String headlineId,
23
- }) : _headlinesRepository = headlinesRepository,
24
- _sourcesRepository = sourcesRepository,
25
- _topicsRepository = topicsRepository,
26
- _countriesRepository = countriesRepository,
27
- _headlineId = headlineId,
28
- super (const EditHeadlineState ()) {
19
+ }) : _headlinesRepository = headlinesRepository,
20
+ _sourcesRepository = sourcesRepository,
21
+ _topicsRepository = topicsRepository,
22
+ _headlineId = headlineId,
23
+ super (EditHeadlineState (countries: countries)) {
29
24
on < EditHeadlineLoaded > (_onLoaded);
30
25
on < EditHeadlineTitleChanged > (_onTitleChanged);
31
26
on < EditHeadlineExcerptChanged > (_onExcerptChanged);
@@ -36,13 +31,11 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
36
31
on < EditHeadlineCountryChanged > (_onCountryChanged);
37
32
on < EditHeadlineStatusChanged > (_onStatusChanged);
38
33
on < EditHeadlineSubmitted > (_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 String _headlineId;
47
40
48
41
Future <void > _onLoaded (
@@ -67,10 +60,6 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
67
60
final sources = (responses[1 ] as PaginatedResponse <Source >).items;
68
61
final topics = (responses[2 ] as PaginatedResponse <Topic >).items;
69
62
70
- final countriesResponse = await _countriesRepository.readAll (
71
- sort: [const SortOption ('name' , SortOrder .asc)],
72
- );
73
-
74
63
emit (
75
64
state.copyWith (
76
65
status: EditHeadlineStatus .initial,
@@ -84,18 +73,9 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
84
73
eventCountry: () => headline.eventCountry,
85
74
sources: sources,
86
75
topics: topics,
87
- countries: countriesResponse.items,
88
- countriesCursor: countriesResponse.cursor,
89
- countriesHasMore: countriesResponse.hasMore,
90
76
contentStatus: headline.status,
91
77
),
92
78
);
93
-
94
- // After the initial page of countries is loaded, start a background
95
- // process to fetch all remaining pages.
96
- if (state.countriesHasMore) {
97
- add (const _FetchNextCountryPage ());
98
- }
99
79
} on HttpException catch (e) {
100
80
emit (state.copyWith (status: EditHeadlineStatus .failure, exception: e));
101
81
} catch (e) {
@@ -197,49 +177,6 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
197
177
}
198
178
199
179
// --- Background Data Fetching for Dropdown ---
200
- // The DropdownButtonFormField widget does not natively support on-scroll
201
- // pagination. To preserve UI consistency across the application, this BLoC
202
- // employs an event-driven background fetching mechanism.
203
- //
204
- // After the first page of items is loaded, a chain of events is initiated
205
- // to progressively fetch all remaining pages. This process is throttled
206
- // and runs in the background, ensuring the UI remains responsive while the
207
- // full list of dropdown options is populated over time.
208
- Future <void > _onFetchNextCountryPage (
209
- _FetchNextCountryPage event,
210
- Emitter <EditHeadlineState > emit,
211
- ) async {
212
- if (! state.countriesHasMore || state.countriesIsLoadingMore) return ;
213
-
214
- try {
215
- emit (state.copyWith (countriesIsLoadingMore: true ));
216
-
217
- // ignore: inference_failure_on_instance_creation
218
- await Future .delayed (const Duration (milliseconds: 400 ));
219
-
220
- final nextCountries = await _countriesRepository.readAll (
221
- pagination: PaginationOptions (cursor: state.countriesCursor),
222
- sort: [const SortOption ('name' , SortOrder .asc)],
223
- );
224
-
225
- emit (
226
- state.copyWith (
227
- countries: List .of (state.countries)..addAll (nextCountries.items),
228
- countriesCursor: nextCountries.cursor,
229
- countriesHasMore: nextCountries.hasMore,
230
- countriesIsLoadingMore: false ,
231
- ),
232
- );
233
-
234
- if (nextCountries.hasMore) {
235
- add (const _FetchNextCountryPage ());
236
- }
237
- } catch (e) {
238
- emit (state.copyWith (countriesIsLoadingMore: false ));
239
- // Optionally log the error without disrupting the user
240
- }
241
- }
242
-
243
180
Future <void > _onSubmitted (
244
181
EditHeadlineSubmitted event,
245
182
Emitter <EditHeadlineState > emit,
0 commit comments