Skip to content

Commit 212b5ea

Browse files
committed
feat(ads): enhance AdMob ad provider with dynamic ad sizing
- Add support for large thumbnail headline image style - Implement conditional logic to select ad template and size based on headline image style - Update native and banner ad loading methods to accommodate new ad sizes - Improve code readability and maintainability
1 parent e5cf932 commit 212b5ea

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

lib/ads/admob_ad_provider.dart

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import 'dart:async';
33
import 'package:core/core.dart';
44
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_provider.dart';
55
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart';
6-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/banner_ad.dart'; // Import the new BannerAd model
6+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/banner_ad.dart';
77
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/interstitial_ad.dart';
8-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/native_ad.dart'; // Import the refactored NativeAd model
8+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/native_ad.dart';
99
import 'package:google_mobile_ads/google_mobile_ads.dart' as admob;
1010
import 'package:logging/logging.dart';
1111
import 'package:uuid/uuid.dart';
@@ -46,6 +46,7 @@ class AdMobAdProvider implements AdProvider {
4646
required AdPlatformIdentifiers adPlatformIdentifiers,
4747
required String? adId,
4848
required AdThemeStyle adThemeStyle,
49+
HeadlineImageStyle? headlineImageStyle,
4950
}) async {
5051
if (adId == null || adId.isEmpty) {
5152
_logger.warning('No native ad unit ID provided for AdMob.');
@@ -54,7 +55,12 @@ class AdMobAdProvider implements AdProvider {
5455

5556
_logger.info('Attempting to load native ad from unit ID: $adId');
5657

57-
const templateType = NativeAdTemplateType.medium; // Default to medium for native
58+
// Determine the template type based on the user's feed style preference.
59+
// Use largeThumbnail for a more prominent, square-like ad.
60+
final templateType =
61+
headlineImageStyle == HeadlineImageStyle.largeThumbnail
62+
? NativeAdTemplateType.medium
63+
: NativeAdTemplateType.small;
5864

5965
final completer = Completer<admob.NativeAd?>();
6066

@@ -128,6 +134,7 @@ class AdMobAdProvider implements AdProvider {
128134
required AdPlatformIdentifiers adPlatformIdentifiers,
129135
required String? adId,
130136
required AdThemeStyle adThemeStyle,
137+
HeadlineImageStyle? headlineImageStyle,
131138
}) async {
132139
if (adId == null || adId.isEmpty) {
133140
_logger.warning('No banner ad unit ID provided for AdMob.');
@@ -136,11 +143,17 @@ class AdMobAdProvider implements AdProvider {
136143

137144
_logger.info('Attempting to load banner ad from unit ID: $adId');
138145

146+
// Determine the ad size based on the user's feed style preference.
147+
// Use mediumRectangle for a more square-like ad with large thumbnails.
148+
final adSize = headlineImageStyle == HeadlineImageStyle.largeThumbnail
149+
? admob.AdSize.mediumRectangle
150+
: admob.AdSize.banner;
151+
139152
final completer = Completer<admob.BannerAd?>();
140153

141154
final ad = admob.BannerAd(
142155
adUnitId: adId,
143-
size: admob.AdSize.banner, // Default banner size
156+
size: adSize,
144157
request: const admob.AdRequest(),
145158
listener: admob.BannerAdListener(
146159
onAdLoaded: (ad) {

0 commit comments

Comments
 (0)