Skip to content

Commit aa89234

Browse files
committed
refactor(ads): improve AdNavigatorObserver logging and ad display logic
- Prefix all log messages with "AdNavigatorObserver:" for better log filtering - Enhance log details in _handlePageTransition for better debugging - Add checks for null navigator context before attempting to show ads - Add warning logs for unexpected conditions to catch potential issues early - Refactor ad display logic to handle null or unexpected ad types
1 parent b1ad736 commit aa89234

File tree

1 file changed

+55
-23
lines changed

1 file changed

+55
-23
lines changed

lib/ads/ad_navigator_observer.dart

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class AdNavigatorObserver extends NavigatorObserver {
5959
super.didPush(route, previousRoute);
6060
final currentRouteName = route.settings.name;
6161
_logger.info(
62-
'Route pushed: $currentRouteName (Previous: $_previousRouteName)',
62+
'AdNavigatorObserver: Route pushed: $currentRouteName (Previous: $_previousRouteName)',
6363
);
6464
if (route is PageRoute && currentRouteName != null) {
6565
_handlePageTransition(currentRouteName);
@@ -74,7 +74,7 @@ class AdNavigatorObserver extends NavigatorObserver {
7474
?.settings
7575
.name; // After pop, previousRoute is the new current
7676
_logger.info(
77-
'Route popped: ${route.settings.name} (New Current: $currentRouteName)',
77+
'AdNavigatorObserver: Route popped: ${route.settings.name} (New Current: $currentRouteName)',
7878
);
7979
if (route is PageRoute && currentRouteName != null) {
8080
_handlePageTransition(currentRouteName);
@@ -114,7 +114,7 @@ class AdNavigatorObserver extends NavigatorObserver {
114114
final isToDetailPage = detailPageRoutes.contains(current);
115115

116116
_logger.info(
117-
'Eligibility check: Previous: $previous (Is Content List: $isFromContentList), '
117+
'AdNavigatorObserver: Eligibility check: Previous: $previous (Is Content List: $isFromContentList), '
118118
'Current: $current (Is Detail Page: $isToDetailPage)',
119119
);
120120

@@ -127,24 +127,34 @@ class AdNavigatorObserver extends NavigatorObserver {
127127
final remoteConfig = appState.remoteConfig;
128128
final user = appState.user;
129129

130+
_logger.info(
131+
'AdNavigatorObserver: _handlePageTransition called for route: $currentRouteName',
132+
);
133+
130134
// Only proceed if remote config is available, ads are globally enabled,
131135
// and interstitial ads are enabled in the config.
132-
if (remoteConfig == null ||
133-
!remoteConfig.adConfig.enabled ||
134-
!remoteConfig.adConfig.interstitialAdConfiguration.enabled) {
135-
_logger.info('Interstitial ads are not enabled or config not ready.');
136+
if (remoteConfig == null) {
137+
_logger.warning('AdNavigatorObserver: RemoteConfig is null. Cannot check ad enablement.');
138+
return;
139+
}
140+
if (!remoteConfig.adConfig.enabled) {
141+
_logger.info('AdNavigatorObserver: Ads are globally disabled in RemoteConfig.');
142+
return;
143+
}
144+
if (!remoteConfig.adConfig.interstitialAdConfiguration.enabled) {
145+
_logger.info('AdNavigatorObserver: Interstitial ads are disabled in RemoteConfig.');
136146
return;
137147
}
138148

139149
// Only increment count if the transition is eligible for an interstitial ad.
140150
if (_isEligibleForInterstitialAd(currentRouteName)) {
141151
_pageTransitionCount++;
142152
_logger.info(
143-
'Eligible page transition. Current count: $_pageTransitionCount',
153+
'AdNavigatorObserver: Eligible page transition. Current count: $_pageTransitionCount',
144154
);
145155
} else {
146156
_logger.info(
147-
'Ineligible page transition. Count remains: $_pageTransitionCount',
157+
'AdNavigatorObserver: Ineligible page transition. Count remains: $_pageTransitionCount',
148158
);
149159
return; // Do not proceed if not an eligible transition
150160
}
@@ -173,42 +183,53 @@ class AdNavigatorObserver extends NavigatorObserver {
173183
}
174184

175185
_logger.info(
176-
'Required transitions for user role ${user?.appRole}: $requiredTransitions. '
186+
'AdNavigatorObserver: Required transitions for user role ${user?.appRole}: $requiredTransitions. '
177187
'Current eligible transitions: $_pageTransitionCount',
178188
);
179189

180190
// Check if it's time to show an interstitial ad.
181191
if (requiredTransitions > 0 &&
182192
_pageTransitionCount >= requiredTransitions) {
183-
_logger.info('Interstitial ad due. Requesting ad.');
193+
_logger.info('AdNavigatorObserver: Interstitial ad due. Requesting ad.');
184194
unawaited(_showInterstitialAd()); // Use unawaited to not block navigation
185195
// Reset count only after an ad is due (whether it shows or fails)
186196
_pageTransitionCount = 0;
197+
} else {
198+
_logger.info(
199+
'AdNavigatorObserver: Interstitial ad not yet due. '
200+
'Required: $requiredTransitions, Current: $_pageTransitionCount',
201+
);
187202
}
188203
}
189204

190205
/// Requests and shows an interstitial ad if conditions are met.
191206
Future<void> _showInterstitialAd() async {
207+
_logger.info('AdNavigatorObserver: Attempting to show interstitial ad.');
192208
final appState = appStateProvider();
193209
final appEnvironment = appState.environment;
194210
final remoteConfig = appState.remoteConfig;
195211

196212
// In demo environment, display a placeholder interstitial ad directly.
197213
if (appEnvironment == AppEnvironment.demo) {
198-
_logger.info('Demo environment: Showing placeholder interstitial ad.');
214+
_logger.info('AdNavigatorObserver: Demo environment: Showing placeholder interstitial ad.');
215+
if (navigator?.context == null) {
216+
_logger.severe('AdNavigatorObserver: Navigator context is null. Cannot show demo interstitial ad.');
217+
return;
218+
}
199219
await showDialog<void>(
200220
context: navigator!.context,
201221
builder: (context) => const DemoInterstitialAdDialog(),
202222
);
223+
_logger.info('AdNavigatorObserver: Placeholder interstitial ad shown.');
203224
return;
204225
}
205226

206227
// For other environments (development, production), proceed with real ad loading.
207228
// This is a secondary check. The primary check is in _handlePageTransition.
208229
if (remoteConfig == null || !remoteConfig.adConfig.enabled) {
209230
_logger.warning(
210-
'Interstitial ads disabled or remote config not available. '
211-
'This should have been caught earlier.',
231+
'AdNavigatorObserver: Interstitial ads disabled or remote config not available. '
232+
'This should have been caught earlier in _handlePageTransition.',
212233
);
213234
return;
214235
}
@@ -218,52 +239,63 @@ class AdNavigatorObserver extends NavigatorObserver {
218239

219240
if (!interstitialConfig.enabled) {
220241
_logger.warning(
221-
'Interstitial ads are specifically disabled in config. '
222-
'This should have been caught earlier.',
242+
'AdNavigatorObserver: Interstitial ads are specifically disabled in config. '
243+
'This should have been caught earlier in _handlePageTransition.',
223244
);
224245
return;
225246
}
226247

227-
_logger.info('Attempting to load interstitial ad...');
248+
_logger.info('AdNavigatorObserver: Requesting interstitial ad from AdService...');
228249
final interstitialAd = await adService.getInterstitialAd(
229250
adConfig: adConfig,
230251
adThemeStyle: _adThemeStyle,
231252
);
232253

233254
if (interstitialAd != null) {
234-
_logger.info('Interstitial ad loaded. Showing...');
255+
_logger.info('AdNavigatorObserver: Interstitial ad loaded. Showing...');
256+
if (navigator?.context == null) {
257+
_logger.severe('AdNavigatorObserver: Navigator context is null. Cannot show interstitial ad.');
258+
return;
259+
}
235260
// Show the AdMob interstitial ad.
236261
if (interstitialAd.provider == AdPlatformType.admob &&
237262
interstitialAd.adObject is admob.InterstitialAd) {
263+
_logger.info('AdNavigatorObserver: Showing AdMob interstitial ad.');
238264
final admobInterstitialAd =
239265
interstitialAd.adObject as admob.InterstitialAd
240266
..fullScreenContentCallback = admob.FullScreenContentCallback(
241267
onAdDismissedFullScreenContent: (ad) {
242-
_logger.info('Interstitial Ad dismissed.');
268+
_logger.info('AdNavigatorObserver: AdMob Interstitial Ad dismissed.');
243269
ad.dispose();
244270
},
245271
onAdFailedToShowFullScreenContent: (ad, error) {
246-
_logger.severe('Interstitial Ad failed to show: $error');
272+
_logger.severe('AdNavigatorObserver: AdMob Interstitial Ad failed to show: $error');
247273
ad.dispose();
248274
},
249275
onAdShowedFullScreenContent: (ad) {
250-
_logger.info('Interstitial Ad showed.');
276+
_logger.info('AdNavigatorObserver: AdMob Interstitial Ad showed.');
251277
},
252278
);
253279
await admobInterstitialAd.show();
254280
} else if (interstitialAd.provider == AdPlatformType.local &&
255281
interstitialAd.adObject is LocalInterstitialAd) {
256-
_logger.info('Showing local interstitial ad.');
282+
_logger.info('AdNavigatorObserver: Showing local interstitial ad.');
257283
await showDialog<void>(
258284
context: navigator!.context,
259285
builder: (context) => LocalInterstitialAdDialog(
260286
localInterstitialAd: interstitialAd.adObject as LocalInterstitialAd,
261287
),
262288
);
289+
_logger.info('AdNavigatorObserver: Local interstitial ad shown.');
290+
} else {
291+
_logger.warning(
292+
'AdNavigatorObserver: Loaded interstitial ad has unknown provider '
293+
'or adObject type: ${interstitialAd.provider}, ${interstitialAd.adObject.runtimeType}',
294+
);
263295
}
264296
} else {
265297
_logger.warning(
266-
'No interstitial ad loaded by AdService, even though one was due. '
298+
'AdNavigatorObserver: No interstitial ad loaded by AdService, even though one was due. '
267299
'Check AdService implementation and ad unit availability.',
268300
);
269301
}

0 commit comments

Comments
 (0)