Skip to content

Commit 967782d

Browse files
committed
fix(headlines-feed): improve ad caching and feed pagination
- Clear ad cache on full refresh, filter application, and filter clearance - Update feed pagination to use ad placeholders instead of ads - Refactor ad injection method name for clarity
1 parent f959dd4 commit 967782d

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

lib/headlines-feed/bloc/headlines_feed_bloc.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:bloc_concurrency/bloc_concurrency.dart';
55
import 'package:core/core.dart';
66
import 'package:data_repository/data_repository.dart';
77
import 'package:equatable/equatable.dart';
8+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_cache_service.dart';
89
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart';
910
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
1011
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/models/headline_filter.dart';
@@ -109,8 +110,9 @@ class HeadlinesFeedBloc extends Bloc<HeadlinesFeedEvent, HeadlinesFeedState> {
109110
sort: [const SortOption('updatedAt', SortOrder.desc)],
110111
);
111112

112-
// For pagination, only inject ads, not feed actions.
113-
final newProcessedFeedItems = await _feedDecoratorService.injectAds(
113+
// For pagination, only inject ad placeholders, not feed actions.
114+
final newProcessedFeedItems =
115+
await _feedDecoratorService.injectAdPlaceholders(
114116
feedItems: headlineResponse.items,
115117
user: currentUser,
116118
adConfig: remoteConfig.adConfig,
@@ -139,6 +141,8 @@ class HeadlinesFeedBloc extends Bloc<HeadlinesFeedEvent, HeadlinesFeedState> {
139141
HeadlinesFeedRefreshRequested event,
140142
Emitter<HeadlinesFeedState> emit,
141143
) async {
144+
// On a full refresh, clear the ad cache to ensure fresh ads are loaded.
145+
AdCacheService().clearAllAds();
142146
emit(state.copyWith(status: HeadlinesFeedStatus.loading));
143147
try {
144148
final currentUser = _appBloc.state.user;
@@ -213,6 +217,9 @@ class HeadlinesFeedBloc extends Bloc<HeadlinesFeedEvent, HeadlinesFeedState> {
213217
HeadlinesFeedFiltersApplied event,
214218
Emitter<HeadlinesFeedState> emit,
215219
) async {
220+
// When applying new filters, this is considered a major feed change,
221+
// so we clear the ad cache to get a fresh set of relevant ads.
222+
AdCacheService().clearAllAds();
216223
emit(
217224
state.copyWith(
218225
status: HeadlinesFeedStatus.loading,
@@ -292,6 +299,8 @@ class HeadlinesFeedBloc extends Bloc<HeadlinesFeedEvent, HeadlinesFeedState> {
292299
HeadlinesFeedFiltersCleared event,
293300
Emitter<HeadlinesFeedState> emit,
294301
) async {
302+
// Clearing filters is a major feed change, so clear the ad cache.
303+
AdCacheService().clearAllAds();
295304
emit(
296305
state.copyWith(
297306
status: HeadlinesFeedStatus.loading,

0 commit comments

Comments
 (0)