|
| 1 | +import 'package:core/core.dart'; |
| 2 | +import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_provider.dart'; |
| 3 | +import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart'; |
| 4 | +import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/native_ad.dart'; |
| 5 | +import 'package:logging/logging.dart'; |
| 6 | +import 'package:uuid/uuid.dart'; |
| 7 | + |
| 8 | +/// {@template no_op_ad_provider} |
| 9 | +/// A "no-operation" implementation of [AdProvider] for platforms where |
| 10 | +/// native ad SDKs are not supported (e.g., web). |
| 11 | +/// |
| 12 | +/// This provider's `initialize` method does nothing, and its `loadNativeAd` |
| 13 | +/// method returns a [NativeAd] with [AdProviderType.placeholder]. This ensures |
| 14 | +/// that the application's UI can still render an "ad slot" for visual |
| 15 | +/// consistency in demo environments, without attempting to load actual native |
| 16 | +/// ads that would cause platform exceptions. |
| 17 | +/// {@endtemplate} |
| 18 | +class NoOpAdProvider implements AdProvider { |
| 19 | + /// {@macro no_op_ad_provider} |
| 20 | + NoOpAdProvider({Logger? logger}) |
| 21 | + : _logger = logger ?? Logger('NoOpAdProvider'); |
| 22 | + |
| 23 | + final Logger _logger; |
| 24 | + final Uuid _uuid = const Uuid(); |
| 25 | + |
| 26 | + /// This method does nothing as there's no SDK to initialize. |
| 27 | + /// |
| 28 | + /// It logs a message to indicate its no-op nature. |
| 29 | + @override |
| 30 | + Future<void> initialize() async { |
| 31 | + _logger.info('No-Op Ad Provider initialized (no actual SDK to init).'); |
| 32 | + } |
| 33 | + |
| 34 | + /// Loads a placeholder native ad. |
| 35 | + /// |
| 36 | + /// This method does not interact with any external ad network. Instead, it |
| 37 | + /// creates a [NativeAd] object with [AdProviderType.placeholder], which |
| 38 | + /// signals to the UI that a generic placeholder should be rendered. |
| 39 | + /// |
| 40 | + /// The [imageStyle] and [adThemeStyle] parameters are accepted for API |
| 41 | + /// compatibility but are not used to load a real ad. |
| 42 | + @override |
| 43 | + Future<NativeAd?> loadNativeAd({ |
| 44 | + required HeadlineImageStyle imageStyle, |
| 45 | + required AdThemeStyle adThemeStyle, |
| 46 | + }) async { |
| 47 | + _logger.info('Loading placeholder native ad.'); |
| 48 | + // Return a dummy NativeAd object with a placeholder type. |
| 49 | + // The `adObject` can be any non-null object, as it won't be used by |
| 50 | + // the placeholder rendering widget. |
| 51 | + return NativeAd( |
| 52 | + id: _uuid.v4(), |
| 53 | + provider: AdProviderType.placeholder, |
| 54 | + adObject: Object(), // Dummy object |
| 55 | + ); |
| 56 | + } |
| 57 | +} |
0 commit comments