Skip to content

Commit 84beb22

Browse files
committed
refactor(ads): simplify ad loading process and improve caching
- Replace InArticleAdSlotConfiguration with InArticleAdSlotType for simplicity - Streamline ad loading logic by removing slot configuration checks - Update caching mechanism to use slot type instead of full configuration - Add user role consideration when loading in-article ads - Improve logging throughout the ad loading process
1 parent 9d4e5f5 commit 84beb22

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

lib/ads/widgets/in_article_ad_loader_widget.dart

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ import 'package:ui_kit/ui_kit.dart';
4141
class InArticleAdLoaderWidget extends StatefulWidget {
4242
/// {@macro in_article_ad_loader_widget}
4343
const InArticleAdLoaderWidget({
44-
required this.slotConfiguration,
44+
required this.slotType,
4545
required this.adThemeStyle,
4646
required this.adConfig,
4747
super.key,
4848
});
4949

50-
/// The configuration for this specific in-article ad slot.
51-
final InArticleAdSlotConfiguration slotConfiguration;
50+
/// The type of the in-article ad slot.
51+
final InArticleAdSlotType slotType;
5252

5353
/// The current theme style for ads, used during ad loading.
5454
final AdThemeStyle adThemeStyle;
@@ -67,8 +67,7 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
6767
bool _hasError = false;
6868
final Logger _logger = Logger('InArticleAdLoaderWidget');
6969
late final InlineAdCacheService _adCacheService;
70-
late final AdService
71-
_adService; // AdService will be accessed via _adCacheService
70+
late final AdService _adService;
7271

7372
Completer<void>? _loadAdCompleter;
7473

@@ -83,24 +82,24 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
8382
@override
8483
void didUpdateWidget(covariant InArticleAdLoaderWidget oldWidget) {
8584
super.didUpdateWidget(oldWidget);
86-
// If the slotConfiguration changes, it means this widget is being reused
85+
// If the slotType changes, it means this widget is being reused
8786
// for a different ad slot. We need to cancel any ongoing load for the old
8887
// ad and initiate a new load for the new ad.
8988
// Also, if the adConfig changes, we should re-evaluate and potentially reload.
90-
if (widget.slotConfiguration != oldWidget.slotConfiguration ||
89+
if (widget.slotType != oldWidget.slotType ||
9190
widget.adConfig != oldWidget.adConfig) {
9291
_logger.info(
93-
'InArticleAdLoaderWidget updated for new slot configuration or AdConfig changed. Re-loading ad.',
92+
'InArticleAdLoaderWidget updated for new slot type: '
93+
'${widget.slotType.name} or AdConfig changed. Re-loading ad.',
9494
);
9595
// Dispose of the old ad's resources before loading a new one.
96-
final oldCacheKey =
97-
'in_article_ad_${oldWidget.slotConfiguration.slotType.name}';
96+
final oldCacheKey = 'in_article_ad_${oldWidget.slotType.name}';
9897
// Only dispose if it was actually cached (i.e., not an AdMob in-article ad).
9998
// The removeAndDisposeAd method handles the check internally.
10099
_adCacheService.removeAndDisposeAd(oldCacheKey);
101100

102101
if (_loadAdCompleter != null && !_loadAdCompleter!.isCompleted) {
103-
_loadAdCompleter!.complete(); // Complete normally to prevent crashes
102+
_loadAdCompleter!.complete();
104103
}
105104
_loadAdCompleter = null;
106105

@@ -116,13 +115,13 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
116115
@override
117116
void dispose() {
118117
// Dispose of the ad's resources when the widget is permanently removed.
119-
final cacheKey = 'in_article_ad_${widget.slotConfiguration.slotType.name}';
118+
final cacheKey = 'in_article_ad_${widget.slotType.name}';
120119
// Only dispose if it was actually cached (i.e., not an AdMob in-article ad).
121120
// The removeAndDisposeAd method handles the check internally.
122121
_adCacheService.removeAndDisposeAd(cacheKey);
123122

124123
if (_loadAdCompleter != null && !_loadAdCompleter!.isCompleted) {
125-
_loadAdCompleter!.complete(); // Complete normally to prevent crashes
124+
_loadAdCompleter!.complete();
126125
}
127126
_loadAdCompleter = null;
128127
super.dispose();
@@ -147,7 +146,7 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
147146

148147
// In-article ads are typically unique to their slot, so we use the slotType
149148
// as part of the cache key to differentiate them.
150-
final cacheKey = 'in_article_ad_${widget.slotConfiguration.slotType.name}';
149+
final cacheKey = 'in_article_ad_${widget.slotType.name}';
151150
InlineAd? loadedAd;
152151

153152
// Determine if the primary ad platform is AdMob.
@@ -158,7 +157,7 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
158157
final cachedAd = _adCacheService.getAd(cacheKey);
159158
if (cachedAd != null) {
160159
_logger.info(
161-
'Using cached in-article ad for slot: ${widget.slotConfiguration.slotType.name}',
160+
'Using cached in-article ad for slot: ${widget.slotType.name}',
162161
);
163162
if (!mounted) return;
164163
setState(() {
@@ -173,23 +172,27 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
173172
} else {
174173
_logger.info(
175174
'AdMob is primary ad platform. Bypassing cache for in-article ad '
176-
'for slot: ${widget.slotConfiguration.slotType.name}.',
175+
'for slot: ${widget.slotType.name}.',
177176
);
178177
}
179178

180-
_logger.info(
181-
'Loading new in-article ad for slot: ${widget.slotConfiguration.slotType.name}',
182-
);
179+
_logger.info('Loading new in-article ad for slot: ${widget.slotType.name}');
183180
try {
181+
// Get the current user role from AppBloc
182+
final appBlocState = context.read<AppBloc>().state;
183+
final userRole = appBlocState.user?.appRole ?? AppUserRole.guestUser;
184+
184185
// Call AdService.getInArticleAd with the full AdConfig.
185186
loadedAd = await _adService.getInArticleAd(
186187
adConfig: widget.adConfig,
187188
adThemeStyle: widget.adThemeStyle,
189+
userRole: userRole,
190+
slotType: widget.slotType,
188191
);
189192

190193
if (loadedAd != null) {
191194
_logger.info(
192-
'New in-article ad loaded for slot: ${widget.slotConfiguration.slotType.name}',
195+
'New in-article ad loaded for slot: ${widget.slotType.name}',
193196
);
194197
// Only cache non-AdMob ads. AdMob ads are not cached to prevent reuse issues.
195198
if (!isAdMob) {
@@ -210,7 +213,7 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
210213
}
211214
} else {
212215
_logger.warning(
213-
'Failed to load in-article ad for slot: ${widget.slotConfiguration.slotType.name}. '
216+
'Failed to load in-article ad for slot: ${widget.slotType.name}. '
214217
'No ad returned.',
215218
);
216219
if (!mounted) return;
@@ -226,7 +229,7 @@ class _InArticleAdLoaderWidgetState extends State<InArticleAdLoaderWidget> {
226229
}
227230
} catch (e, s) {
228231
_logger.severe(
229-
'Error loading in-article ad for slot: ${widget.slotConfiguration.slotType.name}: $e',
232+
'Error loading in-article ad for slot: ${widget.slotType.name}: $e',
230233
e,
231234
s,
232235
);

0 commit comments

Comments
 (0)