handleGrantRights method

Future<bool> handleGrantRights()

Implementation

Future<bool> handleGrantRights() async {
  await _clearAndCancelAdRequest();

  rewardedAdBloc.addEvent(
    const FastRewardedAdBlocEvent.loadAndShowAd(),
  );

  // Wait for the ad to be dismissed
  final response = await RaceStream([
    rewardedAdBloc.onError,
    rewardedAdBloc.onReward,
    rewardedAdBloc.onData.where((FastRewardedAdBlocState state) {
      return state.error != null;
    }),
    rewardedAdBloc.onData.where((FastRewardedAdBlocState state) {
      return state.hasDismissedAd;
    }),
  ]).first;

  if (response is RewardItem) {
    return true;
  }

  if (response is FastRewardedAdBlocState) {
    if (response.hasDismissedAd) {
      return false;
    }

    if (response.error is FastRewardedAdBlocError) {
      debugLog(
        'Error while granting rights',
        debugLabel: 'FastOperationRewardedDialog',
        value: response.error,
      );

      throw response.error as FastRewardedAdBlocError;
    }
  }

  debugLog(
    'Error while granting rights',
    debugLabel: 'FastOperationRewardedDialog',
    value: response,
  );

  throw FastRewardedAdBlocError.unknown;
}