Skip to content

Commit 6358a8c

Browse files
committed
refactor(content_management): improve source creation page
- Move country and language data fetching logic to BlocListener - Update CreateSourceBloc with latest data from ContentManagementBloc - Enhance code readability and maintainability
1 parent 45bda50 commit 6358a8c

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

lib/content_management/view/create_source_page.dart

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,11 @@ class CreateSourcePage extends StatelessWidget {
2020

2121
@override
2222
Widget build(BuildContext context) {
23-
// The lists of all countries and languages are fetched once and cached in
24-
// the ContentManagementBloc. We read them here and provide them to the
25-
// CreateSourceBloc.
26-
final contentManagementState = context.read<ContentManagementBloc>().state;
27-
final allCountries = contentManagementState.allCountries;
28-
final allLanguages = contentManagementState.allLanguages;
29-
3023
return BlocProvider(
3124
create: (context) => CreateSourceBloc(
3225
sourcesRepository: context.read<DataRepository<Source>>(),
33-
countries: allCountries,
34-
languages: allLanguages,
26+
countries: context.read<ContentManagementBloc>().state.allCountries,
27+
languages: context.read<ContentManagementBloc>().state.allLanguages,
3528
),
3629
child: const _CreateSourceView(),
3730
);
@@ -80,12 +73,26 @@ class _CreateSourceViewState extends State<_CreateSourceView> {
8073
),
8174
],
8275
),
83-
body: BlocConsumer<CreateSourceBloc, CreateSourceState>(
84-
listenWhen: (previous, current) => previous.status != current.status,
85-
listener: (context, state) {
86-
if (state.status == CreateSourceStatus.success &&
87-
state.createdSource != null &&
88-
ModalRoute.of(context)!.isCurrent) {
76+
body: BlocListener<ContentManagementBloc, ContentManagementState>(
77+
listenWhen: (previous, current) =>
78+
(previous.allCountriesStatus != current.allCountriesStatus &&
79+
current.allCountriesStatus == ContentManagementStatus.success) ||
80+
(previous.allLanguagesStatus != current.allLanguagesStatus &&
81+
current.allLanguagesStatus == ContentManagementStatus.success),
82+
listener: (context, contentState) {
83+
context.read<CreateSourceBloc>().add(
84+
CreateSourceDataUpdated(
85+
countries: contentState.allCountries,
86+
languages: contentState.allLanguages,
87+
),
88+
);
89+
},
90+
child: BlocConsumer<CreateSourceBloc, CreateSourceState>(
91+
listenWhen: (previous, current) => previous.status != current.status,
92+
listener: (context, state) {
93+
if (state.status == CreateSourceStatus.success &&
94+
state.createdSource != null &&
95+
ModalRoute.of(context)!.isCurrent) {
8996
ScaffoldMessenger.of(context)
9097
..hideCurrentSnackBar()
9198
..showSnackBar(
@@ -108,14 +115,14 @@ class _CreateSourceViewState extends State<_CreateSourceView> {
108115
);
109116
}
110117
},
111-
builder: (context, state) {
112-
if (state.status == CreateSourceStatus.loading) {
113-
return LoadingStateWidget(
114-
icon: Icons.source,
115-
headline: l10n.loadingData,
116-
subheadline: l10n.pleaseWait,
117-
);
118-
}
118+
builder: (context, state) {
119+
if (state.status == CreateSourceStatus.loading) {
120+
return LoadingStateWidget(
121+
icon: Icons.source,
122+
headline: l10n.loadingData,
123+
subheadline: l10n.pleaseWait,
124+
);
125+
}
119126

120127
if (state.status == CreateSourceStatus.failure) {
121128
return FailureStateWidget(

0 commit comments

Comments
 (0)