Skip to content

Commit bc9c8a8

Browse files
committed
fix(content_management): prevent multiple concurrent loads for countries and languages
- Add loading flags for countries and languages - Check loading flags before initiating new load requests - Update loading flags appropriately during and after API calls - Ensure proper UI feedback during loading states
1 parent f1d1eeb commit bc9c8a8

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

lib/content_management/bloc/create_source/create_source_bloc.dart

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
221221
CreateSourceLoadMoreCountriesRequested event,
222222
Emitter<CreateSourceState> emit,
223223
) async {
224-
if (!state.countriesHasMore) return;
224+
if (!state.countriesHasMore || state.countriesIsLoadingMore) return;
225+
226+
emit(state.copyWith(countriesIsLoadingMore: true));
225227

226228
try {
227229
final countriesResponse = await _countriesRepository.readAll(
@@ -239,15 +241,23 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
239241
countries: List.of(state.countries)..addAll(countriesResponse.items),
240242
countriesCursor: countriesResponse.cursor,
241243
countriesHasMore: countriesResponse.hasMore,
244+
countriesIsLoadingMore: false,
242245
),
243246
);
244247
} on HttpException catch (e) {
245-
emit(state.copyWith(status: CreateSourceStatus.failure, exception: e));
248+
emit(
249+
state.copyWith(
250+
status: CreateSourceStatus.failure,
251+
exception: e,
252+
countriesIsLoadingMore: false,
253+
),
254+
);
246255
} catch (e) {
247256
emit(
248257
state.copyWith(
249258
status: CreateSourceStatus.failure,
250259
exception: UnknownException('An unexpected error occurred: $e'),
260+
countriesIsLoadingMore: false,
251261
),
252262
);
253263
}
@@ -288,7 +298,9 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
288298
CreateSourceLoadMoreLanguagesRequested event,
289299
Emitter<CreateSourceState> emit,
290300
) async {
291-
if (!state.languagesHasMore) return;
301+
if (!state.languagesHasMore || state.languagesIsLoadingMore) return;
302+
303+
emit(state.copyWith(languagesIsLoadingMore: true));
292304

293305
try {
294306
final languagesResponse = await _languagesRepository.readAll(
@@ -306,15 +318,23 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
306318
languages: List.of(state.languages)..addAll(languagesResponse.items),
307319
languagesCursor: languagesResponse.cursor,
308320
languagesHasMore: languagesResponse.hasMore,
321+
languagesIsLoadingMore: false,
309322
),
310323
);
311324
} on HttpException catch (e) {
312-
emit(state.copyWith(status: CreateSourceStatus.failure, exception: e));
325+
emit(
326+
state.copyWith(
327+
status: CreateSourceStatus.failure,
328+
exception: e,
329+
languagesIsLoadingMore: false,
330+
),
331+
);
313332
} catch (e) {
314333
emit(
315334
state.copyWith(
316335
status: CreateSourceStatus.failure,
317336
exception: UnknownException('An unexpected error occurred: $e'),
337+
languagesIsLoadingMore: false,
318338
),
319339
);
320340
}

0 commit comments

Comments
 (0)