Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import com.facebook.react.viewmanagers.RNGoogleMobileAdsMediaViewManagerDelegate
import com.facebook.react.viewmanagers.RNGoogleMobileAdsMediaViewManagerInterface

@ReactModule(name = ReactNativeGoogleMobileAdsMediaViewManager.NAME)
class ReactNativeGoogleMobileAdsMediaViewManager(
reactContext: ReactApplicationContext
) : ViewGroupManager<ReactNativeGoogleMobileAdsMediaView>(reactContext),
class ReactNativeGoogleMobileAdsMediaViewManager : ViewGroupManager<ReactNativeGoogleMobileAdsMediaView>(),
RNGoogleMobileAdsMediaViewManagerInterface<ReactNativeGoogleMobileAdsMediaView> {
private val delegate: ViewManagerDelegate<ReactNativeGoogleMobileAdsMediaView> = RNGoogleMobileAdsMediaViewManagerDelegate(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package io.invertase.googlemobileads
*/

import android.view.View
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.uimanager.ThemedReactContext
Expand All @@ -29,9 +28,7 @@ import com.facebook.react.viewmanagers.RNGoogleMobileAdsNativeViewManagerDelegat
import com.facebook.react.viewmanagers.RNGoogleMobileAdsNativeViewManagerInterface

@ReactModule(name = ReactNativeGoogleMobileAdsNativeAdViewManager.NAME)
class ReactNativeGoogleMobileAdsNativeAdViewManager(
reactContext: ReactApplicationContext
) : ViewGroupManager<ReactNativeGoogleMobileAdsNativeAdView>(reactContext),
class ReactNativeGoogleMobileAdsNativeAdViewManager : ViewGroupManager<ReactNativeGoogleMobileAdsNativeAdView>(),
RNGoogleMobileAdsNativeViewManagerInterface<ReactNativeGoogleMobileAdsNativeAdView> {
private val delegate: ViewManagerDelegate<ReactNativeGoogleMobileAdsNativeAdView> = RNGoogleMobileAdsNativeViewManagerDelegate(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap
import com.facebook.react.module.annotations.ReactModule
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdLoader
Expand All @@ -30,8 +31,10 @@ import com.google.android.gms.ads.VideoController.VideoLifecycleCallbacks
import com.google.android.gms.ads.VideoOptions
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdOptions
import io.invertase.googlemobileads.common.ReactNativeEventEmitter
import io.invertase.googlemobileads.interfaces.NativeEvent

@ReactModule(ReactNativeGoogleMobileAdsNativeModule.NAME)
@ReactModule(name = ReactNativeGoogleMobileAdsNativeModule.NAME)
class ReactNativeGoogleMobileAdsNativeModule(
reactContext: ReactApplicationContext
) : NativeGoogleMobileAdsNativeModuleSpec(reactContext) {
Expand Down Expand Up @@ -201,7 +204,12 @@ class ReactNativeGoogleMobileAdsNativeModule(
val payload = Arguments.createMap()
payload.putString("responseId", nativeAd.responseInfo?.responseId)
payload.putString("type", type)
[email protected](payload)

val emitter = ReactNativeEventEmitter.getSharedInstance()
emitter.sendEvent(object : NativeEvent {
override fun getEventName() = "RNGMANativeAdEvent"
override fun getEventBody() = payload
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class ReactNativeGoogleMobileAdsPackage : TurboReactPackage() {
): List<ViewManager<*, *>> {
return listOf(
ReactNativeGoogleMobileAdsBannerAdViewManager(),
ReactNativeGoogleMobileAdsNativeAdViewManager(reactContext),
ReactNativeGoogleMobileAdsMediaViewManager(reactContext)
ReactNativeGoogleMobileAdsNativeAdViewManager(),
ReactNativeGoogleMobileAdsMediaViewManager()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@ import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.modules.core.DeviceEventManagerModule

abstract class NativeGoogleMobileAdsNativeModuleSpec(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
abstract fun load(adUnitId: String, requestOptions: ReadableMap, promise: Promise)
abstract fun destroy(responseId: String)

fun emitOnAdEvent(params: ReadableMap) {
reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit("RNGMANativeAdEvent", params)
}
}
2 changes: 1 addition & 1 deletion ios/RNGoogleMobileAds/RNGoogleMobileAdsFullScreenAd.mm
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,4 @@ - (void)showWithRequestId:(int)requestId

@end

#endif
#endif
3 changes: 1 addition & 2 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsNativeModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
: NativeGoogleMobileAdsNativeModuleSpecBase <NativeGoogleMobileAdsNativeModuleSpec>
#else
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

@interface RNGoogleMobileAdsNativeModule : RCTEventEmitter <RCTBridgeModule>
@interface RNGoogleMobileAdsNativeModule : NSObject <RCTBridgeModule>
#endif

- (GADNativeAd *)nativeAdForResponseId:(NSString *)responseId;
Expand Down
11 changes: 2 additions & 9 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsNativeModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#import "RNGoogleMobileAdsNativeModule.h"
#import "RNGoogleMobileAdsCommon.h"
#import "common/RNRCTEventEmitter.h"

typedef void (^RNGMANativeAdLoadCompletionHandler)(GADNativeAd *_Nullable nativeAd,
NSError *_Nullable error);
Expand Down Expand Up @@ -53,10 +54,6 @@ - (dispatch_queue_t)methodQueue {
(const facebook::react::ObjCTurboModule::InitParams &)params {
return std::make_shared<facebook::react::NativeGoogleMobileAdsNativeModuleSpecJSI>(params);
}
#else
- (NSArray<NSString *> *)supportedEvents {
return @[ @"RNGMANativeAdEvent" ];
}
#endif

- (instancetype)init {
Expand Down Expand Up @@ -278,11 +275,7 @@ - (void)emitAdEvent:(NSString *)type {
}
NSDictionary *payload =
@{@"responseId" : _nativeAd.responseInfo.responseIdentifier, @"type" : type};
#ifdef RCT_NEW_ARCH_ENABLED
[_nativeModule emitOnAdEvent:payload];
#else
[_nativeModule sendEventWithName:@"RNGMANativeAdEvent" body:payload];
#endif
[[RNRCTEventEmitter shared] sendEventWithName:@"RNGMANativeAdEvent" body:payload];
}

@end
Expand Down
9 changes: 6 additions & 3 deletions src/MobileAds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ class MobileAdsModule implements MobileAdsModuleInterface {

subscribeToNativeModuleEvent(eventName: string) {
if (!NATIVE_MODULE_EVENT_SUBSCRIPTIONS[eventName]) {
GoogleMobileAdsNativeEventEmitter.addListener(eventName, event => {
SharedEventEmitter.emit(`${eventName}:${event.adUnitId}:${event.requestId}`, event);
});
GoogleMobileAdsNativeEventEmitter.addListener<{ adUnitId: string; requestId: number }>(
eventName,
event => {
SharedEventEmitter.emit(`${eventName}:${event.adUnitId}:${event.requestId}`, event);
},
);

NATIVE_MODULE_EVENT_SUBSCRIPTIONS[eventName] = true;
}
Expand Down
30 changes: 13 additions & 17 deletions src/ads/native-ad/NativeAd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
*/

import { EventSubscription, NativeEventEmitter, Platform } from 'react-native';
import { EventSubscription } from 'react-native';
import EventEmitter from 'react-native/Libraries/vendor/emitter/EventEmitter';

import { NativeAdEventType } from '../../NativeAdEventType';
Expand All @@ -28,6 +28,7 @@ import NativeGoogleMobileAdsNativeModule, {
} from '../../specs/modules/NativeGoogleMobileAdsNativeModule';
import { NativeAdRequestOptions } from '../../types';
import { validateAdRequestOptions } from '../../validateAdRequestOptions';
import { GoogleMobileAdsNativeEventEmitter } from '../../internal/GoogleMobileAdsNativeEventEmitter';

/**
* A class for loading Native Ads.
Expand Down Expand Up @@ -65,22 +66,17 @@ export class NativeAd {
this.mediaContent = props.mediaContent;
this.extras = props.extras as Record<string, unknown>;

if ('onAdEvent' in NativeGoogleMobileAdsNativeModule) {
this.nativeEventSubscription = NativeGoogleMobileAdsNativeModule.onAdEvent(
this.onNativeAdEvent.bind(this),
);
} else {
let eventEmitter;
if (Platform.OS === 'ios') {
eventEmitter = new NativeEventEmitter(NativeGoogleMobileAdsNativeModule);
} else {
eventEmitter = new NativeEventEmitter();
}
this.nativeEventSubscription = eventEmitter.addListener(
'RNGMANativeAdEvent',
this.onNativeAdEvent.bind(this),
);
}
// Codegen EventEmitter was introduced in RN 0.76.2, so we can't apply it for now, due to backward compatibility.
// if ('onAdEvent' in NativeGoogleMobileAdsNativeModule) {
// this.nativeEventSubscription = NativeGoogleMobileAdsNativeModule.onAdEvent(
// this.onNativeAdEvent.bind(this),
// );
// }

this.nativeEventSubscription = GoogleMobileAdsNativeEventEmitter.addListener(
'RNGMANativeAdEvent',
this.onNativeAdEvent.bind(this),
);
this.eventEmitter = new EventEmitter();
}

Expand Down
4 changes: 2 additions & 2 deletions src/internal/GoogleMobileAdsNativeEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class GANativeEventEmitter extends NativeEventEmitter {
this.ready = false;
}

addListener(
addListener<T>(
eventType: string,
listener: (event: { adUnitId: string; requestId: number }) => void,
listener: (event: T) => void,
context?: Record<string, unknown>,
) {
if (!this.ready) {
Expand Down
10 changes: 3 additions & 7 deletions src/specs/modules/NativeGoogleMobileAdsNativeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@

import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
import type {
Double,
Float,
UnsafeObject,
EventEmitter,
} from 'react-native/Libraries/Types/CodegenTypes';
import type { Double, Float, UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';

export type NativeAdProps = {
responseId: string;
Expand Down Expand Up @@ -58,7 +53,8 @@ export type NativeAdEventPayload = {
export interface Spec extends TurboModule {
load(adUnitId: string, requestOptions: UnsafeObject): Promise<NativeAdProps>;
destroy(responseId: string): void;
readonly onAdEvent: EventEmitter<NativeAdEventPayload>;
// Codegen EventEmitter was introduced in RN 0.76.2, so we can't apply it for now, due to backward compatibility.
// readonly onAdEvent: EventEmitter<NativeAdEventPayload>;
}

export default TurboModuleRegistry.getEnforcing<Spec>('RNGoogleMobileAdsNativeModule');
Loading