Skip to content

Commit ce06486

Browse files
committed
refactor(ads): improve in-article ad loading and caching
- Replace AdCacheService with InlineAdCacheService for better specificity - Integrate AppBloc to access and pass headlineImageStyle for ad customization - Enhance code readability by formatting conditional statements - Update class and variable names for consistency and clarity
1 parent 11dc2ea commit ce06486

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

lib/ads/widgets/in_article_ad_loader_widget.dart

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import 'dart:async';
22

33
import 'package:core/core.dart';
44
import 'package:flutter/material.dart';
5-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_cache_service.dart';
5+
import 'package:flutter_bloc/flutter_bloc.dart';
6+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/inline_ad_cache_service.dart';
67
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_service.dart';
78
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart';
89
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/banner_ad.dart';
@@ -12,6 +13,7 @@ import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/admo
1213
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/local_banner_ad_widget.dart';
1314
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/local_native_ad_widget.dart';
1415
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/placeholder_ad_widget.dart';
16+
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
1517
import 'package:logging/logging.dart';
1618
import 'package:ui_kit/ui_kit.dart';
1719

@@ -59,7 +61,7 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
5961
bool _isLoading = true;
6062
bool _hasError = false;
6163
final Logger _logger = Logger('InArticleAdLoaderWidget');
62-
final AdCacheService _adCacheService = AdCacheService();
64+
final InlineAdCacheService _adCacheService = InlineAdCacheService();
6365

6466
Completer<void>? _loadAdCompleter;
6567

@@ -110,7 +112,7 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
110112

111113
/// Loads the in-article ad for this slot.
112114
///
113-
/// This method first checks the [AdCacheService] for a pre-loaded [InlineAd].
115+
/// This method first checks the [InlineAdCacheService] for a pre-loaded [InlineAd].
114116
/// If found, it uses the cached ad. Otherwise, it requests a new in-article ad
115117
/// from the [AdService] using `getInArticleAd` and stores it in the cache
116118
/// upon success.
@@ -143,10 +145,19 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
143145
'Loading new in-article ad for slot: ${widget.slotConfiguration.slotType.name}',
144146
);
145147
try {
148+
// Get the current HeadlineImageStyle from AppBloc
149+
final headlineImageStyle = context
150+
.read<AppBloc>()
151+
.state
152+
.settings
153+
.feedPreferences
154+
.headlineImageStyle;
155+
146156
// Call AdService.getInArticleAd with the full AdConfig.
147157
final loadedAd = await widget.adService.getInArticleAd(
148158
adConfig: widget.adConfig,
149159
adThemeStyle: widget.adThemeStyle,
160+
headlineImageStyle: headlineImageStyle, // Pass the headlineImageStyle
150161
);
151162

152163
if (loadedAd != null) {
@@ -220,9 +231,14 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
220231
return AdmobInlineAdWidget(inlineAd: _loadedAd!);
221232
case AdPlatformType.local:
222233
if (_loadedAd is NativeAd && _loadedAd!.adObject is LocalNativeAd) {
223-
return LocalNativeAdWidget(localNativeAd: _loadedAd!.adObject as LocalNativeAd);
224-
} else if (_loadedAd is BannerAd && _loadedAd!.adObject is LocalBannerAd) {
225-
return LocalBannerAdWidget(localBannerAd: _loadedAd!.adObject as LocalBannerAd);
234+
return LocalNativeAdWidget(
235+
localNativeAd: _loadedAd!.adObject as LocalNativeAd,
236+
);
237+
} else if (_loadedAd is BannerAd &&
238+
_loadedAd!.adObject is LocalBannerAd) {
239+
return LocalBannerAdWidget(
240+
localBannerAd: _loadedAd!.adObject as LocalBannerAd,
241+
);
226242
}
227243
// Fallback for unsupported local ad types or errors
228244
return const PlaceholderAdWidget();

0 commit comments

Comments
 (0)