Skip to content

Commit d735ea8

Browse files
authored
Merge pull request #114 from flutter-news-app-full-source-code/in-article-ads-fix-and-logging-enhancements
2 parents d2de638 + 89875e5 commit d735ea8

10 files changed

+297
-149
lines changed

lib/ads/ad_navigator_observer.dart

Lines changed: 75 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,40 @@ 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(
138+
'AdNavigatorObserver: RemoteConfig is null. Cannot check ad enablement.',
139+
);
140+
return;
141+
}
142+
if (!remoteConfig.adConfig.enabled) {
143+
_logger.info(
144+
'AdNavigatorObserver: Ads are globally disabled in RemoteConfig.',
145+
);
146+
return;
147+
}
148+
if (!remoteConfig.adConfig.interstitialAdConfiguration.enabled) {
149+
_logger.info(
150+
'AdNavigatorObserver: Interstitial ads are disabled in RemoteConfig.',
151+
);
136152
return;
137153
}
138154

139155
// Only increment count if the transition is eligible for an interstitial ad.
140156
if (_isEligibleForInterstitialAd(currentRouteName)) {
141157
_pageTransitionCount++;
142158
_logger.info(
143-
'Eligible page transition. Current count: $_pageTransitionCount',
159+
'AdNavigatorObserver: Eligible page transition. Current count: $_pageTransitionCount',
144160
);
145161
} else {
146162
_logger.info(
147-
'Ineligible page transition. Count remains: $_pageTransitionCount',
163+
'AdNavigatorObserver: Ineligible page transition. Count remains: $_pageTransitionCount',
148164
);
149165
return; // Do not proceed if not an eligible transition
150166
}
@@ -173,42 +189,57 @@ class AdNavigatorObserver extends NavigatorObserver {
173189
}
174190

175191
_logger.info(
176-
'Required transitions for user role ${user?.appRole}: $requiredTransitions. '
192+
'AdNavigatorObserver: Required transitions for user role ${user?.appRole}: $requiredTransitions. '
177193
'Current eligible transitions: $_pageTransitionCount',
178194
);
179195

180196
// Check if it's time to show an interstitial ad.
181197
if (requiredTransitions > 0 &&
182198
_pageTransitionCount >= requiredTransitions) {
183-
_logger.info('Interstitial ad due. Requesting ad.');
199+
_logger.info('AdNavigatorObserver: Interstitial ad due. Requesting ad.');
184200
unawaited(_showInterstitialAd()); // Use unawaited to not block navigation
185201
// Reset count only after an ad is due (whether it shows or fails)
186202
_pageTransitionCount = 0;
203+
} else {
204+
_logger.info(
205+
'AdNavigatorObserver: Interstitial ad not yet due. '
206+
'Required: $requiredTransitions, Current: $_pageTransitionCount',
207+
);
187208
}
188209
}
189210

190211
/// Requests and shows an interstitial ad if conditions are met.
191212
Future<void> _showInterstitialAd() async {
213+
_logger.info('AdNavigatorObserver: Attempting to show interstitial ad.');
192214
final appState = appStateProvider();
193215
final appEnvironment = appState.environment;
194216
final remoteConfig = appState.remoteConfig;
195217

196218
// In demo environment, display a placeholder interstitial ad directly.
197219
if (appEnvironment == AppEnvironment.demo) {
198-
_logger.info('Demo environment: Showing placeholder interstitial ad.');
220+
_logger.info(
221+
'AdNavigatorObserver: Demo environment: Showing placeholder interstitial ad.',
222+
);
223+
if (navigator?.context == null) {
224+
_logger.severe(
225+
'AdNavigatorObserver: Navigator context is null. Cannot show demo interstitial ad.',
226+
);
227+
return;
228+
}
199229
await showDialog<void>(
200230
context: navigator!.context,
201231
builder: (context) => const DemoInterstitialAdDialog(),
202232
);
233+
_logger.info('AdNavigatorObserver: Placeholder interstitial ad shown.');
203234
return;
204235
}
205236

206237
// For other environments (development, production), proceed with real ad loading.
207238
// This is a secondary check. The primary check is in _handlePageTransition.
208239
if (remoteConfig == null || !remoteConfig.adConfig.enabled) {
209240
_logger.warning(
210-
'Interstitial ads disabled or remote config not available. '
211-
'This should have been caught earlier.',
241+
'AdNavigatorObserver: Interstitial ads disabled or remote config not available. '
242+
'This should have been caught earlier in _handlePageTransition.',
212243
);
213244
return;
214245
}
@@ -218,52 +249,73 @@ class AdNavigatorObserver extends NavigatorObserver {
218249

219250
if (!interstitialConfig.enabled) {
220251
_logger.warning(
221-
'Interstitial ads are specifically disabled in config. '
222-
'This should have been caught earlier.',
252+
'AdNavigatorObserver: Interstitial ads are specifically disabled in config. '
253+
'This should have been caught earlier in _handlePageTransition.',
223254
);
224255
return;
225256
}
226257

227-
_logger.info('Attempting to load interstitial ad...');
258+
_logger.info(
259+
'AdNavigatorObserver: Requesting interstitial ad from AdService...',
260+
);
228261
final interstitialAd = await adService.getInterstitialAd(
229262
adConfig: adConfig,
230263
adThemeStyle: _adThemeStyle,
231264
);
232265

233266
if (interstitialAd != null) {
234-
_logger.info('Interstitial ad loaded. Showing...');
267+
_logger.info('AdNavigatorObserver: Interstitial ad loaded. Showing...');
268+
if (navigator?.context == null) {
269+
_logger.severe(
270+
'AdNavigatorObserver: Navigator context is null. Cannot show interstitial ad.',
271+
);
272+
return;
273+
}
235274
// Show the AdMob interstitial ad.
236275
if (interstitialAd.provider == AdPlatformType.admob &&
237276
interstitialAd.adObject is admob.InterstitialAd) {
277+
_logger.info('AdNavigatorObserver: Showing AdMob interstitial ad.');
238278
final admobInterstitialAd =
239279
interstitialAd.adObject as admob.InterstitialAd
240280
..fullScreenContentCallback = admob.FullScreenContentCallback(
241281
onAdDismissedFullScreenContent: (ad) {
242-
_logger.info('Interstitial Ad dismissed.');
282+
_logger.info(
283+
'AdNavigatorObserver: AdMob Interstitial Ad dismissed.',
284+
);
243285
ad.dispose();
244286
},
245287
onAdFailedToShowFullScreenContent: (ad, error) {
246-
_logger.severe('Interstitial Ad failed to show: $error');
288+
_logger.severe(
289+
'AdNavigatorObserver: AdMob Interstitial Ad failed to show: $error',
290+
);
247291
ad.dispose();
248292
},
249293
onAdShowedFullScreenContent: (ad) {
250-
_logger.info('Interstitial Ad showed.');
294+
_logger.info(
295+
'AdNavigatorObserver: AdMob Interstitial Ad showed.',
296+
);
251297
},
252298
);
253299
await admobInterstitialAd.show();
254300
} else if (interstitialAd.provider == AdPlatformType.local &&
255301
interstitialAd.adObject is LocalInterstitialAd) {
256-
_logger.info('Showing local interstitial ad.');
302+
_logger.info('AdNavigatorObserver: Showing local interstitial ad.');
257303
await showDialog<void>(
258304
context: navigator!.context,
259305
builder: (context) => LocalInterstitialAdDialog(
260306
localInterstitialAd: interstitialAd.adObject as LocalInterstitialAd,
261307
),
262308
);
309+
_logger.info('AdNavigatorObserver: Local interstitial ad shown.');
310+
} else {
311+
_logger.warning(
312+
'AdNavigatorObserver: Loaded interstitial ad has unknown provider '
313+
'or adObject type: ${interstitialAd.provider}, ${interstitialAd.adObject.runtimeType}',
314+
);
263315
}
264316
} else {
265317
_logger.warning(
266-
'No interstitial ad loaded by AdService, even though one was due. '
318+
'AdNavigatorObserver: No interstitial ad loaded by AdService, even though one was due. '
267319
'Check AdService implementation and ad unit availability.',
268320
);
269321
}

0 commit comments

Comments
 (0)