Skip to content

Commit 927fd65

Browse files
committed
refactor(ads): rename AdCacheService to InlineAdCacheService
- Reflects the service's specific focus on caching inline ads - Excludes interstitial ads from caching scope - Updates class name and related comments
1 parent fd22b17 commit 927fd65

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed
Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
1-
import 'package:core/core.dart'; // Import core for AdPlatformType
1+
import 'package:core/core.dart';
22
import 'package:flutter/foundation.dart';
3-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/banner_ad.dart'; // Import BannerAd
4-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/inline_ad.dart'; // Import InlineAd
5-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/native_ad.dart'; // Import NativeAd
6-
import 'package:google_mobile_ads/google_mobile_ads.dart' as admob; // Import AdMob for disposal
3+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/banner_ad.dart';
4+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/inline_ad.dart';
5+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/native_ad.dart';
6+
import 'package:google_mobile_ads/google_mobile_ads.dart' as admob;
77
import 'package:logging/logging.dart';
88

9-
/// {@template ad_cache_service}
9+
/// {@template inline_ad_cache_service}
1010
/// A singleton service for caching loaded inline ad objects (native and banner).
1111
///
1212
/// This service helps to prevent unnecessary re-loading of inline ads
1313
/// when they scroll out of view and then back into view in a scrollable list.
1414
/// It stores [InlineAd] objects by a unique ID (typically the [AdPlaceholder.id])
1515
/// and allows for retrieval and clearing of cached ads.
1616
///
17+
/// **Scope of Caching:**
18+
/// This service specifically caches *inline* ads, which include both feed ads
19+
/// and in-article ads. These ads are designed to be displayed directly within
20+
/// content lists and benefit from caching to improve scrolling performance
21+
/// and reduce network requests.
22+
///
23+
/// **Exclusion of Interstitial Ads:**
24+
/// Interstitial ads are *not* cached by this service. Their lifecycle is
25+
/// managed differently: they are typically loaded on demand, shown once
26+
/// during navigation transitions, and then disposed immediately after use.
27+
/// Caching them would not provide performance benefits and could lead to
28+
/// resource leaks or unexpected behavior.
29+
///
1730
/// Inline ad objects (like `google_mobile_ads.NativeAd` and `google_mobile_ads.BannerAd`)
1831
/// are stateful and resource-intensive. Caching them allows for smoother scrolling
1932
/// and reduces network requests, while still ensuring proper disposal when
2033
/// the cache is cleared (e.g., on a full feed refresh).
21-
///
22-
/// Interstitial ads are not cached by this service as their lifecycle is
23-
/// managed differently (typically shown once on navigation and then disposed).
2434
/// {@endtemplate}
25-
class AdCacheService {
35+
class InlineAdCacheService {
2636
/// Factory constructor to provide the singleton instance.
27-
factory AdCacheService() => _instance;
37+
factory InlineAdCacheService() => _instance;
2838

2939
/// Private constructor for the singleton pattern.
30-
AdCacheService._internal() : _logger = Logger('AdCacheService');
40+
InlineAdCacheService._internal() : _logger = Logger('InlineAdCacheService');
3141

32-
/// The single instance of [AdCacheService].
33-
static final AdCacheService _instance = AdCacheService._internal();
42+
/// The single instance of [InlineAdCacheService].
43+
static final InlineAdCacheService _instance =
44+
InlineAdCacheService._internal();
3445

3546
final Logger _logger;
3647

@@ -71,7 +82,9 @@ class AdCacheService {
7182
/// when the application is closing to ensure all native ad resources
7283
/// are released.
7384
void clearAllAds() {
74-
_logger.info('Clearing all cached inline ads and disposing native resources.');
85+
_logger.info(
86+
'Clearing all cached inline ads and disposing native resources.',
87+
);
7588
for (final ad in _cache.values) {
7689
if (ad?.provider == AdPlatformType.admob) {
7790
// Dispose AdMob native and banner ad objects.
@@ -90,7 +103,7 @@ class AdCacheService {
90103
/// For debugging: prints the current state of the cache.
91104
@visibleForTesting
92105
void printCacheState() {
93-
_logger.info('Current Ad Cache State:');
106+
_logger.info('Current Inline Ad Cache State:');
94107
if (_cache.isEmpty) {
95108
_logger.info(' Cache is empty.');
96109
} else {

0 commit comments

Comments
 (0)