@@ -35,9 +35,6 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
35
35
on < EditHeadlineCountryChanged > (_onCountryChanged);
36
36
on < EditHeadlineStatusChanged > (_onStatusChanged);
37
37
on < EditHeadlineSubmitted > (_onSubmitted);
38
- on < EditHeadlineLoadMoreCountriesRequested > (
39
- _onLoadMoreCountriesRequested,
40
- );
41
38
}
42
39
43
40
final DataRepository <Headline > _headlinesRepository;
@@ -89,6 +86,21 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
89
86
contentStatus: headline.status,
90
87
),
91
88
);
89
+
90
+ // Start background fetching for all countries
91
+ while (state.countriesHasMore) {
92
+ final nextCountries = await _countriesRepository.readAll (
93
+ pagination: PaginationOptions (cursor: state.countriesCursor),
94
+ sort: [const SortOption ('name' , SortOrder .asc)],
95
+ );
96
+ emit (
97
+ state.copyWith (
98
+ countries: List .of (state.countries)..addAll (nextCountries.items),
99
+ countriesCursor: nextCountries.cursor,
100
+ countriesHasMore: nextCountries.hasMore,
101
+ ),
102
+ );
103
+ }
92
104
} on HttpException catch (e) {
93
105
emit (state.copyWith (status: EditHeadlineStatus .failure, exception: e));
94
106
} catch (e) {
@@ -240,47 +252,4 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
240
252
);
241
253
}
242
254
}
243
-
244
- Future <void > _onLoadMoreCountriesRequested (
245
- EditHeadlineLoadMoreCountriesRequested event,
246
- Emitter <EditHeadlineState > emit,
247
- ) async {
248
- if (! state.countriesHasMore || state.countriesIsLoadingMore) return ;
249
-
250
- emit (state.copyWith (countriesIsLoadingMore: true ));
251
-
252
- try {
253
- final countriesResponse = await _countriesRepository.readAll (
254
- pagination: state.countriesCursor != null
255
- ? PaginationOptions (cursor: state.countriesCursor)
256
- : null ,
257
- sort: [const SortOption ('name' , SortOrder .asc)],
258
- );
259
-
260
- emit (
261
- state.copyWith (
262
- countries: List .of (state.countries)..addAll (countriesResponse.items),
263
- countriesCursor: countriesResponse.cursor,
264
- countriesHasMore: countriesResponse.hasMore,
265
- countriesIsLoadingMore: false ,
266
- ),
267
- );
268
- } on HttpException catch (e) {
269
- emit (
270
- state.copyWith (
271
- status: EditHeadlineStatus .failure,
272
- exception: e,
273
- countriesIsLoadingMore: false ,
274
- ),
275
- );
276
- } catch (e) {
277
- emit (
278
- state.copyWith (
279
- status: EditHeadlineStatus .failure,
280
- exception: UnknownException ('An unexpected error occurred: $e ' ),
281
- countriesIsLoadingMore: false ,
282
- ),
283
- );
284
- }
285
- }
286
255
}
0 commit comments