Skip to content

Commit 08e7b8b

Browse files
authored
Merge pull request #47 from flutter-news-app-full-source-code/fix-content-management-form
Fix content management form
2 parents 320fbed + c1dc563 commit 08e7b8b

16 files changed

+98
-607
lines changed

lib/content_management/bloc/create_headline/create_headline_bloc.dart

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

lib/content_management/bloc/create_headline/create_headline_event.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,3 @@ final class CreateHeadlineStatusChanged extends CreateHeadlineEvent {
8383
final class CreateHeadlineSubmitted extends CreateHeadlineEvent {
8484
const CreateHeadlineSubmitted();
8585
}
86-
87-
/// Event to request loading more countries.
88-
final class CreateHeadlineLoadMoreCountriesRequested
89-
extends CreateHeadlineEvent {
90-
const CreateHeadlineLoadMoreCountriesRequested();
91-
}

lib/content_management/bloc/create_source/create_source_bloc.dart

Lines changed: 30 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
3131
on<CreateSourceHeadquartersChanged>(_onHeadquartersChanged);
3232
on<CreateSourceStatusChanged>(_onStatusChanged);
3333
on<CreateSourceSubmitted>(_onSubmitted);
34-
on<CreateSourceLoadMoreCountriesRequested>(
35-
_onLoadMoreCountriesRequested,
36-
);
37-
on<CreateSourceLoadMoreLanguagesRequested>(
38-
_onLoadMoreLanguagesRequested,
39-
);
4034
}
4135

4236
final DataRepository<Source> _sourcesRepository;
@@ -71,6 +65,36 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
7165
languagesHasMore: languagesPaginated.hasMore,
7266
),
7367
);
68+
69+
// Start background fetching for all countries
70+
while (state.countriesHasMore) {
71+
final nextCountries = await _countriesRepository.readAll(
72+
pagination: PaginationOptions(cursor: state.countriesCursor),
73+
sort: [const SortOption('name', SortOrder.asc)],
74+
);
75+
emit(
76+
state.copyWith(
77+
countries: List.of(state.countries)..addAll(nextCountries.items),
78+
countriesCursor: nextCountries.cursor,
79+
countriesHasMore: nextCountries.hasMore,
80+
),
81+
);
82+
}
83+
84+
// Start background fetching for all languages
85+
while (state.languagesHasMore) {
86+
final nextLanguages = await _languagesRepository.readAll(
87+
pagination: PaginationOptions(cursor: state.languagesCursor),
88+
sort: [const SortOption('name', SortOrder.asc)],
89+
);
90+
emit(
91+
state.copyWith(
92+
languages: List.of(state.languages)..addAll(nextLanguages.items),
93+
languagesCursor: nextLanguages.cursor,
94+
languagesHasMore: nextLanguages.hasMore,
95+
),
96+
);
97+
}
7498
} on HttpException catch (e) {
7599
emit(state.copyWith(status: CreateSourceStatus.failure, exception: e));
76100
} catch (e) {
@@ -177,90 +201,4 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
177201
);
178202
}
179203
}
180-
181-
Future<void> _onLoadMoreCountriesRequested(
182-
CreateSourceLoadMoreCountriesRequested event,
183-
Emitter<CreateSourceState> emit,
184-
) async {
185-
if (!state.countriesHasMore || state.countriesIsLoadingMore) return;
186-
187-
emit(state.copyWith(countriesIsLoadingMore: true));
188-
189-
try {
190-
final countriesResponse = await _countriesRepository.readAll(
191-
pagination: state.countriesCursor != null
192-
? PaginationOptions(cursor: state.countriesCursor)
193-
: null,
194-
sort: [const SortOption('name', SortOrder.asc)],
195-
);
196-
197-
emit(
198-
state.copyWith(
199-
countries: List.of(state.countries)..addAll(countriesResponse.items),
200-
countriesCursor: countriesResponse.cursor,
201-
countriesHasMore: countriesResponse.hasMore,
202-
countriesIsLoadingMore: false,
203-
),
204-
);
205-
} on HttpException catch (e) {
206-
emit(
207-
state.copyWith(
208-
status: CreateSourceStatus.failure,
209-
exception: e,
210-
countriesIsLoadingMore: false,
211-
),
212-
);
213-
} catch (e) {
214-
emit(
215-
state.copyWith(
216-
status: CreateSourceStatus.failure,
217-
exception: UnknownException('An unexpected error occurred: $e'),
218-
countriesIsLoadingMore: false,
219-
),
220-
);
221-
}
222-
}
223-
224-
Future<void> _onLoadMoreLanguagesRequested(
225-
CreateSourceLoadMoreLanguagesRequested event,
226-
Emitter<CreateSourceState> emit,
227-
) async {
228-
if (!state.languagesHasMore || state.languagesIsLoadingMore) return;
229-
230-
emit(state.copyWith(languagesIsLoadingMore: true));
231-
232-
try {
233-
final languagesResponse = await _languagesRepository.readAll(
234-
pagination: state.languagesCursor != null
235-
? PaginationOptions(cursor: state.languagesCursor)
236-
: null,
237-
sort: [const SortOption('name', SortOrder.asc)],
238-
);
239-
240-
emit(
241-
state.copyWith(
242-
languages: List.of(state.languages)..addAll(languagesResponse.items),
243-
languagesCursor: languagesResponse.cursor,
244-
languagesHasMore: languagesResponse.hasMore,
245-
languagesIsLoadingMore: false,
246-
),
247-
);
248-
} on HttpException catch (e) {
249-
emit(
250-
state.copyWith(
251-
status: CreateSourceStatus.failure,
252-
exception: e,
253-
languagesIsLoadingMore: false,
254-
),
255-
);
256-
} catch (e) {
257-
emit(
258-
state.copyWith(
259-
status: CreateSourceStatus.failure,
260-
exception: UnknownException('An unexpected error occurred: $e'),
261-
languagesIsLoadingMore: false,
262-
),
263-
);
264-
}
265-
}
266204
}

lib/content_management/bloc/create_source/create_source_event.dart

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,3 @@ final class CreateSourceStatusChanged extends CreateSourceEvent {
7575
final class CreateSourceSubmitted extends CreateSourceEvent {
7676
const CreateSourceSubmitted();
7777
}
78-
79-
/// Event to request loading more countries.
80-
final class CreateSourceLoadMoreCountriesRequested extends CreateSourceEvent {
81-
const CreateSourceLoadMoreCountriesRequested();
82-
}
83-
84-
/// Event to request loading more languages.
85-
final class CreateSourceLoadMoreLanguagesRequested extends CreateSourceEvent {
86-
const CreateSourceLoadMoreLanguagesRequested();
87-
}

lib/content_management/bloc/create_source/create_source_state.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ final class CreateSourceState extends Equatable {
125125
headquarters,
126126
countries,
127127
countriesHasMore,
128-
countriesIsLoadingMore,
129-
countriesCursor,
130-
languages,
131-
languagesHasMore,
132-
languagesIsLoadingMore,
133-
languagesCursor,
134-
contentStatus,
135-
exception,
128+
countriesIsLoadingMore,
129+
countriesCursor,
130+
languages,
131+
languagesHasMore,
132+
languagesIsLoadingMore,
133+
languagesCursor,
134+
contentStatus,
135+
exception,
136136
createdSource,
137137
];
138138
}

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
}

lib/content_management/bloc/edit_headline/edit_headline_event.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,3 @@ final class EditHeadlineStatusChanged extends EditHeadlineEvent {
8383
final class EditHeadlineSubmitted extends EditHeadlineEvent {
8484
const EditHeadlineSubmitted();
8585
}
86-
87-
/// Event to request loading more countries.
88-
final class EditHeadlineLoadMoreCountriesRequested extends EditHeadlineEvent {
89-
const EditHeadlineLoadMoreCountriesRequested();
90-
}

0 commit comments

Comments
 (0)