Skip to content

Enhance content management dropdown fetching performance #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class CreateHeadlineBloc
required DataRepository<Source> sourcesRepository,
required DataRepository<Topic> topicsRepository,
required DataRepository<Country> countriesRepository,
}) : _headlinesRepository = headlinesRepository,
_sourcesRepository = sourcesRepository,
_topicsRepository = topicsRepository,
_countriesRepository = countriesRepository,
super(const CreateHeadlineState()) {
}) : _headlinesRepository = headlinesRepository,
_sourcesRepository = sourcesRepository,
_topicsRepository = topicsRepository,
_countriesRepository = countriesRepository,
super(const CreateHeadlineState()) {
on<CreateHeadlineDataLoaded>(_onDataLoaded);
on<CreateHeadlineTitleChanged>(_onTitleChanged);
on<CreateHeadlineExcerptChanged>(_onExcerptChanged);
Expand All @@ -37,10 +37,9 @@ class CreateHeadlineBloc
on<CreateHeadlineSubmitted>(_onSubmitted);
on<CreateHeadlineCountrySearchChanged>(
_onCountrySearchChanged,
transformer: restartable(),
transformer: droppable(),
);
on<CreateHeadlineLoadMoreCountriesRequested>(
_onLoadMoreCountriesRequested);
on<CreateHeadlineLoadMoreCountriesRequested>(_onLoadMoreCountriesRequested);
}

final DataRepository<Headline> _headlinesRepository;
Expand Down Expand Up @@ -204,8 +203,7 @@ class CreateHeadlineBloc
emit(state.copyWith(countrySearchTerm: event.searchTerm));
try {
final countriesResponse = await _countriesRepository.readAll(
filter:
event.searchTerm.isNotEmpty ? {'name': event.searchTerm} : null,
filter: event.searchTerm.isNotEmpty ? {'name': event.searchTerm} : null,
sort: [const SortOption('name', SortOrder.asc)],
);

Expand All @@ -232,7 +230,9 @@ class CreateHeadlineBloc
CreateHeadlineLoadMoreCountriesRequested event,
Emitter<CreateHeadlineState> emit,
) async {
if (!state.countriesHasMore) return;
if (!state.countriesHasMore || state.countriesIsLoadingMore) return;

emit(state.copyWith(countriesIsLoadingMore: true));

try {
final countriesResponse = await _countriesRepository.readAll(
Expand All @@ -250,15 +250,23 @@ class CreateHeadlineBloc
countries: List.of(state.countries)..addAll(countriesResponse.items),
countriesCursor: countriesResponse.cursor,
countriesHasMore: countriesResponse.hasMore,
countriesIsLoadingMore: false,
),
);
} on HttpException catch (e) {
emit(state.copyWith(status: CreateHeadlineStatus.failure, exception: e));
emit(
state.copyWith(
status: CreateHeadlineStatus.failure,
exception: e,
countriesIsLoadingMore: false,
),
);
} catch (e) {
emit(
state.copyWith(
status: CreateHeadlineStatus.failure,
exception: UnknownException('An unexpected error occurred: $e'),
countriesIsLoadingMore: false,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ final class CreateHeadlineState extends Equatable {
this.topics = const [],
this.countries = const [],
this.countriesHasMore = true,
this.countriesIsLoadingMore = false,
this.countriesCursor,
this.countrySearchTerm = '',
this.contentStatus = ContentStatus.active,
Expand All @@ -52,6 +53,7 @@ final class CreateHeadlineState extends Equatable {
final List<Topic> topics;
final List<Country> countries;
final bool countriesHasMore;
final bool countriesIsLoadingMore;
final String? countriesCursor;
final String countrySearchTerm;
final ContentStatus contentStatus;
Expand Down Expand Up @@ -81,6 +83,7 @@ final class CreateHeadlineState extends Equatable {
List<Topic>? topics,
List<Country>? countries,
bool? countriesHasMore,
bool? countriesIsLoadingMore,
String? countriesCursor,
String? countrySearchTerm,
ContentStatus? contentStatus,
Expand All @@ -100,6 +103,8 @@ final class CreateHeadlineState extends Equatable {
topics: topics ?? this.topics,
countries: countries ?? this.countries,
countriesHasMore: countriesHasMore ?? this.countriesHasMore,
countriesIsLoadingMore:
countriesIsLoadingMore ?? this.countriesIsLoadingMore,
countriesCursor: countriesCursor ?? this.countriesCursor,
countrySearchTerm: countrySearchTerm ?? this.countrySearchTerm,
contentStatus: contentStatus ?? this.contentStatus,
Expand All @@ -122,6 +127,7 @@ final class CreateHeadlineState extends Equatable {
topics,
countries,
countriesHasMore,
countriesIsLoadingMore,
countriesCursor,
countrySearchTerm,
contentStatus,
Expand Down
49 changes: 33 additions & 16 deletions lib/content_management/bloc/create_source/create_source_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
required DataRepository<Source> sourcesRepository,
required DataRepository<Country> countriesRepository,
required DataRepository<Language> languagesRepository,
}) : _sourcesRepository = sourcesRepository,
_countriesRepository = countriesRepository,
_languagesRepository = languagesRepository,
super(const CreateSourceState()) {
}) : _sourcesRepository = sourcesRepository,
_countriesRepository = countriesRepository,
_languagesRepository = languagesRepository,
super(const CreateSourceState()) {
on<CreateSourceDataLoaded>(_onDataLoaded);
on<CreateSourceNameChanged>(_onNameChanged);
on<CreateSourceDescriptionChanged>(_onDescriptionChanged);
Expand All @@ -33,14 +33,14 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
on<CreateSourceSubmitted>(_onSubmitted);
on<CreateSourceCountrySearchChanged>(
_onCountrySearchChanged,
transformer: restartable(),
transformer: droppable(),
);
on<CreateSourceLoadMoreCountriesRequested>(
_onLoadMoreCountriesRequested,
);
on<CreateSourceLanguageSearchChanged>(
_onLanguageSearchChanged,
transformer: restartable(),
transformer: droppable(),
);
on<CreateSourceLoadMoreLanguagesRequested>(
_onLoadMoreLanguagesRequested,
Expand Down Expand Up @@ -194,8 +194,7 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
emit(state.copyWith(countrySearchTerm: event.searchTerm));
try {
final countriesResponse = await _countriesRepository.readAll(
filter:
event.searchTerm.isNotEmpty ? {'name': event.searchTerm} : null,
filter: event.searchTerm.isNotEmpty ? {'name': event.searchTerm} : null,
sort: [const SortOption('name', SortOrder.asc)],
);

Expand All @@ -222,7 +221,9 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
CreateSourceLoadMoreCountriesRequested event,
Emitter<CreateSourceState> emit,
) async {
if (!state.countriesHasMore) return;
if (!state.countriesHasMore || state.countriesIsLoadingMore) return;

emit(state.copyWith(countriesIsLoadingMore: true));

try {
final countriesResponse = await _countriesRepository.readAll(
Expand All @@ -240,15 +241,23 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
countries: List.of(state.countries)..addAll(countriesResponse.items),
countriesCursor: countriesResponse.cursor,
countriesHasMore: countriesResponse.hasMore,
countriesIsLoadingMore: false,
),
);
} on HttpException catch (e) {
emit(state.copyWith(status: CreateSourceStatus.failure, exception: e));
emit(
state.copyWith(
status: CreateSourceStatus.failure,
exception: e,
countriesIsLoadingMore: false,
),
);
} catch (e) {
emit(
state.copyWith(
status: CreateSourceStatus.failure,
exception: UnknownException('An unexpected error occurred: $e'),
countriesIsLoadingMore: false,
),
);
}
Expand All @@ -262,8 +271,7 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
emit(state.copyWith(languageSearchTerm: event.searchTerm));
try {
final languagesResponse = await _languagesRepository.readAll(
filter:
event.searchTerm.isNotEmpty ? {'name': event.searchTerm} : null,
filter: event.searchTerm.isNotEmpty ? {'name': event.searchTerm} : null,
sort: [const SortOption('name', SortOrder.asc)],
);

Expand All @@ -290,7 +298,9 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
CreateSourceLoadMoreLanguagesRequested event,
Emitter<CreateSourceState> emit,
) async {
if (!state.languagesHasMore) return;
if (!state.languagesHasMore || state.languagesIsLoadingMore) return;

emit(state.copyWith(languagesIsLoadingMore: true));

try {
final languagesResponse = await _languagesRepository.readAll(
Expand All @@ -305,19 +315,26 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {

emit(
state.copyWith(
languages: List.of(state.languages)
..addAll(languagesResponse.items),
languages: List.of(state.languages)..addAll(languagesResponse.items),
languagesCursor: languagesResponse.cursor,
languagesHasMore: languagesResponse.hasMore,
languagesIsLoadingMore: false,
),
);
} on HttpException catch (e) {
emit(state.copyWith(status: CreateSourceStatus.failure, exception: e));
emit(
state.copyWith(
status: CreateSourceStatus.failure,
exception: e,
languagesIsLoadingMore: false,
),
);
} catch (e) {
emit(
state.copyWith(
status: CreateSourceStatus.failure,
exception: UnknownException('An unexpected error occurred: $e'),
languagesIsLoadingMore: false,
),
);
}
Expand Down
12 changes: 12 additions & 0 deletions lib/content_management/bloc/create_source/create_source_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ final class CreateSourceState extends Equatable {
this.headquarters,
this.countries = const [],
this.countriesHasMore = true,
this.countriesIsLoadingMore = false,
this.countriesCursor,
this.countrySearchTerm = '',
this.languages = const [],
this.languagesHasMore = true,
this.languagesIsLoadingMore = false,
this.languagesCursor,
this.languageSearchTerm = '',
this.contentStatus = ContentStatus.active,
Expand All @@ -51,10 +53,12 @@ final class CreateSourceState extends Equatable {
final Country? headquarters;
final List<Country> countries;
final bool countriesHasMore;
final bool countriesIsLoadingMore;
final String? countriesCursor;
final String countrySearchTerm;
final List<Language> languages;
final bool languagesHasMore;
final bool languagesIsLoadingMore;
final String? languagesCursor;
final String languageSearchTerm;
final ContentStatus contentStatus;
Expand All @@ -80,10 +84,12 @@ final class CreateSourceState extends Equatable {
ValueGetter<Country?>? headquarters,
List<Country>? countries,
bool? countriesHasMore,
bool? countriesIsLoadingMore,
String? countriesCursor,
String? countrySearchTerm,
List<Language>? languages,
bool? languagesHasMore,
bool? languagesIsLoadingMore,
String? languagesCursor,
String? languageSearchTerm,
ContentStatus? contentStatus,
Expand All @@ -100,10 +106,14 @@ final class CreateSourceState extends Equatable {
headquarters: headquarters != null ? headquarters() : this.headquarters,
countries: countries ?? this.countries,
countriesHasMore: countriesHasMore ?? this.countriesHasMore,
countriesIsLoadingMore:
countriesIsLoadingMore ?? this.countriesIsLoadingMore,
countriesCursor: countriesCursor ?? this.countriesCursor,
countrySearchTerm: countrySearchTerm ?? this.countrySearchTerm,
languages: languages ?? this.languages,
languagesHasMore: languagesHasMore ?? this.languagesHasMore,
languagesIsLoadingMore:
languagesIsLoadingMore ?? this.languagesIsLoadingMore,
languagesCursor: languagesCursor ?? this.languagesCursor,
languageSearchTerm: languageSearchTerm ?? this.languageSearchTerm,
contentStatus: contentStatus ?? this.contentStatus,
Expand All @@ -123,10 +133,12 @@ final class CreateSourceState extends Equatable {
headquarters,
countries,
countriesHasMore,
countriesIsLoadingMore,
countriesCursor,
countrySearchTerm,
languages,
languagesHasMore,
languagesIsLoadingMore,
languagesCursor,
languageSearchTerm,
contentStatus,
Expand Down
31 changes: 20 additions & 11 deletions lib/content_management/bloc/edit_headline/edit_headline_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
required DataRepository<Topic> topicsRepository,
required DataRepository<Country> countriesRepository,
required String headlineId,
}) : _headlinesRepository = headlinesRepository,
_sourcesRepository = sourcesRepository,
_topicsRepository = topicsRepository,
_countriesRepository = countriesRepository,
_headlineId = headlineId,
super(const EditHeadlineState()) {
}) : _headlinesRepository = headlinesRepository,
_sourcesRepository = sourcesRepository,
_topicsRepository = topicsRepository,
_countriesRepository = countriesRepository,
_headlineId = headlineId,
super(const EditHeadlineState()) {
on<EditHeadlineLoaded>(_onLoaded);
on<EditHeadlineTitleChanged>(_onTitleChanged);
on<EditHeadlineExcerptChanged>(_onExcerptChanged);
Expand All @@ -37,7 +37,7 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
on<EditHeadlineSubmitted>(_onSubmitted);
on<EditHeadlineCountrySearchChanged>(
_onCountrySearchChanged,
transformer: restartable(),
transformer: droppable(),
);
on<EditHeadlineLoadMoreCountriesRequested>(
_onLoadMoreCountriesRequested,
Expand Down Expand Up @@ -253,8 +253,7 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
emit(state.copyWith(countrySearchTerm: event.searchTerm));
try {
final countriesResponse = await _countriesRepository.readAll(
filter:
event.searchTerm.isNotEmpty ? {'name': event.searchTerm} : null,
filter: event.searchTerm.isNotEmpty ? {'name': event.searchTerm} : null,
sort: [const SortOption('name', SortOrder.asc)],
);

Expand All @@ -281,7 +280,9 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
EditHeadlineLoadMoreCountriesRequested event,
Emitter<EditHeadlineState> emit,
) async {
if (!state.countriesHasMore) return;
if (!state.countriesHasMore || state.countriesIsLoadingMore) return;

emit(state.copyWith(countriesIsLoadingMore: true));

try {
final countriesResponse = await _countriesRepository.readAll(
Expand All @@ -299,15 +300,23 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
countries: List.of(state.countries)..addAll(countriesResponse.items),
countriesCursor: countriesResponse.cursor,
countriesHasMore: countriesResponse.hasMore,
countriesIsLoadingMore: false,
),
);
} on HttpException catch (e) {
emit(state.copyWith(status: EditHeadlineStatus.failure, exception: e));
emit(
state.copyWith(
status: EditHeadlineStatus.failure,
exception: e,
countriesIsLoadingMore: false,
),
);
} catch (e) {
emit(
state.copyWith(
status: EditHeadlineStatus.failure,
exception: UnknownException('An unexpected error occurred: $e'),
countriesIsLoadingMore: false,
),
);
}
Expand Down
Loading
Loading