Skip to content

Commit f754f0c

Browse files
committed
fix(ads): make InterstitialAdManager's onPotentialAdTrigger awaitable
- Add return type [Future<void>] to onPotentialAdTrigger for better navigation control - Implement awaiting ad dismissal in _showAdMobAd by using a Completer - Update _showAd to return the result of _showAdMobAd - Improve documentation for ad showing methods
1 parent 73f7216 commit f754f0c

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

lib/ads/interstitial_ad_manager.dart

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class InterstitialAdManager {
2626
required AppBloc appBloc,
2727
required AdService adService,
2828
Logger? logger,
29-
}) : _appBloc = appBloc,
30-
_adService = adService,
31-
_logger = logger ?? Logger('InterstitialAdManager') {
29+
}) : _appBloc = appBloc,
30+
_adService = adService,
31+
_logger = logger ?? Logger('InterstitialAdManager') {
3232
// Listen to the AppBloc stream to react to state changes.
3333
_appBlocSubscription = _appBloc.stream.listen(_onAppStateChanged);
3434
// Initialize with the current state.
@@ -100,8 +100,8 @@ class InterstitialAdManager {
100100
final brightness = appState.themeMode == ThemeMode.system
101101
? SchedulerBinding.instance.window.platformBrightness
102102
: (appState.themeMode == ThemeMode.dark
103-
? Brightness.dark
104-
: Brightness.light);
103+
? Brightness.dark
104+
: Brightness.light);
105105

106106
// Create a ThemeData instance from the AppState's settings.
107107
// This allows us to derive AdThemeStyle without a BuildContext.
@@ -153,6 +153,9 @@ class InterstitialAdManager {
153153
///
154154
/// This method increments the transition counter and shows a pre-loaded ad
155155
/// if the frequency criteria are met.
156+
///
157+
/// Returns a [Future] that completes when the ad is dismissed, allowing the
158+
/// caller to await the ad's lifecycle before proceeding with navigation.
156159
Future<void> onPotentialAdTrigger() async {
157160
_transitionCount++;
158161
_logger.info('Potential ad trigger. Transition count: $_transitionCount');
@@ -180,6 +183,8 @@ class InterstitialAdManager {
180183
}
181184

182185
/// Shows the pre-loaded interstitial ad.
186+
///
187+
/// Returns a [Future] that completes when the ad is dismissed.
183188
Future<void> _showAd() async {
184189
if (_preloadedAd == null) {
185190
_logger.warning(
@@ -234,20 +239,30 @@ class InterstitialAdManager {
234239

235240
Future<void> _showAdMobAd(InterstitialAd ad) async {
236241
if (ad.adObject is! admob.InterstitialAd) return;
242+
243+
final completer = Completer<void>();
237244
final admobAd = ad.adObject as admob.InterstitialAd
238245
..fullScreenContentCallback = admob.FullScreenContentCallback(
239246
onAdShowedFullScreenContent: (ad) =>
240247
_logger.info('AdMob ad showed full screen.'),
241248
onAdDismissedFullScreenContent: (ad) {
242249
_logger.info('AdMob ad dismissed.');
243250
ad.dispose();
251+
if (!completer.isCompleted) {
252+
completer.complete();
253+
}
244254
},
245255
onAdFailedToShowFullScreenContent: (ad, error) {
246256
_logger.severe('AdMob ad failed to show: $error');
247257
ad.dispose();
258+
if (!completer.isCompleted) {
259+
// Complete normally even on failure to unblock navigation.
260+
completer.complete();
261+
}
248262
},
249263
);
250264
await admobAd.show();
265+
return completer.future;
251266
}
252267

253268
Future<void> _showLocalAd(BuildContext context, InterstitialAd ad) async {

0 commit comments

Comments
 (0)