Skip to content

Commit b81c083

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 while loads are in progress
1 parent bc9c8a8 commit b81c083

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

lib/content_management/bloc/edit_source/edit_source_bloc.dart

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
275275
EditSourceLoadMoreCountriesRequested event,
276276
Emitter<EditSourceState> emit,
277277
) async {
278-
if (!state.countriesHasMore) return;
278+
if (!state.countriesHasMore || state.countriesIsLoadingMore) return;
279+
280+
emit(state.copyWith(countriesIsLoadingMore: true));
279281

280282
try {
281283
final countriesResponse = await _countriesRepository.readAll(
@@ -293,15 +295,23 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
293295
countries: List.of(state.countries)..addAll(countriesResponse.items),
294296
countriesCursor: countriesResponse.cursor,
295297
countriesHasMore: countriesResponse.hasMore,
298+
countriesIsLoadingMore: false,
296299
),
297300
);
298301
} on HttpException catch (e) {
299-
emit(state.copyWith(status: EditSourceStatus.failure, exception: e));
302+
emit(
303+
state.copyWith(
304+
status: EditSourceStatus.failure,
305+
exception: e,
306+
countriesIsLoadingMore: false,
307+
),
308+
);
300309
} catch (e) {
301310
emit(
302311
state.copyWith(
303312
status: EditSourceStatus.failure,
304313
exception: UnknownException('An unexpected error occurred: $e'),
314+
countriesIsLoadingMore: false,
305315
),
306316
);
307317
}
@@ -342,7 +352,9 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
342352
EditSourceLoadMoreLanguagesRequested event,
343353
Emitter<EditSourceState> emit,
344354
) async {
345-
if (!state.languagesHasMore) return;
355+
if (!state.languagesHasMore || state.languagesIsLoadingMore) return;
356+
357+
emit(state.copyWith(languagesIsLoadingMore: true));
346358

347359
try {
348360
final languagesResponse = await _languagesRepository.readAll(
@@ -360,15 +372,23 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
360372
languages: List.of(state.languages)..addAll(languagesResponse.items),
361373
languagesCursor: languagesResponse.cursor,
362374
languagesHasMore: languagesResponse.hasMore,
375+
languagesIsLoadingMore: false,
363376
),
364377
);
365378
} on HttpException catch (e) {
366-
emit(state.copyWith(status: EditSourceStatus.failure, exception: e));
379+
emit(
380+
state.copyWith(
381+
status: EditSourceStatus.failure,
382+
exception: e,
383+
languagesIsLoadingMore: false,
384+
),
385+
);
367386
} catch (e) {
368387
emit(
369388
state.copyWith(
370389
status: EditSourceStatus.failure,
371390
exception: UnknownException('An unexpected error occurred: $e'),
391+
languagesIsLoadingMore: false,
372392
),
373393
);
374394
}

0 commit comments

Comments
 (0)