handleLoadAndShowAd method

Stream<FastRewardedAdBlocState> handleLoadAndShowAd()

Handles the event to load and show the rewarded ad.

Implementation

Stream<FastRewardedAdBlocState> handleLoadAndShowAd() async* {
  debugLog('Loading ad...', debugLabel: debugLabel);

  if (currentState.isLoadingAd) {
    debugLog(
      'Ad is already loading. Ignoring event.',
      debugLabel: debugLabel,
    );

    return;
  }

  // Reset state
  final emptyState = _getEmptyState();

  if (isRequestBlocked) {
    debugLog(
      'Ad request is blocked. Ignoring event.',
      debugLabel: debugLabel,
    );

    yield emptyState;

    addEvent(FastRewardedAdBlocEvent.adLoadingError(
      FastRewardedAdBlocError.noAdAvailable,
    ));

    return;
  }

  final requestId = await _admobService!.loadAd();

  debugLog('Ad requested. Request id: $requestId', debugLabel: debugLabel);

  yield emptyState.copyWith(
    error: requestId == null ? FastRewardedAdBlocError.adFailedToLoad : null,
    isLoadingAd: requestId != null,
    requestId: requestId,
  );
}