showAdIfAvailable method

void showAdIfAvailable()

Implementation

void showAdIfAvailable() {
  if (!isAdAvailable) {
    print('Tried to show ad before available.');
    loadAd();
    return;
  }
  if (_isShowingAd) {
    print('Tried to show ad while already showing an ad.');
    return;
  }
  if (DateTime.now().subtract(maxCacheDuration).isAfter(_appOpenLoadTime!)) {
    print('Maximum cache duration exceeded. Loading another ad.');
    _appOpenAd!.dispose();
    _appOpenAd = null;
    loadAd();
    return;
  }
  // Set the fullScreenContentCallback and show the ad.
  _appOpenAd!.fullScreenContentCallback = FullScreenContentCallback(
    onAdShowedFullScreenContent: (ad) {
      _isShowingAd = true;
      print('$ad onAdShowedFullScreenContent');
    },
    onAdFailedToShowFullScreenContent: (ad, error) {
      print('$ad onAdFailedToShowFullScreenContent: $error');
      _isShowingAd = false;
      ad.dispose();
      _appOpenAd = null;
    },
    onAdDismissedFullScreenContent: (ad) {
      print('$ad onAdDismissedFullScreenContent');
      _isShowingAd = false;
      ad.dispose();
      _appOpenAd = null;
      loadAd();
    },
  );
  (interstitialAdRunning == false)
      ? appOpenAdRunning == true
          ? null
          : getDifferenceAppOpenTime()
              ? _appOpenAd!.show().then((value) {
                  box.write(ArgumentConstant.isAppOpenStartTime,
                      DateTime.now().millisecondsSinceEpoch.toString());
                })
              : null
      : null;
}