@@ -10,6 +10,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
1010import 'package:flutter_news_app_mobile_client_full_source_code/ads/services/inline_ad_cache_service.dart' ;
1111import 'package:flutter_news_app_mobile_client_full_source_code/app/models/app_life_cycle_status.dart' ;
1212import 'package:flutter_news_app_mobile_client_full_source_code/app/models/initialization_result.dart' ;
13+ import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/services/feed_cache_service.dart' ;
1314import 'package:flutter_news_app_mobile_client_full_source_code/app/services/app_initializer.dart' ;
1415import 'package:flutter_news_app_mobile_client_full_source_code/notifications/services/push_notification_service.dart' ;
1516import 'package:flutter_news_app_mobile_client_full_source_code/shared/extensions/extensions.dart' ;
@@ -46,6 +47,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
4647 required DataRepository <UserContentPreferences >
4748 userContentPreferencesRepository,
4849 required InlineAdCacheService inlineAdCacheService,
50+ required FeedCacheService feedCacheService,
4951 required Logger logger,
5052 required DataRepository <User > userRepository,
5153 required PushNotificationService pushNotificationService,
@@ -60,6 +62,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
6062 _userContentPreferencesRepository = userContentPreferencesRepository,
6163 _userRepository = userRepository,
6264 _inAppNotificationRepository = inAppNotificationRepository,
65+ _feedCacheService = feedCacheService,
6366 _pushNotificationService = pushNotificationService,
6467 _reportRepository = reportRepository,
6568 _contentLimitationService = contentLimitationService,
@@ -138,6 +141,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
138141 final ContentLimitationService _contentLimitationService;
139142 final AppReviewService _appReviewService;
140143 final InlineAdCacheService _inlineAdCacheService;
144+ final FeedCacheService _feedCacheService;
141145
142146 /// Handles the [AppStarted] event.
143147 ///
@@ -220,6 +224,10 @@ class AppBloc extends Bloc<AppEvent, AppState> {
220224 // data to ensure a clean state for the next session. This prevents
221225 // stale data from causing issues on subsequent logins.
222226 _inlineAdCacheService.clearAllAds ();
227+ // Also clear the feed cache to ensure the next user (or a new anonymous
228+ // session) gets fresh data instead of seeing the previous user's feed.
229+ _feedCacheService.clearAll ();
230+ _logger.info ('[AppBloc] Cleared inline ad and feed caches on logout.' );
223231
224232 emit (
225233 state.copyWith (
@@ -230,6 +238,17 @@ class AppBloc extends Bloc<AppEvent, AppState> {
230238 return ;
231239 }
232240
241+ // If the user is changing (e.g., anonymous to authenticated), clear caches
242+ // to prevent showing stale data from the previous user session.
243+ if (oldUser != null && oldUser.id != newUser.id) {
244+ _inlineAdCacheService.clearAllAds ();
245+ _feedCacheService.clearAll ();
246+ _logger.info (
247+ '[AppBloc] User changed from ${oldUser .id } to ${newUser .id }. '
248+ 'Cleared inline ad and feed caches.' ,
249+ );
250+ }
251+
233252 // A user is present, so we are logging in or transitioning roles.
234253 // Show a loading screen while we handle this process.
235254 emit (state.copyWith (status: AppLifeCycleStatus .loadingUserData));
0 commit comments