showAdIfAvailable method

void showAdIfAvailable()

Shows the ad if it is available.

Does nothing if the ad is not available or is already being shown.

Implementation

void showAdIfAvailable() async {
  if (_loadAdFuture != null) {
    _logger.debug('Waiting for ad to load...');
    await _loadAdFuture;
  }

  if (!isAdAvailable) {
    _logger.debug('Tried to show ad before it was available.');
    return;
  }

  if (_isShowingAd) {
    _logger.debug('Tried to show ad while already showing an ad.');
    return;
  }

  _splashAd!.fullScreenContentCallback = FullScreenContentCallback(
    onAdShowedFullScreenContent: (ad) => _isShowingAd = true,
    onAdDismissedFullScreenContent: _disposeAd,
    onAdFailedToShowFullScreenContent: (ad, error) {
      _logger.debug('Failed to show Splash Ad: $error');
      _disposeAd(ad);
    },
    onAdImpression: (ad) {
      _logger.debug('Splash Ad impression');
      final nowUtc = DateTime.now().toUtc();
      _adImpressionController.add(nowUtc);
    },
  );

  return _splashAd!.show();
}