Skip to content

Commit 48b805c

Browse files
committed
refactor(ads): use injected InlineAdCacheService instead of creating new instance
- Inject InlineAdCacheService into HeadlinesFeedBloc constructor - Replace InlineAdCacheService() calls with _inlineAdCacheService field - Remove unused AdService import and usage in HeadlinesFeedPage
1 parent 7dc7ed9 commit 48b805c

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

lib/headlines-feed/bloc/headlines_feed_bloc.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ class HeadlinesFeedBloc extends Bloc<HeadlinesFeedEvent, HeadlinesFeedState> {
3131
userContentPreferencesRepository,
3232
required FeedDecoratorService feedDecoratorService,
3333
required AppBloc appBloc,
34+
required InlineAdCacheService inlineAdCacheService,
3435
}) : _headlinesRepository = headlinesRepository,
3536
_userContentPreferencesRepository = userContentPreferencesRepository,
3637
_feedDecoratorService = feedDecoratorService,
3738
_appBloc = appBloc,
39+
_inlineAdCacheService = inlineAdCacheService,
3840
super(const HeadlinesFeedState()) {
3941
on<HeadlinesFeedFetchRequested>(
4042
_onHeadlinesFeedFetchRequested,
@@ -65,6 +67,7 @@ class HeadlinesFeedBloc extends Bloc<HeadlinesFeedEvent, HeadlinesFeedState> {
6567
_userContentPreferencesRepository;
6668
final FeedDecoratorService _feedDecoratorService;
6769
final AppBloc _appBloc;
70+
final InlineAdCacheService _inlineAdCacheService;
6871

6972
/// The number of headlines to fetch per page.
7073
static const _headlinesFetchLimit = 10;
@@ -154,7 +157,7 @@ class HeadlinesFeedBloc extends Bloc<HeadlinesFeedEvent, HeadlinesFeedState> {
154157
Emitter<HeadlinesFeedState> emit,
155158
) async {
156159
// On a full refresh, clear the ad cache to ensure fresh ads are loaded.
157-
InlineAdCacheService().clearAllAds();
160+
_inlineAdCacheService.clearAllAds();
158161
emit(state.copyWith(status: HeadlinesFeedStatus.loading));
159162
try {
160163
final currentUser = _appBloc.state.user;
@@ -236,7 +239,7 @@ class HeadlinesFeedBloc extends Bloc<HeadlinesFeedEvent, HeadlinesFeedState> {
236239
) async {
237240
// When applying new filters, this is considered a major feed change,
238241
// so we clear the ad cache to get a fresh set of relevant ads.
239-
InlineAdCacheService().clearAllAds();
242+
_inlineAdCacheService.clearAllAds();
240243
emit(
241244
state.copyWith(
242245
status: HeadlinesFeedStatus.loading,
@@ -323,7 +326,7 @@ class HeadlinesFeedBloc extends Bloc<HeadlinesFeedEvent, HeadlinesFeedState> {
323326
Emitter<HeadlinesFeedState> emit,
324327
) async {
325328
// Clearing filters is a major feed change, so clear the ad cache.
326-
InlineAdCacheService().clearAllAds();
329+
_inlineAdCacheService.clearAllAds();
327330
emit(
328331
state.copyWith(
329332
status: HeadlinesFeedStatus.loading,

lib/headlines-feed/view/headlines_feed_page.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'package:core/core.dart';
55
import 'package:flutter/material.dart';
66
import 'package:flutter_bloc/flutter_bloc.dart';
77
import 'package:flutter_news_app_mobile_client_full_source_code/account/bloc/account_bloc.dart';
8-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_service.dart';
98
import 'package:flutter_news_app_mobile_client_full_source_code/ads/interstitial_ad_manager.dart';
109
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_placeholder.dart';
1110
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart';
@@ -344,7 +343,6 @@ class _HeadlinesFeedPageState extends State<HeadlinesFeedPage> {
344343

345344
return FeedAdLoaderWidget(
346345
adPlaceholder: item,
347-
adService: context.read<AdService>(),
348346
adThemeStyle: AdThemeStyle.fromTheme(Theme.of(context)),
349347
adConfig: adConfig,
350348
);

0 commit comments

Comments
 (0)