Skip to content

Commit c1dc563

Browse files
committed
refactor(content_management): fetch all countries in the background
- Remove EditHeadlineLoadMoreCountriesRequested event and related logic - Implement background fetching for all countries in _onHeadlineChanged - Update state with loaded countries
1 parent 5a042de commit c1dc563

File tree

1 file changed

+15
-46
lines changed

1 file changed

+15
-46
lines changed

lib/content_management/bloc/edit_headline/edit_headline_bloc.dart

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
3535
on<EditHeadlineCountryChanged>(_onCountryChanged);
3636
on<EditHeadlineStatusChanged>(_onStatusChanged);
3737
on<EditHeadlineSubmitted>(_onSubmitted);
38-
on<EditHeadlineLoadMoreCountriesRequested>(
39-
_onLoadMoreCountriesRequested,
40-
);
4138
}
4239

4340
final DataRepository<Headline> _headlinesRepository;
@@ -89,6 +86,21 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
8986
contentStatus: headline.status,
9087
),
9188
);
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+
}
92104
} on HttpException catch (e) {
93105
emit(state.copyWith(status: EditHeadlineStatus.failure, exception: e));
94106
} catch (e) {
@@ -240,47 +252,4 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
240252
);
241253
}
242254
}
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-
}
286255
}

0 commit comments

Comments
 (0)