Migrate Lumen to Dart and Flutter 2.0
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
|
||||
class Ads {
|
||||
Ads({this.enabled = true});
|
||||
final bool enabled;
|
||||
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;
|
||||
_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;
|
||||
_interstitialAt = DateTime.now();
|
||||
},
|
||||
onAdFailedToLoad: (_) {
|
||||
_interstitialLoading = false;
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> hint(VoidCallback earned) async {
|
||||
if (!supported || !await ConsentInformation.instance.canRequestAds()) {
|
||||
return false;
|
||||
}
|
||||
final a = _reward;
|
||||
_reward = null;
|
||||
if (a == null ||
|
||||
_rewardAt == null ||
|
||||
DateTime.now().difference(_rewardAt!).inMinutes >= 58) {
|
||||
a?.dispose();
|
||||
unawaited(_load());
|
||||
return false;
|
||||
}
|
||||
a.fullScreenContentCallback = FullScreenContentCallback(
|
||||
onAdDismissedFullScreenContent: (ad) {
|
||||
ad.dispose();
|
||||
_load();
|
||||
},
|
||||
onAdFailedToShowFullScreenContent: (ad, e) {
|
||||
ad.dispose();
|
||||
_load();
|
||||
},
|
||||
);
|
||||
a.show(
|
||||
onUserEarnedReward: (ad, reward) {
|
||||
earned();
|
||||
},
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> between(VoidCallback next) async {
|
||||
if (!supported || !await ConsentInformation.instance.canRequestAds()) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
final a = _interstitial;
|
||||
_interstitial = null;
|
||||
if (a == null ||
|
||||
_interstitialAt == null ||
|
||||
DateTime.now().difference(_interstitialAt!).inMinutes >= 58) {
|
||||
a?.dispose();
|
||||
next();
|
||||
unawaited(_load());
|
||||
return;
|
||||
}
|
||||
var routed = false;
|
||||
void finish() {
|
||||
if (routed) return;
|
||||
routed = true;
|
||||
a.dispose();
|
||||
next();
|
||||
_load();
|
||||
}
|
||||
|
||||
a.fullScreenContentCallback = FullScreenContentCallback(
|
||||
onAdDismissedFullScreenContent: (_) => finish(),
|
||||
onAdFailedToShowFullScreenContent: (_, e) => finish(),
|
||||
);
|
||||
a.show();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user