Skip to content

Commit 4417377

Browse files
committed
refactor(ads): remove BuildContext dependency for interstitial ad theming
- Updated InterstitialAdManager to use AppState for theme information - Replaced BuildContext-based theming with ThemeData creation from AppState - Added logic to handle system/theme-mode brightness detection - Removed navigatorKey dependency for ad theming
1 parent 5909f85 commit 4417377

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

lib/ads/interstitial_ad_manager.dart

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import 'dart:async';
22

33
import 'package:core/core.dart';
44
import 'package:flutter/material.dart';
5+
import 'package:flutter/scheduler.dart';
56
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_service.dart';
67
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart';
78
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/interstitial_ad.dart';
89
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/widgets.dart';
910
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
1011
import 'package:google_mobile_ads/google_mobile_ads.dart' as admob;
1112
import 'package:logging/logging.dart';
13+
import 'package:ui_kit/ui_kit.dart';
1214

1315
/// {@template interstitial_ad_manager}
1416
/// A service that manages the lifecycle of interstitial ads.
@@ -71,12 +73,15 @@ class InterstitialAdManager {
7173
_adConfig = newAdConfig;
7274
_userRole = newUserRole;
7375
// A config change might mean we need to load an ad now.
74-
_maybePreloadAd();
76+
_maybePreloadAd(state);
7577
}
7678
}
7779

7880
/// Pre-loads an interstitial ad if one is not already loaded and conditions are met.
79-
Future<void> _maybePreloadAd() async {
81+
///
82+
/// This method now takes the current [AppState] to derive theme information
83+
/// without needing a [BuildContext].
84+
Future<void> _maybePreloadAd(AppState appState) async {
8085
if (_preloadedAd != null) {
8186
_logger.info('An interstitial ad is already pre-loaded. Skipping.');
8287
return;
@@ -92,16 +97,31 @@ class InterstitialAdManager {
9297

9398
_logger.info('Attempting to pre-load an interstitial ad...');
9499
try {
95-
// We need a BuildContext to get the theme for AdThemeStyle.
96-
// Since this is a service, we get it from the AppBloc's navigatorKey.
97-
final context = _appBloc.navigatorKey.currentContext;
98-
if (context == null) {
99-
_logger.warning(
100-
'BuildContext not available from navigatorKey. Cannot create AdThemeStyle.',
101-
);
102-
return;
103-
}
104-
final adThemeStyle = AdThemeStyle.fromTheme(Theme.of(context));
100+
// Determine the brightness for theme creation.
101+
// If themeMode is system, use platform brightness.
102+
final brightness = appState.themeMode == ThemeMode.system
103+
? SchedulerBinding.instance.window.platformBrightness
104+
: (appState.themeMode == ThemeMode.dark
105+
? Brightness.dark
106+
: Brightness.light);
107+
108+
// Create a ThemeData instance from the AppState's settings.
109+
// This allows us to derive AdThemeStyle without a BuildContext.
110+
final themeData = brightness == Brightness.light
111+
? lightTheme(
112+
scheme: appState.flexScheme,
113+
appTextScaleFactor: appState.settings.displaySettings.textScaleFactor,
114+
appFontWeight: appState.settings.displaySettings.fontWeight,
115+
fontFamily: appState.settings.displaySettings.fontFamily,
116+
)
117+
: darkTheme(
118+
scheme: appState.flexScheme,
119+
appTextScaleFactor: appState.settings.displaySettings.textScaleFactor,
120+
appFontWeight: appState.settings.displaySettings.fontWeight,
121+
fontFamily: appState.settings.displaySettings.fontFamily,
122+
);
123+
124+
final adThemeStyle = AdThemeStyle.fromTheme(themeData);
105125

106126
final ad = await _adService.getInterstitialAd(
107127
adConfig: adConfig,
@@ -163,7 +183,7 @@ class InterstitialAdManager {
163183
if (_preloadedAd == null) {
164184
_logger.warning('Show ad called, but no ad is pre-loaded. Pre-loading now.');
165185
// Attempt a last-minute load if no ad is ready.
166-
await _maybePreloadAd();
186+
await _maybePreloadAd(_appBloc.state);
167187
if (_preloadedAd == null) {
168188
_logger.severe('Last-minute ad load failed. Cannot show ad.');
169189
return;
@@ -188,7 +208,7 @@ class InterstitialAdManager {
188208
// After the ad is shown or fails to show, dispose of it and
189209
// start pre-loading the next one for the next opportunity.
190210
_disposePreloadedAd(); // Ensure the ad object is disposed
191-
_maybePreloadAd();
211+
_maybePreloadAd(_appBloc.state);
192212
}
193213
}
194214

0 commit comments

Comments
 (0)