@@ -35,7 +35,6 @@ class CreateHeadlineBloc
3535 on < CreateHeadlineCountryChanged > (_onCountryChanged);
3636 on < CreateHeadlineStatusChanged > (_onStatusChanged);
3737 on < CreateHeadlineSubmitted > (_onSubmitted);
38- on < CreateHeadlineLoadMoreCountriesRequested > (_onLoadMoreCountriesRequested);
3938 }
4039
4140 final DataRepository <Headline > _headlinesRepository;
@@ -76,6 +75,21 @@ class CreateHeadlineBloc
7675 countriesHasMore: countriesResponse.hasMore,
7776 ),
7877 );
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+ }
7993 } on HttpException catch (e) {
8094 emit (state.copyWith (status: CreateHeadlineStatus .failure, exception: e));
8195 } catch (e) {
@@ -190,47 +204,4 @@ class CreateHeadlineBloc
190204 );
191205 }
192206 }
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- }
236207}
0 commit comments