Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Callbacks #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flutter-plugins
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# This is a generated file; do not edit or check into version control.
google_mobile_ads=/Users/tanzilzubairbinzaman/Flutter/.pub-cache/hosted/pub.dartlang.org/google_mobile_ads-2.3.0/
google_mobile_ads=/Users/makduman/.pub-cache/hosted/pub.dev/google_mobile_ads-2.4.0/
2 changes: 1 addition & 1 deletion .flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"google_mobile_ads","path":"/Users/tanzilzubairbinzaman/Flutter/.pub-cache/hosted/pub.dartlang.org/google_mobile_ads-2.3.0/","native_build":true,"dependencies":[]}],"android":[{"name":"google_mobile_ads","path":"/Users/tanzilzubairbinzaman/Flutter/.pub-cache/hosted/pub.dartlang.org/google_mobile_ads-2.3.0/","native_build":true,"dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"google_mobile_ads","dependencies":[]}],"date_created":"2023-02-03 11:45:31.898293","version":"3.3.10"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"google_mobile_ads","path":"/Users/makduman/.pub-cache/hosted/pub.dev/google_mobile_ads-2.4.0/","native_build":true,"dependencies":[]}],"android":[{"name":"google_mobile_ads","path":"/Users/makduman/.pub-cache/hosted/pub.dev/google_mobile_ads-2.4.0/","native_build":true,"dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"google_mobile_ads","dependencies":[]}],"date_created":"2023-10-06 06:53:19.477305","version":"3.7.12"}
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class RewardedExample extends StatelessWidget {
MobileAdsManager.instance
.getManagedRewardedAdQueue(adUnitId: '<Ad Unit ID>')
?.showRewardedAd(
onUserEarnedReward: (AdWithoutView ad, RewardItem reward) {
callback: (bool hasError) {
// Reward user
},
);
Expand Down
86 changes: 56 additions & 30 deletions lib/src/managed_interstitial_ad_queue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ class ManagedInterstitialAdQueue {
Future<void> _initializeInterstitialAdQueue() async {
try {
await Future.wait(
<Future>[
for (int i = 0; i <= _interstitialAdInitializer.count; i++)
_addInterstitialAd()
],
<Future>[for (int i = 0; i <= _interstitialAdInitializer.count; i++) _addInterstitialAd()],
);
} catch (e) {
/// Rethrowing the error, to be caught by an enclosing try-catch block
Expand All @@ -77,7 +74,7 @@ class ManagedInterstitialAdQueue {
/// Shows an interstitial ad, based on the [showChance], automatically
/// reloading the queue after the ad is shown, or doing nothing at all
/// if the ad is not shown (due to [showChance])
void showInterstitialAd({double showChance = 1.0}) {
void showInterstitialAd({double showChance = 1.0, void Function(bool hasError)? callback}) {
assert(
showChance >= 0.0 && showChance <= 1.0,
'showChance must be a double between 0.0 and 1.0, both inclusive',
Expand All @@ -92,6 +89,41 @@ class ManagedInterstitialAdQueue {
/// exhausted before refills are completed
if (_queue.isNotEmpty == true) {
/// Showing the ad
_queue.first.fullScreenContentCallback = FullScreenContentCallback<InterstitialAd>(
onAdShowedFullScreenContent: _interstitialAdInitializer.fullScreenContentCallback?.onAdShowedFullScreenContent,
onAdClicked: _interstitialAdInitializer.fullScreenContentCallback?.onAdClicked,
onAdImpression: _interstitialAdInitializer.fullScreenContentCallback?.onAdImpression,
onAdWillDismissFullScreenContent: (InterstitialAd ad) {
callback?.call(false);

_interstitialAdInitializer.fullScreenContentCallback?.onAdWillDismissFullScreenContent?.call(ad);
},
onAdDismissedFullScreenContent: (InterstitialAd ad) {
/// Calling the user provided function
_interstitialAdInitializer.fullScreenContentCallback?.onAdDismissedFullScreenContent?.call(ad);
callback?.call(false);

/// Disposing the ad. The code is inside a try block to guard
/// against developers who accidentally include a call to .dispose()
/// in their code
try {
ad.dispose();
} catch (_) {}
},
onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) async {
/// Calling the user provided function
_interstitialAdInitializer.fullScreenContentCallback?.onAdFailedToShowFullScreenContent?.call(ad, error);
callback?.call(true);

/// Disposing the ad. The code is inside a try block to guard
/// against developers who accidentally include a call to .dispose()
/// in [_interstitialAdInitializer.fullScreenContentCallback?.onAdFailedToShowFullScreenContent]
try {
ad.dispose();
} catch (_) {}
},
);

_queue.removeFirst().show();

/// Updating the public count of how many ads are
Expand All @@ -101,6 +133,11 @@ class ManagedInterstitialAdQueue {
/// Reloading the queue
_addInterstitialAd();
}
else {
callback?.call(true);
}
} else {
callback?.call(true);
}
}

Expand All @@ -110,35 +147,27 @@ class ManagedInterstitialAdQueue {
Future<void> _addInterstitialAd() async {
/// Configuring the callbacks for the ad, and combining them with the user
/// provided ones
final FullScreenContentCallback<InterstitialAd> fullScreenContentCallback =
FullScreenContentCallback<InterstitialAd>(
onAdShowedFullScreenContent: _interstitialAdInitializer
.fullScreenContentCallback?.onAdShowedFullScreenContent,
onAdClicked:
_interstitialAdInitializer.fullScreenContentCallback?.onAdClicked,
onAdImpression:
_interstitialAdInitializer.fullScreenContentCallback?.onAdImpression,
onAdWillDismissFullScreenContent: _interstitialAdInitializer
.fullScreenContentCallback?.onAdWillDismissFullScreenContent,
final FullScreenContentCallback<InterstitialAd> fullScreenContentCallback = FullScreenContentCallback<InterstitialAd>(
onAdShowedFullScreenContent: _interstitialAdInitializer.fullScreenContentCallback?.onAdShowedFullScreenContent,
onAdClicked: _interstitialAdInitializer.fullScreenContentCallback?.onAdClicked,
onAdImpression: _interstitialAdInitializer.fullScreenContentCallback?.onAdImpression,
onAdWillDismissFullScreenContent: (InterstitialAd ad) {
_interstitialAdInitializer.fullScreenContentCallback?.onAdWillDismissFullScreenContent?.call(ad);
},
onAdDismissedFullScreenContent: (InterstitialAd ad) {
/// Calling the user provided function
_interstitialAdInitializer
.fullScreenContentCallback?.onAdDismissedFullScreenContent
?.call(ad);

_interstitialAdInitializer.fullScreenContentCallback?.onAdDismissedFullScreenContent?.call(ad);
/// Disposing the ad. The code is inside a try block to guard
/// against developers who accidentally include a call to .dispose()
/// in their code
try {
ad.dispose();
} catch (_) {}
},
onAdFailedToShowFullScreenContent:
(InterstitialAd ad, AdError error) async {
onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) async {
/// Calling the user provided function
_interstitialAdInitializer
.fullScreenContentCallback?.onAdFailedToShowFullScreenContent
?.call(ad, error);
_interstitialAdInitializer.fullScreenContentCallback?.onAdFailedToShowFullScreenContent?.call(ad, error);


/// Disposing the ad. The code is inside a try block to guard
/// against developers who accidentally include a call to .dispose()
Expand Down Expand Up @@ -170,8 +199,7 @@ class ManagedInterstitialAdQueue {
adsInQueue = _queue.length;

/// Calling the user provided function
_interstitialAdInitializer.interstitialAdLoadCallback
?.onAdLoaded(ad);
_interstitialAdInitializer.interstitialAdLoadCallback?.onAdLoaded(ad);
} else {
/// Disposing the ad as it was an excess, probably a result
/// of showing and requesting ad loads in rapid succession.
Expand All @@ -180,8 +208,7 @@ class ManagedInterstitialAdQueue {
},
onAdFailedToLoad: (LoadAdError error) async {
/// Calling the user provided function
_interstitialAdInitializer.interstitialAdLoadCallback
?.onAdFailedToLoad(error);
_interstitialAdInitializer.interstitialAdLoadCallback?.onAdFailedToLoad(error);

/// Retrying the load on fail, via recursion, up to 3 times before cancelling
_retryLoadCount += 1;
Expand Down Expand Up @@ -225,8 +252,7 @@ class ManagedInterstitialAdQueue {
/// Removing this instance of [ManagedInterstitialAdQueue] from the internal
/// list of instantiated [ManagedInterstitialAdQueue] maintained by
/// [MobileAdsManager.instance._managedInterstitialAdQueueList]
MobileAdsManager.instance._managedInterstitialAdQueueList
.removeWhere((ManagedInterstitialAdQueue queue) {
MobileAdsManager.instance._managedInterstitialAdQueueList.removeWhere((ManagedInterstitialAdQueue queue) {
/// Matching Ad Unit Ids to determine which queue to remove, as there
/// can only be one queue per Ad Unit Id
return queue.adUnitId == adUnitId;
Expand Down
49 changes: 18 additions & 31 deletions lib/src/managed_rewarded_ad_queue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ class ManagedRewardedAdQueue {
Future<void> _initializeRewardedAdQueue() async {
try {
await Future.wait(
<Future>[
for (int i = 0; i <= _rewardedAdInitializer.count; i++)
_addRewardedAd()
],
<Future>[for (int i = 0; i <= _rewardedAdInitializer.count; i++) _addRewardedAd()],
);
} catch (e) {
/// Rethrowing the error, to be caught by an enclosing try-catch block
Expand All @@ -79,11 +76,7 @@ class ManagedRewardedAdQueue {
/// if the ad is not shown (due to [showChance])
void showRewardedAd({
double showChance = 1.0,
required void Function(
AdWithoutView ad,
RewardItem reward,
)
onUserEarnedReward,
void Function(bool hasError)? callback,
}) {
assert(
showChance >= 0.0 && showChance <= 1.0,
Expand All @@ -99,7 +92,7 @@ class ManagedRewardedAdQueue {
/// exhausted before refills are completed
if (_queue.isNotEmpty == true) {
/// Showing the ad
_queue.removeFirst().show(onUserEarnedReward: onUserEarnedReward);
_queue.removeFirst().show(onUserEarnedReward: (_, __) => callback?.call(false));

/// Updating the public count of how many ads are
/// currently available in the queue
Expand All @@ -108,6 +101,11 @@ class ManagedRewardedAdQueue {
/// Reloading the queue
_addRewardedAd();
}
else {
callback?.call(true);
}
} else {
callback?.call(true);
}
}

Expand All @@ -117,21 +115,15 @@ class ManagedRewardedAdQueue {
Future<void> _addRewardedAd() async {
/// Configuring the callbacks for the ad, and combining them with the user
/// provided ones
final FullScreenContentCallback<RewardedAd> fullScreenContentCallback =
FullScreenContentCallback<RewardedAd>(
onAdShowedFullScreenContent: _rewardedAdInitializer
.fullScreenContentCallback?.onAdShowedFullScreenContent,
onAdClicked:
_rewardedAdInitializer.fullScreenContentCallback?.onAdClicked,
onAdImpression:
_rewardedAdInitializer.fullScreenContentCallback?.onAdImpression,
onAdWillDismissFullScreenContent: _rewardedAdInitializer
.fullScreenContentCallback?.onAdWillDismissFullScreenContent,
final FullScreenContentCallback<RewardedAd> fullScreenContentCallback = FullScreenContentCallback<RewardedAd>(
onAdShowedFullScreenContent: _rewardedAdInitializer.fullScreenContentCallback?.onAdShowedFullScreenContent,
onAdClicked: _rewardedAdInitializer.fullScreenContentCallback?.onAdClicked,
onAdImpression: _rewardedAdInitializer.fullScreenContentCallback?.onAdImpression,
onAdWillDismissFullScreenContent: _rewardedAdInitializer.fullScreenContentCallback?.onAdWillDismissFullScreenContent,
onAdDismissedFullScreenContent: (RewardedAd ad) {
/// Calling the user provided function
_rewardedAdInitializer
.fullScreenContentCallback?.onAdDismissedFullScreenContent
?.call(ad);
_rewardedAdInitializer.fullScreenContentCallback?.onAdDismissedFullScreenContent?.call(ad);


/// Disposing the ad. The code is inside a try block to guard
/// against developers who accidentally include a call to .dispose()
Expand All @@ -142,10 +134,7 @@ class ManagedRewardedAdQueue {
},
onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) async {
/// Calling the user provided function
_rewardedAdInitializer
.fullScreenContentCallback?.onAdFailedToShowFullScreenContent
?.call(ad, error);

_rewardedAdInitializer.fullScreenContentCallback?.onAdFailedToShowFullScreenContent?.call(ad, error);
/// Disposing the ad. The code is inside a try block to guard
/// against developers who accidentally include a call to .dispose()
/// in [_rewardedAdInitializer.fullScreenContentCallback?.onAdFailedToShowFullScreenContent]
Expand Down Expand Up @@ -185,8 +174,7 @@ class ManagedRewardedAdQueue {
},
onAdFailedToLoad: (LoadAdError error) async {
/// Calling the user provided function
_rewardedAdInitializer.rewardedAdLoadCallback
?.onAdFailedToLoad(error);
_rewardedAdInitializer.rewardedAdLoadCallback?.onAdFailedToLoad(error);

/// Retrying the load on fail, via recursion, up to 3 times before cancelling
_retryLoadCount += 1;
Expand Down Expand Up @@ -230,8 +218,7 @@ class ManagedRewardedAdQueue {
/// Removing this instance of [ManagedRewardedAdQueue] from the internal
/// list of instantiated [ManagedRewardedAdQueue] maintained by
/// [MobileAdsManager.instance._managedRewardedAdQueueList]
MobileAdsManager.instance._managedRewardedAdQueueList
.removeWhere((ManagedRewardedAdQueue queue) {
MobileAdsManager.instance._managedRewardedAdQueueList.removeWhere((ManagedRewardedAdQueue queue) {
/// Matching Ad Unit Ids to determine which queue to remove, as there
/// can only be one queue per Ad Unit Id
return queue.adUnitId == adUnitId;
Expand Down