showAdIfAvailable method

void showAdIfAvailable(
  1. BuildContext context
)

Shows the ad, if one exists and is not already being shown.

If the previously cached ad has expired, this just loads and caches a new ad.

Implementation

void showAdIfAvailable(BuildContext context) {
  if ((getAdsSettings()?.isAllAds ?? false) == false) {
    return;
  }

  if (!isAdAvailable) {
    loadAd(callback: () => showAdIfAvailable(context));
    return;
  }
  if (_isShowingAd) {
    return;
  }
  print("Step 1");
  if (_appOpenLoadTime != null && DateTime.now().subtract(maxCacheDuration).isAfter(_appOpenLoadTime!)) {
    _appOpenAd?.dispose();
    _appOpenAd = null;
    _appLovinOpenAd?.dispose();
    _appLovinOpenAd = null;
    loadAd(callback: () => showAdIfAvailable(context));
    return;
  }
  print("Step 3");
  if (_appOpenAd != null) {
    // Set the fullScreenContentCallback and show the ad.
    _appOpenAd?.fullScreenContentCallback = FullScreenContentCallback(
      onAdShowedFullScreenContent: (ad) {
        _isShowingAd = true;
      },
      onAdFailedToShowFullScreenContent: (ad, error) {
        Navigator.pop(context);
        _isShowingAd = false;
        ad.dispose();
        _appOpenAd = null;
      },
      onAdDismissedFullScreenContent: (ad) {
        Navigator.pop(context);
        _isShowingAd = false;
        ad.dispose();
        _appOpenAd = null;
        loadAd();
      },
    );
    print("Step 4");
    showOpenProgressbarDialog(context);
    _appOpenAd?.show();
  }

  if (_appLovinOpenAd != null) {
    // Set the fullScreenContentCallback and show the ad.
    _appLovinOpenAd?.fullScreenContentCallback = FullScreenContentCallback(
      onAdShowedFullScreenContent: (ad) {
        _isShowingAd = true;
      },
      onAdFailedToShowFullScreenContent: (ad, error) {
        _isShowingAd = false;
        ad.dispose();
        _appLovinOpenAd = null;
      },
      onAdDismissedFullScreenContent: (ad) {
        _isShowingAd = false;
        ad.dispose();
        _appLovinOpenAd = null;
        loadAd();
      },
    );
    _appLovinOpenAd?.show();
  }

}