Skip to content

Commit 6144b60

Browse files
committed
feat(ads): implement interstitial ads with navigator observer
- Add AdNavigatorObserver for handling interstitial ads on route changes - Subscribe to interstitial ad stream in AppBloc - Initialize AdNavigatorObserver in _AppViewState - Pass AdNavigatorObserver to AdService - Manage interstitial ad subscription lifecycle
1 parent ccb10de commit 6144b60

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

lib/app/view/app.dart

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
77
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_service.dart';
88
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
99
import 'package:flutter_news_app_mobile_client_full_source_code/app/config/app_environment.dart';
10+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_navigator_observer.dart';
11+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart';
1012
import 'package:flutter_news_app_mobile_client_full_source_code/app/services/app_status_service.dart';
1113
import 'package:flutter_news_app_mobile_client_full_source_code/app/services/demo_data_initializer_service.dart';
1214
import 'package:flutter_news_app_mobile_client_full_source_code/app/services/demo_data_migration_service.dart';
@@ -17,6 +19,7 @@ import 'package:flutter_news_app_mobile_client_full_source_code/status/view/view
1719
import 'package:go_router/go_router.dart';
1820
import 'package:kv_storage_service/kv_storage_service.dart';
1921
import 'package:ui_kit/ui_kit.dart';
22+
import 'dart:async';
2023

2124
class App extends StatelessWidget {
2225
const App({
@@ -167,7 +170,10 @@ class _AppViewState extends State<_AppView> {
167170
late final ValueNotifier<AppStatus> _statusNotifier;
168171
// The service responsible for automated status checks.
169172
AppStatusService? _appStatusService;
170-
// Removed Dynamic Links subscription
173+
// The observer for handling interstitial ads on route changes.
174+
AdNavigatorObserver? _adNavigatorObserver;
175+
// Stream subscription for interstitial ad signals from AppBloc.
176+
StreamSubscription<void>? _interstitialAdSubscription;
171177

172178
@override
173179
void initState() {
@@ -185,6 +191,22 @@ class _AppViewState extends State<_AppView> {
185191
environment: widget.environment,
186192
);
187193

194+
// Derive AdThemeStyle from the current theme.
195+
final adThemeStyle = AdThemeStyle.fromTheme(Theme.of(context));
196+
197+
// Initialize AdNavigatorObserver.
198+
_adNavigatorObserver = AdNavigatorObserver(
199+
appBloc: appBloc,
200+
adService: widget.adService,
201+
adThemeStyle: adThemeStyle,
202+
);
203+
204+
// Subscribe to the AppBloc's interstitial ad stream.
205+
_interstitialAdSubscription =
206+
appBloc.showInterstitialAdStream.listen((_) {
207+
_adNavigatorObserver?.showInterstitialAd();
208+
});
209+
188210
_router = createRouter(
189211
authStatusNotifier: _statusNotifier,
190212
authenticationRepository: widget.authenticationRepository,
@@ -199,17 +221,19 @@ class _AppViewState extends State<_AppView> {
199221
environment: widget.environment,
200222
adService: widget.adService,
201223
localAdRepository: widget.localAdRepository,
224+
adNavigatorObserver: _adNavigatorObserver!, // Pass the observer
202225
);
203-
204-
// Removed Dynamic Link Initialization
205226
}
206227

207228
@override
208229
void dispose() {
209230
_statusNotifier.dispose();
210231
// Dispose the AppStatusService to cancel timers and remove observers.
211232
_appStatusService?.dispose();
212-
// Removed Dynamic Links subscription cancellation
233+
// Cancel the interstitial ad stream subscription.
234+
_interstitialAdSubscription?.cancel();
235+
// AdNavigatorObserver does not need explicit dispose here as it's a NavigatorObserver
236+
// and its internal resources are managed by the AdService/AdMob SDK.
213237
super.dispose();
214238
}
215239

0 commit comments

Comments
 (0)