This commit is contained in:
brontomark committed 2026-11-15 13:25:45 +01:00
1 parent 28009867d8
commit 8bfe077108
2 files changed
+78 -22

No files matched your search

+77 -21
View File
@@ -15,15 +15,21 @@ class AdService {
static const String androidTestRewardedAdUnitId = static const String androidTestRewardedAdUnitId =
'ca-app-pub-3940256099942544/5224354917'; '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; RewardedAd? _rewardedAd;
bool _isLoading = false; bool _isLoading = false;
bool _isInitialized = false; bool _isInitialized = false;
Completer<RewardedAd?>? _loadCompleter;
static bool get _isFlutterTest => static bool get _isFlutterTest =>
!kIsWeb && Platform.environment.containsKey('FLUTTER_TEST'); !kIsWeb && Platform.environment.containsKey('FLUTTER_TEST');
String get rewardedAdUnitId { String get rewardedAdUnitId {
if (kReleaseMode) { if (useProductionAds && kReleaseMode) {
return androidProductionRewardedAdUnitId; return androidProductionRewardedAdUnitId;
} }
return androidTestRewardedAdUnitId; return androidTestRewardedAdUnitId;
@@ -31,6 +37,7 @@ class AdService {
bool get isInitialized => _isInitialized; bool get isInitialized => _isInitialized;
bool get isAdReady => _rewardedAd != null; bool get isAdReady => _rewardedAd != null;
bool get isLoading => _isLoading;
Future<void> initialize() async { Future<void> initialize() async {
if (_isFlutterTest || kIsWeb) return; if (_isFlutterTest || kIsWeb) return;
@@ -38,7 +45,7 @@ class AdService {
await MobileAds.instance.initialize(); await MobileAds.instance.initialize();
_isInitialized = true; _isInitialized = true;
// Preload after the first UI frames have settled // Preload after the first UI frames have settled
Future.delayed(const Duration(seconds: 2), () { Future.delayed(const Duration(seconds: 1), () {
loadRewardedAd(); loadRewardedAd();
}); });
} catch (e) { } catch (e) {
@@ -46,30 +53,65 @@ class AdService {
} }
} }
void loadRewardedAd() { Future<RewardedAd?> _requestAd(String unitId) {
if (_isFlutterTest || kIsWeb || _isLoading || _rewardedAd != null) return; final completer = Completer<RewardedAd?>();
_isLoading = true;
try { try {
RewardedAd.load( RewardedAd.load(
adUnitId: rewardedAdUnitId, adUnitId: unitId,
request: const AdRequest(), request: const AdRequest(),
rewardedAdLoadCallback: RewardedAdLoadCallback( rewardedAdLoadCallback: RewardedAdLoadCallback(
onAdLoaded: (ad) { onAdLoaded: (ad) {
_rewardedAd = ad; debugPrint('RewardedAd loaded successfully with unit: $unitId');
_isLoading = false; completer.complete(ad);
}, },
onAdFailedToLoad: (error) { onAdFailedToLoad: (error) {
debugPrint('RewardedAd failed to load: $error'); debugPrint('RewardedAd failed for $unitId: ${error.message} (code: ${error.code})');
_rewardedAd = null; completer.complete(null);
_isLoading = false;
}, },
), ),
); );
} catch (e) { } catch (e) {
debugPrint('RewardedAd.load exception: $e'); debugPrint('RewardedAd request exception for $unitId: $e');
completer.complete(null);
}
return completer.future;
}
Future<bool> 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<RewardedAd?>();
_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; _rewardedAd = null;
_isLoading = false; _isLoading = false;
_loadCompleter?.complete(null);
_loadCompleter = null;
return false;
} }
} }
@@ -79,9 +121,17 @@ class AdService {
return true; return true;
} }
// If ad is not preloaded yet, wait up to 8s for it to load
if (_rewardedAd == null) { if (_rewardedAd == null) {
loadRewardedAd(); debugPrint('Ad not preloaded yet, awaiting load on demand...');
return false; 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<bool>(); final completer = Completer<bool>();
@@ -95,17 +145,24 @@ class AdService {
if (!completer.isCompleted) completer.complete(true); if (!completer.isCompleted) completer.complete(true);
}, },
onAdFailedToShowFullScreenContent: (ad, error) { onAdFailedToShowFullScreenContent: (ad, error) {
debugPrint('Ad failed to show: ${error.message}');
ad.dispose(); ad.dispose();
loadRewardedAd(); loadRewardedAd();
if (!completer.isCompleted) completer.complete(false); if (!completer.isCompleted) completer.complete(false);
}, },
); );
await ad.show( try {
onUserEarnedReward: (_, reward) { await ad.show(
onUserEarnedReward(); 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; return completer.future;
} }
@@ -115,4 +172,3 @@ class AdService {
_rewardedAd = null; _rewardedAd = null;
} }
} }
+1 -1
View File
@@ -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 # 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 # 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. # 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: environment:
sdk: ^3.13.4 sdk: ^3.13.4