Skip to content

Commit ae0563c

Browse files
committed
feat(app): implement cache clearing for feed and ads on user change and logout
- Add FeedCacheService to AppBloc dependencies - Implement cache clearing logic in AppBloc for: - App logout - User change (anonymous to authenticated, or between different users) - Update App widget to provide FeedCacheService to AppBloc
1 parent fc7ba06 commit ae0563c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
1010
import 'package:flutter_news_app_mobile_client_full_source_code/ads/services/inline_ad_cache_service.dart';
1111
import 'package:flutter_news_app_mobile_client_full_source_code/app/models/app_life_cycle_status.dart';
1212
import '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';
1314
import 'package:flutter_news_app_mobile_client_full_source_code/app/services/app_initializer.dart';
1415
import 'package:flutter_news_app_mobile_client_full_source_code/notifications/services/push_notification_service.dart';
1516
import '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));

lib/app/view/app.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ class App extends StatelessWidget {
180180
contentLimitationService: context
181181
.read<ContentLimitationService>(),
182182
reportRepository: context.read<DataRepository<Report>>(),
183+
feedCacheService: context.read<FeedCacheService>(),
183184
)..add(const AppStarted()),
184185
),
185186
],

0 commit comments

Comments
 (0)