diff --git a/app/lib/core/services/ad_service.dart b/app/lib/core/services/ad_service.dart index 0e57a98..4e4ae05 100644 --- a/app/lib/core/services/ad_service.dart +++ b/app/lib/core/services/ad_service.dart @@ -15,15 +15,21 @@ class AdService { static const String androidTestRewardedAdUnitId = 'ca-app-pub-3940256099942544/5224354917'; + // Set to true only when publishing the final public release to production. + // Kept false during development and closed testing to guarantee 100% fill rate + // and prevent accidental invalid traffic strikes on the AdMob account. + static const bool useProductionAds = false; + RewardedAd? _rewardedAd; bool _isLoading = false; bool _isInitialized = false; + Completer? _loadCompleter; static bool get _isFlutterTest => !kIsWeb && Platform.environment.containsKey('FLUTTER_TEST'); String get rewardedAdUnitId { - if (kReleaseMode) { + if (useProductionAds && kReleaseMode) { return androidProductionRewardedAdUnitId; } return androidTestRewardedAdUnitId; @@ -31,6 +37,7 @@ class AdService { bool get isInitialized => _isInitialized; bool get isAdReady => _rewardedAd != null; + bool get isLoading => _isLoading; Future initialize() async { if (_isFlutterTest || kIsWeb) return; @@ -38,7 +45,7 @@ class AdService { await MobileAds.instance.initialize(); _isInitialized = true; // Preload after the first UI frames have settled - Future.delayed(const Duration(seconds: 2), () { + Future.delayed(const Duration(seconds: 1), () { loadRewardedAd(); }); } catch (e) { @@ -46,30 +53,65 @@ class AdService { } } - void loadRewardedAd() { - if (_isFlutterTest || kIsWeb || _isLoading || _rewardedAd != null) return; - _isLoading = true; - + Future _requestAd(String unitId) { + final completer = Completer(); try { RewardedAd.load( - adUnitId: rewardedAdUnitId, + adUnitId: unitId, request: const AdRequest(), rewardedAdLoadCallback: RewardedAdLoadCallback( onAdLoaded: (ad) { - _rewardedAd = ad; - _isLoading = false; + debugPrint('RewardedAd loaded successfully with unit: $unitId'); + completer.complete(ad); }, onAdFailedToLoad: (error) { - debugPrint('RewardedAd failed to load: $error'); - _rewardedAd = null; - _isLoading = false; + debugPrint('RewardedAd failed for $unitId: ${error.message} (code: ${error.code})'); + completer.complete(null); }, ), ); } catch (e) { - debugPrint('RewardedAd.load exception: $e'); + debugPrint('RewardedAd request exception for $unitId: $e'); + completer.complete(null); + } + return completer.future; + } + + Future loadRewardedAd() async { + if (_isFlutterTest || kIsWeb) return true; + if (_rewardedAd != null) return true; + + if (_loadCompleter != null) { + final ad = await _loadCompleter!.future; + return ad != null; + } + + _loadCompleter = Completer(); + _isLoading = true; + + try { + // 1. Try preferred unit (production in release, test in debug) + var ad = await _requestAd(rewardedAdUnitId); + + // 2. If production has No Fill (brand new unit taking up to 24h to activate), + // fallback to Google Test Unit so testers and reviewers are never blocked! + if (ad == null && rewardedAdUnitId != androidTestRewardedAdUnitId) { + debugPrint('Production ad unit has no inventory yet. Falling back to test unit...'); + ad = await _requestAd(androidTestRewardedAdUnitId); + } + + _rewardedAd = ad; + _isLoading = false; + _loadCompleter?.complete(ad); + _loadCompleter = null; + return ad != null; + } catch (e) { + debugPrint('loadRewardedAd top-level error: $e'); _rewardedAd = null; _isLoading = false; + _loadCompleter?.complete(null); + _loadCompleter = null; + return false; } } @@ -79,9 +121,17 @@ class AdService { return true; } + // If ad is not preloaded yet, wait up to 8s for it to load if (_rewardedAd == null) { - loadRewardedAd(); - return false; + debugPrint('Ad not preloaded yet, awaiting load on demand...'); + final loaded = await loadRewardedAd().timeout( + const Duration(seconds: 8), + onTimeout: () => false, + ); + if (!loaded || _rewardedAd == null) { + debugPrint('Ad load failed or timed out'); + return false; + } } final completer = Completer(); @@ -95,17 +145,24 @@ class AdService { if (!completer.isCompleted) completer.complete(true); }, onAdFailedToShowFullScreenContent: (ad, error) { + debugPrint('Ad failed to show: ${error.message}'); ad.dispose(); loadRewardedAd(); if (!completer.isCompleted) completer.complete(false); }, ); - await ad.show( - onUserEarnedReward: (_, reward) { - onUserEarnedReward(); - }, - ); + try { + await ad.show( + onUserEarnedReward: (_, reward) { + debugPrint('User earned reward: ${reward.amount} ${reward.type}'); + onUserEarnedReward(); + }, + ); + } catch (e) { + debugPrint('Exception showing ad: $e'); + if (!completer.isCompleted) completer.complete(false); + } return completer.future; } @@ -115,4 +172,3 @@ class AdService { _rewardedAd = null; } } - diff --git a/app/pubspec.yaml b/app/pubspec.yaml index b6c5aa9..1aad164 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.0.6+7 +version: 1.0.8+9 environment: sdk: ^3.13.4