@@ -51,29 +51,78 @@ class AdNavigatorObserver extends NavigatorObserver {
5151 /// Tracks the number of page transitions since the last interstitial ad.
5252 int _pageTransitionCount = 0 ;
5353
54+ /// Stores the name of the previous route.
55+ String ? _previousRouteName;
56+
5457 @override
5558 void didPush (Route <dynamic > route, Route <dynamic >? previousRoute) {
5659 super .didPush (route, previousRoute);
57- _logger.info ('Route pushed: ${route .settings .name }' );
58- if (route is PageRoute && route.settings.name != null ) {
59- _handlePageTransition ();
60+ final currentRouteName = route.settings.name;
61+ _logger.info (
62+ 'Route pushed: $currentRouteName (Previous: $_previousRouteName )' ,
63+ );
64+ if (route is PageRoute && currentRouteName != null ) {
65+ _handlePageTransition (currentRouteName);
6066 }
67+ _previousRouteName = currentRouteName;
6168 }
6269
6370 @override
6471 void didPop (Route <dynamic > route, Route <dynamic >? previousRoute) {
6572 super .didPop (route, previousRoute);
66- _logger.info ('Route popped: ${route .settings .name }' );
67- if (route is PageRoute && route.settings.name != null ) {
68- _handlePageTransition ();
73+ final currentRouteName = previousRoute
74+ ? .settings
75+ .name; // After pop, previousRoute is the new current
76+ _logger.info (
77+ 'Route popped: ${route .settings .name } (New Current: $currentRouteName )' ,
78+ );
79+ if (route is PageRoute && currentRouteName != null ) {
80+ _handlePageTransition (currentRouteName);
6981 }
82+ _previousRouteName = currentRouteName;
7083 }
7184
72- /// Handles a page transition event, checks ad frequency, and shows an ad if needed.
73- void _handlePageTransition () {
74- _pageTransitionCount++ ;
75- _logger.info ('Page transitioned. Current count: $_pageTransitionCount ' );
85+ /// Determines if a route transition is eligible for an interstitial ad.
86+ ///
87+ /// An ad is considered eligible if the transition is from a content list
88+ /// (e.g., feed, search) to a detail page (e.g., article, entity details).
89+ bool _isEligibleForInterstitialAd (String currentRouteName) {
90+ // Define content list routes
91+ const contentListRoutes = {
92+ 'feed' ,
93+ 'search' ,
94+ 'followedTopicsList' ,
95+ 'followedSourcesList' ,
96+ 'followedCountriesList' ,
97+ 'accountSavedHeadlines' ,
98+ };
99+
100+ // Define detail page routes
101+ const detailPageRoutes = {
102+ 'articleDetails' ,
103+ 'searchArticleDetails' ,
104+ 'accountArticleDetails' ,
105+ 'globalArticleDetails' ,
106+ 'entityDetails' ,
107+ };
108+
109+ final previous = _previousRouteName;
110+ final current = currentRouteName;
111+
112+ final isFromContentList =
113+ previous != null && contentListRoutes.contains (previous);
114+ final isToDetailPage = detailPageRoutes.contains (current);
115+
116+ _logger.info (
117+ 'Eligibility check: Previous: $previous (Is Content List: $isFromContentList ), '
118+ 'Current: $current (Is Detail Page: $isToDetailPage )' ,
119+ );
76120
121+ return isFromContentList && isToDetailPage;
122+ }
123+
124+ /// Handles a page transition event, checks ad frequency, and shows an ad if needed.
125+ void _handlePageTransition (String currentRouteName) {
77126 final appState = appStateProvider ();
78127 final remoteConfig = appState.remoteConfig;
79128 final user = appState.user;
@@ -87,10 +136,23 @@ class AdNavigatorObserver extends NavigatorObserver {
87136 return ;
88137 }
89138
139+ // Only increment count if the transition is eligible for an interstitial ad.
140+ if (_isEligibleForInterstitialAd (currentRouteName)) {
141+ _pageTransitionCount++ ;
142+ _logger.info (
143+ 'Eligible page transition. Current count: $_pageTransitionCount ' ,
144+ );
145+ } else {
146+ _logger.info (
147+ 'Ineligible page transition. Count remains: $_pageTransitionCount ' ,
148+ );
149+ return ; // Do not proceed if not an eligible transition
150+ }
151+
90152 final interstitialConfig =
91153 remoteConfig.adConfig.interstitialAdConfiguration;
92- final frequencyConfig =
93- interstitialConfig .feedInterstitialAdFrequencyConfig;
154+ final frequencyConfig = interstitialConfig
155+ .feedInterstitialAdFrequencyConfig; // Using existing name
94156
95157 // Determine the required transitions based on user role.
96158 final int requiredTransitions;
@@ -111,14 +173,16 @@ class AdNavigatorObserver extends NavigatorObserver {
111173 }
112174
113175 _logger.info (
114- 'Required transitions for user role ${user ?.appRole }: $requiredTransitions ' ,
176+ 'Required transitions for user role ${user ?.appRole }: $requiredTransitions . '
177+ 'Current eligible transitions: $_pageTransitionCount ' ,
115178 );
116179
117180 // Check if it's time to show an interstitial ad.
118181 if (requiredTransitions > 0 &&
119182 _pageTransitionCount >= requiredTransitions) {
120183 _logger.info ('Interstitial ad due. Requesting ad.' );
121- _showInterstitialAd ();
184+ unawaited (_showInterstitialAd ()); // Use unawaited to not block navigation
185+ // Reset count only after an ad is due (whether it shows or fails)
122186 _pageTransitionCount = 0 ;
123187 }
124188 }
@@ -142,15 +206,21 @@ class AdNavigatorObserver extends NavigatorObserver {
142206 // For other environments (development, production), proceed with real ad loading.
143207 // This is a secondary check. The primary check is in _handlePageTransition.
144208 if (remoteConfig == null || ! remoteConfig.adConfig.enabled) {
145- _logger.info ('Interstitial ads disabled or remote config not available.' );
209+ _logger.warning (
210+ 'Interstitial ads disabled or remote config not available. '
211+ 'This should have been caught earlier.' ,
212+ );
146213 return ;
147214 }
148215
149216 final adConfig = remoteConfig.adConfig;
150217 final interstitialConfig = adConfig.interstitialAdConfiguration;
151218
152219 if (! interstitialConfig.enabled) {
153- _logger.info ('Interstitial ads are specifically disabled in config.' );
220+ _logger.warning (
221+ 'Interstitial ads are specifically disabled in config. '
222+ 'This should have been caught earlier.' ,
223+ );
154224 return ;
155225 }
156226
@@ -192,7 +262,10 @@ class AdNavigatorObserver extends NavigatorObserver {
192262 );
193263 }
194264 } else {
195- _logger.info ('No interstitial ad loaded.' );
265+ _logger.warning (
266+ 'No interstitial ad loaded by AdService, even though one was due. '
267+ 'Check AdService implementation and ad unit availability.' ,
268+ );
196269 }
197270 }
198271}
0 commit comments