Skip to content

Commit 2ceb028

Browse files
committed
feat(ads): add method to get in-article ads
- Implement getInArticleAd method in AdService class - This method supports loading native or banner ads for in-article placements - It delegates ad loading to the appropriate AdProvider based on the primaryAdPlatform - Includes checks for ad configuration and logs relevant information and warnings
1 parent e52fe6a commit 2ceb028

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

lib/ads/ad_service.dart

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class AdService {
2525
AdService({
2626
required Map<AdPlatformType, AdProvider> adProviders,
2727
Logger? logger,
28-
}) : _adProviders = adProviders,
29-
_logger = logger ?? Logger('AdService');
28+
}) : _adProviders = adProviders,
29+
_logger = logger ?? Logger('AdService');
3030

3131
final Map<AdPlatformType, AdProvider> _adProviders;
3232
final Logger _logger;
@@ -220,4 +220,98 @@ class AdService {
220220
return null;
221221
}
222222
}
223+
224+
/// Retrieves a loaded inline ad (native or banner) for an in-article placement.
225+
///
226+
/// This method delegates ad loading to the appropriate [AdProvider] based on
227+
/// the [adConfig]'s `primaryAdPlatform` and the `defaultInArticleAdType`
228+
/// from the `articleAdConfiguration`.
229+
///
230+
/// Returns an [InlineAd] if an ad is available, otherwise `null`.
231+
///
232+
/// - [adConfig]: The remote configuration for ad display rules.
233+
/// - [adThemeStyle]: UI-agnostic theme properties for ad styling.
234+
Future<InlineAd?> getInArticleAd({
235+
required AdConfig adConfig,
236+
required AdThemeStyle adThemeStyle,
237+
}) async {
238+
// Check if ads are globally enabled and specifically for articles.
239+
if (!adConfig.enabled || !adConfig.articleAdConfiguration.enabled) {
240+
_logger.info(
241+
'In-article ads are disabled in RemoteConfig, either globally or for articles.',
242+
);
243+
return null;
244+
}
245+
246+
final primaryAdPlatform = adConfig.primaryAdPlatform;
247+
final adProvider = _adProviders[primaryAdPlatform];
248+
249+
if (adProvider == null) {
250+
_logger.warning('No AdProvider found for platform: $primaryAdPlatform');
251+
return null;
252+
}
253+
254+
final platformAdIdentifiers =
255+
adConfig.platformAdIdentifiers[primaryAdPlatform];
256+
if (platformAdIdentifiers == null) {
257+
_logger.warning(
258+
'No AdPlatformIdentifiers found for platform: $primaryAdPlatform',
259+
);
260+
return null;
261+
}
262+
263+
// Determine the ad type and ID from the article-specific configuration.
264+
final articleAdConfig = adConfig.articleAdConfiguration;
265+
final adType = articleAdConfig.defaultInArticleAdType;
266+
final adId = switch (adType) {
267+
AdType.native => platformAdIdentifiers.inArticleNativeAdId,
268+
AdType.banner => platformAdIdentifiers.inArticleBannerAdId,
269+
_ => null, // Should not happen due to AdConfig assertion
270+
};
271+
272+
if (adId == null || adId.isEmpty) {
273+
_logger.warning(
274+
'No in-article ad ID configured for platform $primaryAdPlatform and ad type $adType',
275+
);
276+
return null;
277+
}
278+
279+
_logger.info(
280+
'Requesting in-article $adType ad from $primaryAdPlatform AdProvider with ID: $adId',
281+
);
282+
try {
283+
InlineAd? loadedAd;
284+
switch (adType) {
285+
case AdType.native:
286+
loadedAd = await adProvider.loadNativeAd(
287+
adPlatformIdentifiers: platformAdIdentifiers,
288+
adId: adId,
289+
adThemeStyle: adThemeStyle,
290+
);
291+
case AdType.banner:
292+
loadedAd = await adProvider.loadBannerAd(
293+
adPlatformIdentifiers: platformAdIdentifiers,
294+
adId: adId,
295+
adThemeStyle: adThemeStyle,
296+
);
297+
case AdType.interstitial:
298+
case AdType.video:
299+
_logger.warning(
300+
'Attempted to load $adType ad using getInArticleAd. This is not supported.',
301+
);
302+
return null;
303+
}
304+
305+
if (loadedAd != null) {
306+
_logger.info('In-article $adType ad successfully loaded.');
307+
return loadedAd;
308+
} else {
309+
_logger.info('No in-article $adType ad loaded by AdProvider.');
310+
return null;
311+
}
312+
} catch (e) {
313+
_logger.severe('Error getting in-article $adType ad from AdProvider: $e');
314+
return null;
315+
}
316+
}
223317
}

0 commit comments

Comments
 (0)