loadInterstitial method

Future<void> loadInterstitial()

Preloads an interstitial ad.

Does nothing if AdConfig.interstitialId is null. Automatically retries with exponential backoff on failure.

Throws StateError if not initialized.

Implementation

Future<void> loadInterstitial() async {
  _assertInitialized();
  if (_config.interstitialId == null) return;
  if (_interstitialState == AdState.loading ||
      _interstitialState == AdState.ready) {
    return;
  }
  if (_activeAdCount >= maxConcurrentAds) return;

  _interstitialState = AdState.loading;
  _activeAdCount++;

  final adUnitId = _config.interstitialId!.resolved(
    adType: AdType.interstitial,
  );

  // Use retry strategy for loading
  final loaded = await _retryStrategy.execute<bool>(
    action: () async {
      // In production, this would call InterstitialAd.load()
      // For now, simulate a successful load
      return true;
    },
    onRetry: (attempt, error) {
      // Log retry attempt
    },
  );

  if (loaded == true) {
    _interstitialState = AdState.ready;
    emitEvent(AdEvent(type: AdEventType.loaded, adUnitId: adUnitId));
  } else {
    _interstitialState = AdState.error;
    _activeAdCount--;
    emitEvent(AdEvent(
      type: AdEventType.failedToLoad,
      adUnitId: adUnitId,
      error: const AdError(code: 0, message: 'Failed to load after retries'),
    ));
  }
}