Skip to content

Commit 5d3acf5

Browse files
committed
refactor(app_configuration): centralize decorator type visibility logic
This commit introduces a new method `_isDecoratorApplicableToRole` to centralize the logic for determining if a decorator type is applicable to a user role. This change improves code maintainability and prevents illogical configurations in the dashboard. - Added `_isDecoratorApplicableToRole` method in `_FeedDecoratorFormState` class - Updated checkbox visibility logic in `buildRoleConfigCheckbox` method
1 parent 25bbcbf commit 5d3acf5

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

lib/app_configuration/widgets/feed_decorator_form.dart

Lines changed: 33 additions & 7 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))),
221246
value: roleConfig != null,
222-
onChanged: isLinkAccountForStandardOrPremium
223-
? null // Disable for standard and premium users for linkAccount
224-
: (value) {
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(

0 commit comments

Comments
 (0)