handleLoadAd method

Implementation

Stream<FastNativeAdBlocState> handleLoadAd(
  FastNativeAdBlocEventPayload payload,
) async* {
  final adInfo = payload.adInfo;
  final country = payload.country;
  final language = payload.language;
  final adId = payload.adId;

  AdWithView? adView;
  FastResponseAd? ad;

  yield currentState.copyWith(isLoadingAd: true, showFallback: false);

  if (adId != null && _adService != null) {
    ad = await _adService!.getAdById(adId);
  }

  if (adInfo.nativeAdmobEnabled &&
      adInfo.nativeAdUnitId != null &&
      ad == null) {
    adView = await _admobService.requestAd(
      adInfo.nativeAdUnitId!,
      countryWhiteList: adInfo.countries,
      keywords: adInfo.keywords,
      country: country,
    );
  }

  if (adView == null && ad == null && _adService != null) {
    if (language != null && country != null) {
      ad = await _adService!.getAdByCountryAndLanguage(country, language);
    }

    if (language != null && ad == null) {
      ad = await _adService!.getAdByLanguage(language);
    }
  }

  addEvent(FastNativeAdBlocEvent.adLoaded(payload.copyWith(
    adView: adView,
    ad: ad,
  )));
}