<commit_before>//
//  AdMobRewardedInterstitialAd.swift
//  ee-x-4abf52fd
//
//  Created by eps on 1/29/21.
//

import GoogleMobileAds

private let kTag = "\(AdMobRewardedInterstitialAd.self)"

internal class AdMobRewardedInterstitialAd: NSObject, IFullScreenAd, GADFullScreenContentDelegate {
    private let _bridge: IMessageBridge
    private let _logger: ILogger
    private let _adId: String
    private let _messageHelper: MessageHelper
    private var _helper: FullScreenAdHelper?
    private var _isLoaded = false
    private var _ad: GADRewardedInterstitialAd?
    private var _rewarded = false

    init(_ bridge: IMessageBridge,
         _ logger: ILogger,
         _ adId: String) {
        _bridge = bridge
        _logger = logger
        _adId = adId
        _messageHelper = MessageHelper("AdMobRewardedInterstitialAd", _adId)
        super.init()
        _helper = FullScreenAdHelper(_bridge, self, _messageHelper)
        registerHandlers()
    }

    func destroy() {
        deregisterHandlers()
    }

    func registerHandlers() {
        _helper?.registerHandlers()
    }

    func deregisterHandlers() {
        _helper?.deregisterHandlers()
    }

    var isLoaded: Bool {
        return _isLoaded
    }

    func load() {
        Thread.runOnMainThread {
            GADRewardedInterstitialAd.load(withAdUnitID: self._adId, request: GADRequest()) { ad, error in
                if error == nil {
                    Thread.runOnMainThread {
                        self._logger.debug("\(kTag): \(#function): succeeded")
                        self._isLoaded = true
                        self._ad = ad
                        self._ad?.fullScreenContentDelegate = self
                        self._bridge.callCpp(self._messageHelper.onLoaded)
                    }
                } else {
                    Thread.runOnMainThread {
                        self._logger.debug("\(kTag): \(#function): failed error = \(error?.localizedDescription ?? "")")
                        self._bridge.callCpp(self._messageHelper.onFailedToLoad, error?.localizedDescription ?? "")
                    }
                }
            }
        }
    }

    func show() {
        Thread.runOnMainThread {
            guard let ad = self._ad else {
                self._bridge.callCpp(self._messageHelper.onFailedToShow, "Null ad")
                return
            }
            guard let rootView = Utils.getCurrentRootViewController() else {
                assert(false, "Ad is not initialized")
                return
            }
            self._rewarded = false
            ad.present(fromRootViewController: rootView) {
                self._rewarded = true
            }
        }
    }

    func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        Thread.runOnMainThread {
            self._logger.debug("\(kTag): \(#function)")
        }
    }

    func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
        Thread.runOnMainThread {
            self._logger.debug("\(kTag): \(#function): message = \(error.localizedDescription)")
            self._isLoaded = false
            self._ad = nil
            self._bridge.callCpp(self._messageHelper.onFailedToShow, error.localizedDescription)
        }
    }

    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        Thread.runOnMainThread {
            self._logger.debug("\(kTag): \(#function)")
            self._isLoaded = false
            self._ad = nil
            self._bridge.callCpp(self._messageHelper.onClosed)
        }
    }
}<commit_msg>Fix assertion (rewarded inters - ios).<commit_after>//
//  AdMobRewardedInterstitialAd.swift
//  ee-x-4abf52fd
//
//  Created by eps on 1/29/21.
//

import GoogleMobileAds

private let kTag = "\(AdMobRewardedInterstitialAd.self)"

internal class AdMobRewardedInterstitialAd: NSObject, IFullScreenAd, GADFullScreenContentDelegate {
    private let _bridge: IMessageBridge
    private let _logger: ILogger
    private let _adId: String
    private let _messageHelper: MessageHelper
    private var _helper: FullScreenAdHelper?
    private var _isLoaded = false
    private var _ad: GADRewardedInterstitialAd?
    private var _rewarded = false

    init(_ bridge: IMessageBridge,
         _ logger: ILogger,
         _ adId: String) {
        _bridge = bridge
        _logger = logger
        _adId = adId
        _messageHelper = MessageHelper("AdMobRewardedInterstitialAd", _adId)
        super.init()
        _helper = FullScreenAdHelper(_bridge, self, _messageHelper)
        registerHandlers()
    }

    func destroy() {
        deregisterHandlers()
    }

    func registerHandlers() {
        _helper?.registerHandlers()
    }

    func deregisterHandlers() {
        _helper?.deregisterHandlers()
    }

    var isLoaded: Bool {
        return _isLoaded
    }

    func load() {
        Thread.runOnMainThread {
            GADRewardedInterstitialAd.load(withAdUnitID: self._adId, request: GADRequest()) { ad, error in
                if error == nil {
                    Thread.runOnMainThread {
                        self._logger.debug("\(kTag): \(#function): succeeded")
                        self._isLoaded = true
                        self._ad = ad
                        self._ad?.fullScreenContentDelegate = self
                        self._bridge.callCpp(self._messageHelper.onLoaded)
                    }
                } else {
                    Thread.runOnMainThread {
                        self._logger.debug("\(kTag): \(#function): failed error = \(error?.localizedDescription ?? "")")
                        self._bridge.callCpp(self._messageHelper.onFailedToLoad, error?.localizedDescription ?? "")
                    }
                }
            }
        }
    }

    func show() {
        Thread.runOnMainThread {
            guard let ad = self._ad else {
                self._bridge.callCpp(self._messageHelper.onFailedToShow, "Null ad")
                return
            }
            guard let rootView = Utils.getCurrentRootViewController() else {
                assert(false, "Ad is not initialized")
                return
            }
            self._rewarded = false
            ad.present(fromRootViewController: rootView) {
                self._rewarded = true
            }
        }
    }

    func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        Thread.runOnMainThread {
            self._logger.debug("\(kTag): \(#function)")
        }
    }

    func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
        Thread.runOnMainThread {
            self._logger.debug("\(kTag): \(#function): message = \(error.localizedDescription)")
            self._isLoaded = false
            self._ad = nil
            self._bridge.callCpp(self._messageHelper.onFailedToShow, error.localizedDescription)
        }
    }

    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        Thread.runOnMainThread {
            self._logger.debug("\(kTag): \(#function)")
            self._isLoaded = false
            self._ad = nil
            self._bridge.callCpp(self._messageHelper.onClosed, Utils.toString(self._rewarded))
        }
    }
}
{'fraction_non_alphanumeric': 0.09180462443562731, 'fraction_numerical': 0.0021890819537556437, 'mean_word_length': 1.8790862544308784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2975161', 'n_tokens_mistral': 2070, 'n_tokens_neox': 1959, 'n_words': 379}