249 lines
6.7 KiB
Dart
249 lines
6.7 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
|
|
|
import 'ad_pacing.dart';
|
|
import 'play_metrics.dart';
|
|
|
|
class Ads {
|
|
Ads({this.enabled = true, this.pacing, this.metrics});
|
|
final bool enabled;
|
|
final AdPacing? pacing;
|
|
final PlayMetrics? metrics;
|
|
bool _showing = false;
|
|
bool foreground = true;
|
|
|
|
void _paid(AdWithoutView ad, String format) {
|
|
ad.onPaidEvent = (ad, value, precision, currency) {
|
|
metrics?.paid(
|
|
format: format,
|
|
micros: value,
|
|
currency: currency,
|
|
precision: precision.name,
|
|
test: ad.adUnitId.contains('3940256099942544'),
|
|
);
|
|
};
|
|
}
|
|
|
|
void _impression(Ad ad, String format) => metrics?.event(
|
|
'${ad.adUnitId.contains('3940256099942544') ? 'test' : 'configured'}_${format}_impressions',
|
|
);
|
|
RewardedAd? _reward;
|
|
InterstitialAd? _interstitial;
|
|
bool _started = false,
|
|
_rewardLoading = false,
|
|
_interstitialLoading = false,
|
|
_disposed = false;
|
|
DateTime? _rewardAt, _interstitialAt;
|
|
bool get supported =>
|
|
enabled &&
|
|
!kIsWeb &&
|
|
(defaultTargetPlatform == TargetPlatform.android ||
|
|
defaultTargetPlatform == TargetPlatform.iOS);
|
|
Future<void> start() async {
|
|
if (!supported) return;
|
|
ConsentInformation.instance.requestConsentInfoUpdate(
|
|
ConsentRequestParameters(),
|
|
() {
|
|
ConsentForm.loadAndShowConsentFormIfRequired((_) => _initialize());
|
|
},
|
|
(_) => _initialize(),
|
|
);
|
|
}
|
|
|
|
Future<void> _initialize() async {
|
|
if (_disposed ||
|
|
_started ||
|
|
!await ConsentInformation.instance.canRequestAds()) {
|
|
return;
|
|
}
|
|
_started = true;
|
|
await MobileAds.instance.initialize();
|
|
await _load();
|
|
}
|
|
|
|
Future<void> _load() async {
|
|
if (_disposed ||
|
|
!supported ||
|
|
!await ConsentInformation.instance.canRequestAds()) {
|
|
return;
|
|
}
|
|
final ios = defaultTargetPlatform == TargetPlatform.iOS;
|
|
if (_reward == null && !_rewardLoading) {
|
|
_rewardLoading = true;
|
|
RewardedAd.load(
|
|
adUnitId: const String.fromEnvironment('ADMOB_REWARDED_ID').isNotEmpty
|
|
? const String.fromEnvironment('ADMOB_REWARDED_ID')
|
|
: ios
|
|
? 'ca-app-pub-3940256099942544/1712485313'
|
|
: 'ca-app-pub-3940256099942544/5224354917',
|
|
request: const AdRequest(),
|
|
rewardedAdLoadCallback: RewardedAdLoadCallback(
|
|
onAdLoaded: (a) {
|
|
_rewardLoading = false;
|
|
if (_disposed) {
|
|
a.dispose();
|
|
return;
|
|
}
|
|
_reward = a;
|
|
_paid(a, 'rewarded');
|
|
_rewardAt = DateTime.now();
|
|
},
|
|
onAdFailedToLoad: (_) {
|
|
_rewardLoading = false;
|
|
},
|
|
),
|
|
);
|
|
}
|
|
if (_interstitial == null && !_interstitialLoading) {
|
|
_interstitialLoading = true;
|
|
InterstitialAd.load(
|
|
adUnitId:
|
|
const String.fromEnvironment('ADMOB_INTERSTITIAL_ID').isNotEmpty
|
|
? const String.fromEnvironment('ADMOB_INTERSTITIAL_ID')
|
|
: ios
|
|
? 'ca-app-pub-3940256099942544/4411468910'
|
|
: 'ca-app-pub-3940256099942544/1033173712',
|
|
request: const AdRequest(),
|
|
adLoadCallback: InterstitialAdLoadCallback(
|
|
onAdLoaded: (a) {
|
|
_interstitialLoading = false;
|
|
if (_disposed) {
|
|
a.dispose();
|
|
return;
|
|
}
|
|
_interstitial = a;
|
|
_paid(a, 'interstitial');
|
|
_interstitialAt = DateTime.now();
|
|
},
|
|
onAdFailedToLoad: (_) {
|
|
_interstitialLoading = false;
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<bool> hint(VoidCallback earned) async {
|
|
if (!supported ||
|
|
_disposed ||
|
|
_showing ||
|
|
!foreground ||
|
|
!await ConsentInformation.instance.canRequestAds()) {
|
|
return false;
|
|
}
|
|
if (_disposed || _showing || !foreground) return false;
|
|
metrics?.event('rewarded_opportunities');
|
|
final a = _reward;
|
|
_reward = null;
|
|
if (a == null ||
|
|
_rewardAt == null ||
|
|
DateTime.now().difference(_rewardAt!).inMinutes >= 58) {
|
|
a?.dispose();
|
|
unawaited(_load());
|
|
metrics?.event('rewarded_unavailable');
|
|
return false;
|
|
}
|
|
_showing = true;
|
|
var granted = false;
|
|
a.fullScreenContentCallback = FullScreenContentCallback(
|
|
onAdShowedFullScreenContent: (_) => pacing?.shown(automatic: false),
|
|
onAdImpression: (ad) => _impression(ad, 'rewarded'),
|
|
onAdDismissedFullScreenContent: (ad) {
|
|
_showing = false;
|
|
ad.dispose();
|
|
_load();
|
|
},
|
|
onAdFailedToShowFullScreenContent: (ad, e) {
|
|
_showing = false;
|
|
ad.dispose();
|
|
_load();
|
|
},
|
|
);
|
|
try {
|
|
await a.show(
|
|
onUserEarnedReward: (ad, reward) {
|
|
if (granted) return;
|
|
granted = true;
|
|
metrics?.event('rewarded_earned');
|
|
earned();
|
|
},
|
|
);
|
|
} catch (_) {
|
|
_showing = false;
|
|
a.dispose();
|
|
unawaited(_load());
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
Future<void> between(VoidCallback next) async {
|
|
if (!supported ||
|
|
_disposed ||
|
|
_showing ||
|
|
!foreground ||
|
|
!(pacing?.allowed ?? true) ||
|
|
!await ConsentInformation.instance.canRequestAds()) {
|
|
next();
|
|
return;
|
|
}
|
|
if (_disposed || _showing || !foreground) {
|
|
next();
|
|
return;
|
|
}
|
|
metrics?.event('interstitial_opportunities');
|
|
final a = _interstitial;
|
|
_interstitial = null;
|
|
if (a == null ||
|
|
_interstitialAt == null ||
|
|
DateTime.now().difference(_interstitialAt!).inMinutes >= 58) {
|
|
a?.dispose();
|
|
next();
|
|
metrics?.event('interstitial_unavailable');
|
|
unawaited(_load());
|
|
return;
|
|
}
|
|
var routed = false;
|
|
_showing = true;
|
|
void finish() {
|
|
if (routed) return;
|
|
routed = true;
|
|
_showing = false;
|
|
a.dispose();
|
|
next();
|
|
_load();
|
|
}
|
|
|
|
a.fullScreenContentCallback = FullScreenContentCallback(
|
|
onAdShowedFullScreenContent: (_) => pacing?.shown(automatic: true),
|
|
onAdImpression: (ad) => _impression(ad, 'interstitial'),
|
|
onAdDismissedFullScreenContent: (_) => finish(),
|
|
onAdFailedToShowFullScreenContent: (_, e) => finish(),
|
|
);
|
|
try {
|
|
await a.show();
|
|
} catch (_) {
|
|
finish();
|
|
}
|
|
}
|
|
|
|
Future<bool> privacy() async {
|
|
if (!supported) return false;
|
|
if (await ConsentInformation.instance
|
|
.getPrivacyOptionsRequirementStatus() !=
|
|
PrivacyOptionsRequirementStatus.required) {
|
|
return false;
|
|
}
|
|
ConsentForm.showPrivacyOptionsForm((_) {});
|
|
return true;
|
|
}
|
|
|
|
void dispose() {
|
|
_disposed = true;
|
|
_reward?.dispose();
|
|
_interstitial?.dispose();
|
|
}
|
|
}
|