Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions lib/app_configuration/widgets/feed_decorator_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ class _FeedDecoratorFormState extends State<FeedDecoratorForm>
super.dispose();
}

/// Determines if a given decorator type is logically applicable to a user role.
///
/// This method centralizes the business logic for decorator visibility
/// to prevent illogical configurations in the dashboard.
bool _isDecoratorApplicableToRole(
FeedDecoratorType decoratorType,
AppUserRole role,
) {
switch (decoratorType) {
// The 'linkAccount' decorator is only for guest users.
case FeedDecoratorType.linkAccount:
return role == AppUserRole.guestUser;
// The 'upgrade' decorator is only for standard users.
case FeedDecoratorType.upgrade:
return role == AppUserRole.standardUser;
// All other decorators are applicable to any user role.
case FeedDecoratorType.rateApp:
case FeedDecoratorType.enableNotifications:
case FeedDecoratorType.suggestedTopics:
case FeedDecoratorType.suggestedSources:
return true;
}
}

@override
Widget build(BuildContext context) {
final l10n = AppLocalizationsX(context).l10n;
Expand Down Expand Up @@ -210,18 +234,19 @@ class _FeedDecoratorFormState extends State<FeedDecoratorForm>
FeedDecoratorConfig decoratorConfig,
) {
final roleConfig = decoratorConfig.visibleTo[role];
final isLinkAccountForStandardOrPremium =
widget.decoratorType == FeedDecoratorType.linkAccount &&
(role == AppUserRole.standardUser || role == AppUserRole.premiumUser);
final isApplicable = _isDecoratorApplicableToRole(
widget.decoratorType,
role,
);

return Column(
children: [
CheckboxListTile(
title: Text(l10n.visibleToRoleLabel(role.l10n(context))),
value: roleConfig != null,
onChanged: isLinkAccountForStandardOrPremium
? null // Disable for standard and premium users for linkAccount
: (value) {
value: roleConfig != null && isApplicable,
// Disable the checkbox if the decorator is not applicable to the role.
onChanged: isApplicable
? (value) {
final newVisibleTo =
Map<AppUserRole, FeedDecoratorRoleConfig>.from(
decoratorConfig.visibleTo,
Expand All @@ -245,7 +270,8 @@ class _FeedDecoratorFormState extends State<FeedDecoratorForm>
feedDecoratorConfig: newFeedDecoratorConfig,
),
);
},
}
: null,
),
if (roleConfig != null)
Padding(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class DraftHeadlinesBloc
final headlineToPublish = originalHeadlines[headlineIndex];
final updatedHeadlines = List<Headline>.from(originalHeadlines)
..removeAt(headlineIndex);

// Optimistically remove the headline from the UI.
emit(
state.copyWith(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class DraftTopicsBloc extends Bloc<DraftTopicsEvent, DraftTopicsState> {
final topicToPublish = originalTopics[topicIndex];
final updatedTopics = List<Topic>.from(originalTopics)
..removeAt(topicIndex);

// Optimistically remove the topic from the UI.
emit(
state.copyWith(
Expand Down
Loading