Skip to content

Commit fb622a6

Browse files
authored
Merge pull request #83 from flutter-news-app-full-source-code/Implement-Logical-Constraints-in-FeedDecoratorForm
Implement logical constraints in feed decorator form
2 parents 25bbcbf + 272e7b8 commit fb622a6

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

lib/app_configuration/widgets/feed_decorator_form.dart

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,30 @@ class _FeedDecoratorFormState extends State<FeedDecoratorForm>
119119
super.dispose();
120120
}
121121

122+
/// Determines if a given decorator type is logically applicable to a user role.
123+
///
124+
/// This method centralizes the business logic for decorator visibility
125+
/// to prevent illogical configurations in the dashboard.
126+
bool _isDecoratorApplicableToRole(
127+
FeedDecoratorType decoratorType,
128+
AppUserRole role,
129+
) {
130+
switch (decoratorType) {
131+
// The 'linkAccount' decorator is only for guest users.
132+
case FeedDecoratorType.linkAccount:
133+
return role == AppUserRole.guestUser;
134+
// The 'upgrade' decorator is only for standard users.
135+
case FeedDecoratorType.upgrade:
136+
return role == AppUserRole.standardUser;
137+
// All other decorators are applicable to any user role.
138+
case FeedDecoratorType.rateApp:
139+
case FeedDecoratorType.enableNotifications:
140+
case FeedDecoratorType.suggestedTopics:
141+
case FeedDecoratorType.suggestedSources:
142+
return true;
143+
}
144+
}
145+
122146
@override
123147
Widget build(BuildContext context) {
124148
final l10n = AppLocalizationsX(context).l10n;
@@ -210,18 +234,19 @@ class _FeedDecoratorFormState extends State<FeedDecoratorForm>
210234
FeedDecoratorConfig decoratorConfig,
211235
) {
212236
final roleConfig = decoratorConfig.visibleTo[role];
213-
final isLinkAccountForStandardOrPremium =
214-
widget.decoratorType == FeedDecoratorType.linkAccount &&
215-
(role == AppUserRole.standardUser || role == AppUserRole.premiumUser);
237+
final isApplicable = _isDecoratorApplicableToRole(
238+
widget.decoratorType,
239+
role,
240+
);
216241

217242
return Column(
218243
children: [
219244
CheckboxListTile(
220245
title: Text(l10n.visibleToRoleLabel(role.l10n(context))),
221-
value: roleConfig != null,
222-
onChanged: isLinkAccountForStandardOrPremium
223-
? null // Disable for standard and premium users for linkAccount
224-
: (value) {
246+
value: roleConfig != null && isApplicable,
247+
// Disable the checkbox if the decorator is not applicable to the role.
248+
onChanged: isApplicable
249+
? (value) {
225250
final newVisibleTo =
226251
Map<AppUserRole, FeedDecoratorRoleConfig>.from(
227252
decoratorConfig.visibleTo,
@@ -245,7 +270,8 @@ class _FeedDecoratorFormState extends State<FeedDecoratorForm>
245270
feedDecoratorConfig: newFeedDecoratorConfig,
246271
),
247272
);
248-
},
273+
}
274+
: null,
249275
),
250276
if (roleConfig != null)
251277
Padding(

lib/content_management/bloc/draft_headlines/draft_headlines_bloc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class DraftHeadlinesBloc
120120
final headlineToPublish = originalHeadlines[headlineIndex];
121121
final updatedHeadlines = List<Headline>.from(originalHeadlines)
122122
..removeAt(headlineIndex);
123-
123+
124124
// Optimistically remove the headline from the UI.
125125
emit(
126126
state.copyWith(

lib/content_management/bloc/draft_topics/draft_topics_bloc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class DraftTopicsBloc extends Bloc<DraftTopicsEvent, DraftTopicsState> {
119119
final topicToPublish = originalTopics[topicIndex];
120120
final updatedTopics = List<Topic>.from(originalTopics)
121121
..removeAt(topicIndex);
122-
122+
123123
// Optimistically remove the topic from the UI.
124124
emit(
125125
state.copyWith(

0 commit comments

Comments
 (0)