package com.blaze.rtnblazegam.module import android.util.Log import com.blaze.blazesdk.ads.custom_native.BlazeGoogleCustomNativeAdsHandler import com.blaze.gam.BlazeGAM import com.blaze.gam.custom_native.BlazeCustomNativeAdData import com.blaze.gam.custom_native.BlazeGAMCustomNativeAdsDelegate import com.blaze.gam.custom_native.BlazeGamCustomNativeAdRequestInformation import com.facebook.proguard.annotations.DoNotStrip import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule import com.facebook.react.bridge.ReactMethod import com.facebook.react.bridge.ReadableMap import javax.annotation.Nonnull class BlazeGAMSdkModule( private val context: ReactApplicationContext ): ReactContextBaseJavaModule(context) { @Nonnull override fun getName(): String { return NAME } var additionalCustomGAMTargetingProperties: Map = mapOf() private val delegate: BlazeGAMCustomNativeAdsDelegate = object : BlazeGAMCustomNativeAdsDelegate { override fun onGAMCustomNativeAdEvent( eventType: BlazeGoogleCustomNativeAdsHandler.EventType, adData: BlazeCustomNativeAdData ) { val event = SdkModuleJsEvent.OnAdEvent( eventType = eventType, adData = adData ) sendJsEvent(event) } override fun onGAMCustomNativeAdError(errMsg: String) { val event = SdkModuleJsEvent.OnAdErrorEvent( errorMessage = errMsg ) sendJsEvent(event) } override fun customGAMTargetingProperties(requestData: BlazeGamCustomNativeAdRequestInformation): Map { return this@BlazeGAMSdkModule.additionalCustomGAMTargetingProperties } } @ReactMethod @DoNotStrip fun enableCustomNativeAds(options: ReadableMap) { // Initialize the sdk. val options = options.extractEnableCustomNativeAdsOptions() ?: run { Log.e(TAG, "Error extracting params of `enableCustomNativeAds`: $options") return } BlazeGAM.enableCustomNativeAds( context = context, defaultAdsConfig = options.defaultAdConfig?.asNativeModel, delegate = delegate ) } @ReactMethod @DoNotStrip fun disableCustomNativeAds() { BlazeGAM.disableCustomNativeAds() } @ReactMethod @DoNotStrip fun setCustomGAMTargetingProperties(props: ReadableMap) { val props = props.toHashMap() as? Map ?: run { Log.e(TAG, "Error extracting params of `setCustomGAMTargetingProperties`: $props") return } additionalCustomGAMTargetingProperties = props } private fun sendJsEvent(event: SdkModuleJsEvent) { context.emitDeviceEvent(event.eventName, event.params) } companion object { const val NAME = "RTNBlazeGAM" val TAG: String = BlazeGAMSdkModule::class.java.simpleName } }