Skip to content

Commit ccb10de

Browse files
committed
refactor(ads): improve ad navigator observer and interstitial ad handling
- Refactor AdNavigatorObserver to use AdThemeStyle and handle only PageRoutes - Add InterstitialAd model for better ad management - Remove unnecessary dependencies and simplify ad loading process - Update logging and ad display logic
1 parent bb69ef5 commit ccb10de

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

lib/ads/ad_navigator_observer.dart

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import 'dart:async';
22

33
import 'package:core/core.dart';
44
import 'package:flutter/material.dart';
5-
import 'package:flutter_bloc/flutter_bloc.dart';
65
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_service.dart';
76
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart';
7+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/interstitial_ad.dart';
88
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
99
import 'package:logging/logging.dart';
10+
import 'package:google_mobile_ads/google_mobile_ads.dart' as admob;
1011

1112
/// {@template ad_navigator_observer}
1213
/// A [NavigatorObserver] that listens to route changes and triggers
@@ -25,39 +26,36 @@ class AdNavigatorObserver extends NavigatorObserver {
2526
AdNavigatorObserver({
2627
required this.appBloc,
2728
required this.adService,
29+
required AdThemeStyle adThemeStyle,
2830
Logger? logger,
29-
}) : _logger = logger ?? Logger('AdNavigatorObserver');
31+
}) : _logger = logger ?? Logger('AdNavigatorObserver'),
32+
_adThemeStyle = adThemeStyle;
3033

3134
final AppBloc appBloc;
3235
final AdService adService;
3336
final Logger _logger;
34-
35-
/// Tracks the number of page transitions since the last interstitial ad was shown.
36-
/// This is managed by the AppBloc.
37+
final AdThemeStyle _adThemeStyle;
3738

3839
@override
3940
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
4041
super.didPush(route, previousRoute);
4142
_logger.info('Route pushed: ${route.settings.name}');
42-
_handlePageTransition();
43+
_handlePageTransition(route);
4344
}
4445

4546
@override
4647
void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
4748
super.didPop(route, previousRoute);
4849
_logger.info('Route popped: ${route.settings.name}');
49-
_handlePageTransition();
50+
_handlePageTransition(route);
5051
}
5152

5253
/// Handles a page transition event.
5354
///
5455
/// Dispatches an [AppPageTransitioned] event to the [AppBloc] to update
5556
/// the transition count and potentially trigger an interstitial ad.
56-
void _handlePageTransition() {
57-
// Only consider actual page routes, not overlays or dialogs.
58-
// This check is a simplification; a more robust solution might inspect
59-
// route types or use GoRouter's specific navigation events.
60-
if (navigator?.currentRoutes.last.settings.name != null) {
57+
void _handlePageTransition(Route<dynamic> route) {
58+
if (route is PageRoute && route.settings.name != null) {
6159
appBloc.add(const AppPageTransitioned());
6260
}
6361
}
@@ -67,7 +65,6 @@ class AdNavigatorObserver extends NavigatorObserver {
6765
/// This method is called by the [AppBloc] when it determines an ad is due.
6866
Future<void> showInterstitialAd() async {
6967
final remoteConfig = appBloc.state.remoteConfig;
70-
final user = appBloc.state.user;
7168

7269
if (remoteConfig == null || !remoteConfig.adConfig.enabled) {
7370
_logger.info('Interstitial ads disabled or remote config not available.');
@@ -82,20 +79,10 @@ class AdNavigatorObserver extends NavigatorObserver {
8279
return;
8380
}
8481

85-
// Create AdThemeStyle from current theme for ad loading.
86-
// This requires a BuildContext, which is not directly available here.
87-
// The AppBloc will need to provide the AdThemeStyle or the AdService
88-
// will need to be able to create it. For now, we'll assume a default
89-
// or that the AdService can handle it.
90-
//
91-
// TODO(fulleni): Revisit how AdThemeStyle is passed for interstitial ads.
92-
// For now, a placeholder AdThemeStyle is used.
93-
final adThemeStyle = AdThemeStyle.fromTheme(ThemeData());
94-
9582
_logger.info('Attempting to load interstitial ad...');
9683
final interstitialAd = await adService.getInterstitialAd(
9784
adConfig: adConfig,
98-
adThemeStyle: adThemeStyle,
85+
adThemeStyle: _adThemeStyle,
9986
);
10087

10188
if (interstitialAd != null) {

0 commit comments

Comments
 (0)