Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Improved Promise handling for showInterstitial() #506

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 33 additions & 11 deletions firebase.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,11 @@ firebase.admob.showBanner = function (arg) {
firebase.admob.adView.setAdUnitId(settings.androidBannerId);
var bannerType = firebase.admob._getBannerType(settings.size);
firebase.admob.adView.setAdSize(bannerType);
console.log("----- bannerType: " + bannerType);
//console.log("----- bannerType: " + bannerType);
var BannerAdListener = com.google.android.gms.ads.AdListener.extend({
onAdLoaded: function () {
//firebase.admob.interstitialView.show();
console.log('ad loaded');
//console.log('ad loaded');
resolve();
},
onAdFailedToLoad: function (errorCode) {
Expand Down Expand Up @@ -577,11 +577,29 @@ firebase.admob.showBanner = function (arg) {
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT);

// wrapping it in a timeout makes sure that when this function is loaded from
// a Page.loaded event 'frame.topmost()' doesn't resolve to 'undefined'
setTimeout(function () {
frame.topmost().currentPage.android.getParent().addView(adViewLayout, relativeLayoutParamsOuter);
}, 0);
// wrapping it in a timeout makes sure that when this function is loaded from
// a Page.loaded event 'frame.topmost()' doesn't resolve to 'undefined'
var showBanner = function(counter) {
if (frame.topmost() && frame.topmost().currentPage && frame.topmost().currentPage.android && frame.topmost().currentPage.android.getParent()) {
frame
.topmost()
.currentPage.android.getParent()
.addView(adViewLayout, relativeLayoutParamsOuter);
} else {
if (counter === undefined) {
counter = 1;
} else if (counter < 5) {
var delay = Number(counter * 300);
console.log('trying again in ' + delay + ' ms...');
setTimeout(function() {
showBanner(counter++);
}, delay);
}
}
};
setTimeout(function() {
showBanner();
}, 0);
} catch (ex) {
console.log("Error in firebase.admob.showBanner: " + ex);
reject(ex);
Expand All @@ -599,15 +617,19 @@ firebase.admob.showInterstitial = function (arg) {
// Interstitial ads must be loaded before they can be shown, so adding a listener
var InterstitialAdListener = com.google.android.gms.ads.AdListener.extend({
onAdLoaded: function () {
firebase.admob.interstitialView.show();
resolve();
console.log('showInterstitial -> onAdLoaded');
firebase.admob.interstitialView.show();
},
onAdFailedToLoad: function (errorCode) {
reject(errorCode);
console.log('showInterstitial -> onAdFailedToLoad');
reject(errorCode);
},
onAdClosed: function () {
firebase.admob.interstitialView.setAdListener(null);
console.log('showInterstitial -> onAdClosed #1');
firebase.admob.interstitialView.setAdListener(null);
firebase.admob.interstitialView = null;
console.log('showInterstitial -> onAdClosed #2');
resolve();
}
});
firebase.admob.interstitialView.setAdListener(new InterstitialAdListener());
Expand Down