@@ -5,6 +5,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
55import 'package:flutter_news_app_mobile_client_full_source_code/account/bloc/available_sources_bloc.dart' ;
66import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart' ;
77import 'package:flutter_news_app_mobile_client_full_source_code/l10n/l10n.dart' ;
8+ import 'package:flutter_news_app_mobile_client_full_source_code/shared/services/content_limitation_service.dart' ;
9+ import 'package:flutter_news_app_mobile_client_full_source_code/shared/widgets/content_limitation_bottom_sheet.dart' ;
810import 'package:ui_kit/ui_kit.dart' ;
911
1012/// {@template add_source_to_follow_page}
@@ -80,29 +82,60 @@ class AddSourceToFollowPage extends StatelessWidget {
8082 ? l10n.unfollowSourceTooltip (source.name)
8183 : l10n.followSourceTooltip (source.name),
8284 onPressed: () {
85+ // Ensure user preferences are available before
86+ // proceeding.
8387 if (userContentPreferences == null ) return ;
8488
89+ // Create a mutable copy of the followed sources list.
8590 final updatedFollowedSources = List <Source >.from (
8691 followedSources,
8792 );
93+
94+ // If the user is unfollowing, always allow it.
8895 if (isFollowed) {
8996 updatedFollowedSources.removeWhere (
9097 (s) => s.id == source.id,
9198 );
99+ final updatedPreferences = userContentPreferences
100+ .copyWith (
101+ followedSources: updatedFollowedSources,
102+ );
103+
104+ context.read <AppBloc >().add (
105+ AppUserContentPreferencesChanged (
106+ preferences: updatedPreferences,
107+ ),
108+ );
92109 } else {
93- updatedFollowedSources.add (source);
94- }
110+ // If the user is following, check the limit first.
111+ final limitationService = context
112+ .read <ContentLimitationService >();
113+ final status = limitationService.checkAction (
114+ ContentAction .followSource,
115+ );
95116
96- final updatedPreferences = userContentPreferences
97- .copyWith (
98- followedSources: updatedFollowedSources,
99- );
117+ if (status == LimitationStatus .allowed) {
118+ updatedFollowedSources.add (source);
119+ final updatedPreferences =
120+ userContentPreferences.copyWith (
121+ followedSources: updatedFollowedSources,
122+ );
100123
101- context.read <AppBloc >().add (
102- AppUserContentPreferencesChanged (
103- preferences: updatedPreferences,
104- ),
105- );
124+ context.read <AppBloc >().add (
125+ AppUserContentPreferencesChanged (
126+ preferences: updatedPreferences,
127+ ),
128+ );
129+ } else {
130+ // If the limit is reached, show the bottom sheet.
131+ showModalBottomSheet <void >(
132+ context: context,
133+ builder: (_) => ContentLimitationBottomSheet (
134+ status: status,
135+ ),
136+ );
137+ }
138+ }
106139 },
107140 ),
108141 ),
0 commit comments