Skip to content

Commit 5ce7054

Browse files
committed
refactor(shared): improve content limitation logic and naming
- Update logic for checking content action limitations based on user role - Rename 'limits' variable to 'userPreferenceConfig' for clarity - Simplify limitation checks using switch cases instead of maps - Combine followTopic, followSource, and followCountry cases - Remove unnecessary whitespace and comments
1 parent c3d7971 commit 5ce7054

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

lib/shared/services/content_limitation_service.dart

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'package:core/core.dart';
22
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
33

4-
54
/// Defines the specific type of content-related action a user is trying to
65
/// perform, which may be subject to limitations.
76
enum ContentAction {
@@ -33,7 +32,6 @@ enum LimitationStatus {
3332
premiumUserLimitReached,
3433
}
3534

36-
3735
/// {@template content_limitation_service}
3836
/// A service that centralizes the logic for checking if a user can perform
3937
/// a content-related action based on their role and remote configuration limits.
@@ -65,35 +63,53 @@ class ContentLimitationService {
6563
return LimitationStatus.allowed;
6664
}
6765

68-
final limits = remoteConfig.contentConfiguration;
66+
final limits = remoteConfig.userPreferenceConfig;
6967
final role = user.appRole;
7068

7169
switch (action) {
7270
case ContentAction.bookmarkHeadline:
7371
final count = preferences.savedHeadlines.length;
74-
final limit = limits.savedHeadlinesLimit[role];
75-
if (limit != null && count >= limit) {
72+
final int limit;
73+
switch (role) {
74+
case AppUserRole.guestUser:
75+
limit = limits.guestSavedHeadlinesLimit;
76+
case AppUserRole.standardUser:
77+
limit = limits.authenticatedSavedHeadlinesLimit;
78+
case AppUserRole.premiumUser:
79+
limit = limits.premiumSavedHeadlinesLimit;
80+
}
81+
if (count >= limit) {
7682
return _getLimitationStatusForRole(role);
7783
}
7884

7985
case ContentAction.followTopic:
80-
final count = preferences.followedTopics.length;
81-
final limit = limits.followedTopicsLimit[role];
82-
if (limit != null && count >= limit) {
83-
return _getLimitationStatusForRole(role);
86+
case ContentAction.followSource:
87+
case ContentAction.followCountry:
88+
final int limit;
89+
switch (role) {
90+
case AppUserRole.guestUser:
91+
limit = limits.guestFollowedItemsLimit;
92+
case AppUserRole.standardUser:
93+
limit = limits.authenticatedFollowedItemsLimit;
94+
case AppUserRole.premiumUser:
95+
limit = limits.premiumFollowedItemsLimit;
8496
}
8597

86-
case ContentAction.followSource:
87-
final count = preferences.followedSources.length;
88-
final limit = limits.followedSourcesLimit[role];
89-
if (limit != null && count >= limit) {
90-
return _getLimitationStatusForRole(role);
98+
// Determine the count for the specific item type being followed.
99+
final int count;
100+
switch (action) {
101+
case ContentAction.followTopic:
102+
count = preferences.followedTopics.length;
103+
case ContentAction.followSource:
104+
count = preferences.followedSources.length;
105+
case ContentAction.followCountry:
106+
count = preferences.followedCountries.length;
107+
case ContentAction.bookmarkHeadline:
108+
// This case is handled above and will not be reached here.
109+
count = 0;
91110
}
92111

93-
case ContentAction.followCountry:
94-
final count = preferences.followedCountries.length;
95-
final limit = limits.followedCountriesLimit[role];
96-
if (limit != null && count >= limit) {
112+
if (count >= limit) {
97113
return _getLimitationStatusForRole(role);
98114
}
99115
}

0 commit comments

Comments
 (0)