@@ -35,7 +35,6 @@ class CreateHeadlineBloc
35
35
on < CreateHeadlineCountryChanged > (_onCountryChanged);
36
36
on < CreateHeadlineStatusChanged > (_onStatusChanged);
37
37
on < CreateHeadlineSubmitted > (_onSubmitted);
38
- on < CreateHeadlineLoadMoreCountriesRequested > (_onLoadMoreCountriesRequested);
39
38
}
40
39
41
40
final DataRepository <Headline > _headlinesRepository;
@@ -76,6 +75,21 @@ class CreateHeadlineBloc
76
75
countriesHasMore: countriesResponse.hasMore,
77
76
),
78
77
);
78
+
79
+ // Start background fetching for all countries
80
+ while (state.countriesHasMore) {
81
+ final nextCountries = await _countriesRepository.readAll (
82
+ pagination: PaginationOptions (cursor: state.countriesCursor),
83
+ sort: [const SortOption ('name' , SortOrder .asc)],
84
+ );
85
+ emit (
86
+ state.copyWith (
87
+ countries: List .of (state.countries)..addAll (nextCountries.items),
88
+ countriesCursor: nextCountries.cursor,
89
+ countriesHasMore: nextCountries.hasMore,
90
+ ),
91
+ );
92
+ }
79
93
} on HttpException catch (e) {
80
94
emit (state.copyWith (status: CreateHeadlineStatus .failure, exception: e));
81
95
} catch (e) {
@@ -190,47 +204,4 @@ class CreateHeadlineBloc
190
204
);
191
205
}
192
206
}
193
-
194
- Future <void > _onLoadMoreCountriesRequested (
195
- CreateHeadlineLoadMoreCountriesRequested event,
196
- Emitter <CreateHeadlineState > emit,
197
- ) async {
198
- if (! state.countriesHasMore || state.countriesIsLoadingMore) return ;
199
-
200
- emit (state.copyWith (countriesIsLoadingMore: true ));
201
-
202
- try {
203
- final countriesResponse = await _countriesRepository.readAll (
204
- pagination: state.countriesCursor != null
205
- ? PaginationOptions (cursor: state.countriesCursor)
206
- : null ,
207
- sort: [const SortOption ('name' , SortOrder .asc)],
208
- );
209
-
210
- emit (
211
- state.copyWith (
212
- countries: List .of (state.countries)..addAll (countriesResponse.items),
213
- countriesCursor: countriesResponse.cursor,
214
- countriesHasMore: countriesResponse.hasMore,
215
- countriesIsLoadingMore: false ,
216
- ),
217
- );
218
- } on HttpException catch (e) {
219
- emit (
220
- state.copyWith (
221
- status: CreateHeadlineStatus .failure,
222
- exception: e,
223
- countriesIsLoadingMore: false ,
224
- ),
225
- );
226
- } catch (e) {
227
- emit (
228
- state.copyWith (
229
- status: CreateHeadlineStatus .failure,
230
- exception: UnknownException ('An unexpected error occurred: $e ' ),
231
- countriesIsLoadingMore: false ,
232
- ),
233
- );
234
- }
235
- }
236
207
}
0 commit comments