Last active
February 15, 2023 11:20
-
-
Save nooralibutt/861278af6c1e55d80f3026b23ccdc240 to your computer and use it in GitHub Desktop.
Call AdSetting.fetch(); to initialize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:io'; | |
import 'package:app_tracking_transparency/app_tracking_transparency.dart'; | |
import 'package:easy_ads_flutter/easy_ads_flutter.dart'; | |
import 'package:fake_call/ad_settings/ad_setting.dart'; | |
import 'package:fake_call/utilities/ad_id_manager.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:unity_ads_plugin/unity_ads_plugin.dart'; | |
class AdManager { | |
static const IAdIdManager adIdManager = TestAdIdManager(); | |
static final AdManager _instance = AdManager._(); | |
static AdManager get instance => _instance; | |
AdManager._(); | |
List<AdPriority>? adPriorityList; | |
List<AdNetwork>? bannerAdPriorityList; | |
Future<void> initialize() async { | |
final isIosApproving = Platform.isIOS && AdSetting.singleton.isIosApproving; | |
final isAndroidApproving = | |
Platform.isAndroid && AdSetting.singleton.isAndroidApproving; | |
if (Platform.isIOS) { | |
TrackingStatus status = | |
await AppTrackingTransparency.trackingAuthorizationStatus; | |
if (status == TrackingStatus.notDetermined) { | |
status = await AppTrackingTransparency.requestTrackingAuthorization(); | |
} | |
await UnityAds.setPrivacyConsent( | |
PrivacyConsentType.gdpr, status == TrackingStatus.authorized); | |
await UnityAds.setPrivacyConsent( | |
PrivacyConsentType.ccpa, status == TrackingStatus.authorized); | |
await UnityAds.setPrivacyConsent( | |
PrivacyConsentType.pipl, status == TrackingStatus.authorized); | |
await Future.delayed(const Duration(seconds: 1)); | |
} | |
await UnityAds.setPrivacyConsent( | |
PrivacyConsentType.ageGate, isIosApproving || isAndroidApproving); | |
final targetingInfo = AdRequest( | |
nonPersonalizedAds: !isIosApproving || !isAndroidApproving, | |
keywords: <String>[ | |
'call', | |
'scary', | |
'teacher', | |
'prank', | |
'funny', | |
'messages', | |
'chat', | |
'stories', | |
]); | |
final requestConf = | |
RequestConfiguration(maxAdContentRating: MaxAdContentRating.t); | |
await EasyAds.instance.initialize( | |
adIdManager, | |
admobConfiguration: requestConf, | |
adMobAdRequest: targetingInfo, | |
showAdBadge: isAndroidApproving, | |
fbiOSAdvertiserTrackingEnabled: isIosApproving, | |
isAgeRestrictedUserForApplovin: isIosApproving || isAndroidApproving, | |
); | |
adPriorityList = AdSetting.singleton.adPriorityList; | |
bannerAdPriorityList = AdSetting.singleton.getBannerPriorityList(); | |
} | |
bool showPriorityInterstitial() { | |
final list = adPriorityList; | |
if (list == null || list.isEmpty) { | |
return EasyAds.instance.showAd(AdUnitType.interstitial); | |
} | |
for (int i = 0; i < list.length; i++) { | |
if (list[i] == AdPriority.facebook) { | |
if (EasyAds.instance.showAd(AdUnitType.interstitial, | |
adNetwork: AdNetwork.facebook)) return true; | |
} else if (list[i] == AdPriority.unity) { | |
if (EasyAds.instance.showAd(AdUnitType.interstitial, | |
adNetwork: AdNetwork.unity)) return true; | |
} else if (list[i] == AdPriority.appLovin) { | |
if (EasyAds.instance.showAd(AdUnitType.interstitial, | |
adNetwork: AdNetwork.appLovin)) return true; | |
} else if (list[i] == AdPriority.admob) { | |
if (EasyAds.instance.showAd(AdUnitType.interstitial, | |
adNetwork: AdNetwork.admob)) return true; | |
} | |
} | |
return EasyAds.instance.showAd(AdUnitType.interstitial); | |
} | |
Widget showPriorityBanner({AdSize adSize = AdSize.banner}) { | |
final list = bannerAdPriorityList; | |
if (list == null || list.isEmpty) { | |
return EasySmartBannerAd(adSize: adSize); | |
} | |
return EasySmartBannerAd(priorityAdNetworks: list, adSize: adSize); | |
} | |
int _count = 0; | |
void showCountedInterstitial() { | |
_count++; | |
final serverCounter = AdSetting.singleton.interstitialCounter; | |
if (_count >= serverCounter && showPriorityInterstitial()) { | |
_count = 0; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:http/http.dart' as http; | |
// TODO: 1. Add http dependency https://pub.dev/packages/http/install | |
// dependencies: | |
// http: ^0.13.5 | |
enum AdPriority { admob, appLovin, unity, facebook, any } | |
extension AdPriorityExtension on AdPriority { | |
String get value => describeEnum(this); | |
} | |
const adPriorityStringToEnumMap = { | |
'admob': AdPriority.admob, | |
'appLovin': AdPriority.appLovin, | |
'unity': AdPriority.unity, | |
'facebook': AdPriority.facebook, | |
'any': AdPriority.any, | |
}; | |
const _defaultAdPriority = [ | |
AdPriority.admob, | |
AdPriority.facebook, | |
AdPriority.unity, | |
AdPriority.appLovin, | |
AdPriority.any | |
]; | |
class AdSetting { | |
// TODO: 2. replace your package_name endpoint here | |
static const _endPoint = '/package_name.json'; | |
static AdSetting singleton = const AdSetting(); | |
final List<AdPriority> adPriorityList; | |
final List<AdPriority> bannerAdPriorityList; | |
final bool isAndroidApproving; | |
final bool isIosApproving; | |
final int interstitialCounter; | |
const AdSetting({ | |
this.adPriorityList = _defaultAdPriority, | |
this.bannerAdPriorityList = _defaultAdPriority, | |
this.isIosApproving = true, | |
this.isAndroidApproving = true, | |
this.interstitialCounter = 2, | |
}); | |
factory AdSetting._fromMap(Map<String, dynamic> map) { | |
return AdSetting( | |
adPriorityList: _toList(map["ad_priority"]), | |
bannerAdPriorityList: _toList(map["banner_ad_priority"]), | |
isAndroidApproving: map["is_android_approving"], | |
isIosApproving: map["is_ios_approving"], | |
interstitialCounter: map["interstitial_ad_count"], | |
); | |
} | |
static List<AdPriority> _toList(final List<dynamic> list) => list | |
.map<AdPriority>( | |
(e) => adPriorityStringToEnumMap[e.toString()] ?? AdPriority.any) | |
.toList(); | |
static Future<void> fetch() async { | |
var url = Uri.https('nooralibutt.github.io', _endPoint); | |
final response = await http.get(url, | |
headers: {'Content-Type': 'application/json', 'Charset': 'utf-8'}); | |
if (response.statusCode == 200) { | |
final str = utf8.decode(response.bodyBytes).replaceAll('\n', ''); | |
final decodedResponse = jsonDecode(str) as Map<String, dynamic>; | |
AdSetting.singleton = AdSetting._fromMap(decodedResponse); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment