Skip to content

Commit 45bda50

Browse files
committed
refactor(content_management): improve source editing with dynamic data
- Remove direct access to ContentManagementBloc state for country and language lists - Use BlocListener for ContentManagementBloc to update EditSourceBloc with new data - Add EditSourceDataUpdated event to handle updated country and language lists - Maintain existing functionality for source editing and status updates
1 parent ab20f6c commit 45bda50

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

lib/content_management/view/edit_source_page.dart

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,11 @@ class EditSourcePage extends StatelessWidget {
2323

2424
@override
2525
Widget build(BuildContext context) {
26-
// The lists of all countries and languages are fetched once and cached in
27-
// the ContentManagementBloc. We read them here and provide them to the
28-
// EditSourceBloc.
29-
final contentManagementState = context.read<ContentManagementBloc>().state;
30-
final allCountries = contentManagementState.allCountries;
31-
final allLanguages = contentManagementState.allLanguages;
32-
3326
return BlocProvider(
3427
create: (context) => EditSourceBloc(
3528
sourcesRepository: context.read<DataRepository<Source>>(),
36-
countries: allCountries,
37-
languages: allLanguages,
29+
countries: context.read<ContentManagementBloc>().state.allCountries,
30+
languages: context.read<ContentManagementBloc>().state.allLanguages,
3831
sourceId: sourceId,
3932
)..add(const EditSourceLoaded()),
4033
child: const _EditSourceView(),
@@ -104,14 +97,28 @@ class _EditSourceViewState extends State<_EditSourceView> {
10497
),
10598
],
10699
),
107-
body: BlocConsumer<EditSourceBloc, EditSourceState>(
100+
body: BlocListener<ContentManagementBloc, ContentManagementState>(
108101
listenWhen: (previous, current) =>
109-
previous.status != current.status ||
110-
previous.initialSource != current.initialSource,
111-
listener: (context, state) {
112-
if (state.status == EditSourceStatus.success &&
113-
state.updatedSource != null &&
114-
ModalRoute.of(context)!.isCurrent) {
102+
(previous.allCountriesStatus != current.allCountriesStatus &&
103+
current.allCountriesStatus == ContentManagementStatus.success) ||
104+
(previous.allLanguagesStatus != current.allLanguagesStatus &&
105+
current.allLanguagesStatus == ContentManagementStatus.success),
106+
listener: (context, contentState) {
107+
context.read<EditSourceBloc>().add(
108+
EditSourceDataUpdated(
109+
countries: contentState.allCountries,
110+
languages: contentState.allLanguages,
111+
),
112+
);
113+
},
114+
child: BlocConsumer<EditSourceBloc, EditSourceState>(
115+
listenWhen: (previous, current) =>
116+
previous.status != current.status ||
117+
previous.initialSource != current.initialSource,
118+
listener: (context, state) {
119+
if (state.status == EditSourceStatus.success &&
120+
state.updatedSource != null &&
121+
ModalRoute.of(context)!.isCurrent) {
115122
ScaffoldMessenger.of(context)
116123
..hideCurrentSnackBar()
117124
..showSnackBar(
@@ -138,14 +145,14 @@ class _EditSourceViewState extends State<_EditSourceView> {
138145
_urlController.text = state.url;
139146
}
140147
},
141-
builder: (context, state) {
142-
if (state.status == EditSourceStatus.loading) {
143-
return LoadingStateWidget(
144-
icon: Icons.source,
145-
headline: l10n.loadingSource,
146-
subheadline: l10n.pleaseWait,
147-
);
148-
}
148+
builder: (context, state) {
149+
if (state.status == EditSourceStatus.loading) {
150+
return LoadingStateWidget(
151+
icon: Icons.source,
152+
headline: l10n.loadingSource,
153+
subheadline: l10n.pleaseWait,
154+
);
155+
}
149156

150157
if (state.status == EditSourceStatus.failure &&
151158
state.initialSource == null) {

0 commit comments

Comments
 (0)