From a85abd2e59b725ae505539d143fe10d7179bbab8 Mon Sep 17 00:00:00 2001 From: Boris Tacyniak Date: Tue, 9 Nov 2021 00:41:16 +0100 Subject: [PATCH] WIP --- .../ReactNativeGoogleAdsConsentModule.java | 12 +++++++++++- .../ReactNativeGoogleAdsInterstitialModule.java | 12 +++++++++++- .../googleads/ReactNativeGoogleAdsModule.java | 12 +++++++++++- .../ReactNativeGoogleAdsRewardedModule.java | 12 +++++++++++- ios/RNGoogleAds/RNGoogleAdsModule.m | 8 ++++++++ jest.setup.ts | 8 ++++---- lib/AdsConsent.js | 2 +- lib/index.js | 5 ++++- lib/internal/GoogleAdsNativeEventEmitter.js | 14 +++++++------- lib/internal/registry/nativeModule.js | 4 +++- 10 files changed, 71 insertions(+), 18 deletions(-) diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsConsentModule.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsConsentModule.java index 93fa031f..201ba59f 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsConsentModule.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsConsentModule.java @@ -38,7 +38,7 @@ import java.util.List; public class ReactNativeGoogleAdsConsentModule extends ReactNativeModule { - private static final String TAG = "GoogleAdsConsent"; + private static final String TAG = "RNGoogleAdsConsentModule"; private ConsentInformation consentInformation; private ConsentForm consentForm; @@ -223,4 +223,14 @@ public void addTestDevices(ReadableArray deviceIds, Promise promise) { } promise.resolve(null); } + + @ReactMethod + public void addListener(String eventName) { + // Keep: Required for RN built in Event Emitter Calls. + } + + @ReactMethod + public void removeListeners(Integer count) { + // Keep: Required for RN built in Event Emitter Calls. + } } diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java index 62eb089a..5905533d 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java @@ -41,7 +41,7 @@ import javax.annotation.Nullable; public class ReactNativeGoogleAdsInterstitialModule extends ReactNativeModule { - private static final String SERVICE = "GoogleAdsInterstitial"; + private static final String SERVICE = "RNGoogleAdsInterstitialModule"; private static SparseArray interstitialAdArray = new SparseArray<>(); public ReactNativeGoogleAdsInterstitialModule(ReactApplicationContext reactContext) { @@ -149,4 +149,14 @@ public void interstitialShow(int requestId, ReadableMap showOptions, Promise pro } }); } + + @ReactMethod + public void addListener(String eventName) { + // Keep: Required for RN built in Event Emitter Calls. + } + + @ReactMethod + public void removeListeners(Integer count) { + // Keep: Required for RN built in Event Emitter Calls. + } } diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsModule.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsModule.java index ad4f0212..a1a59ed2 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsModule.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsModule.java @@ -27,7 +27,7 @@ import java.util.Objects; public class ReactNativeGoogleAdsModule extends ReactNativeModule { - private static final String SERVICE = "GoogleAds"; + private static final String SERVICE = "RNGoogleAdsModule"; ReactNativeGoogleAdsModule(ReactApplicationContext reactContext) { super(reactContext, SERVICE); @@ -90,4 +90,14 @@ public void setRequestConfiguration(ReadableMap requestConfiguration, Promise pr MobileAds.setRequestConfiguration(buildRequestConfiguration(requestConfiguration)); promise.resolve(null); } + + @ReactMethod + public void addListener(String eventName) { + // Keep: Required for RN built in Event Emitter Calls. + } + + @ReactMethod + public void removeListeners(Integer count) { + // Keep: Required for RN built in Event Emitter Calls. + } } diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java index 12f80f89..abf9c6bf 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java @@ -27,7 +27,7 @@ import io.invertase.googleads.common.ReactNativeModule; public class ReactNativeGoogleAdsRewardedModule extends ReactNativeModule { - private static final String SERVICE = "GoogleAdsRewarded"; + private static final String SERVICE = "RNGoogleAdsRewardedModule"; private static SparseArray rewardedAdArray = new SparseArray<>(); public ReactNativeGoogleAdsRewardedModule(ReactApplicationContext reactContext) { @@ -158,4 +158,14 @@ public void onRewardedAdFailedToShow(int errorCode) { promise.resolve(null); }); } + + @ReactMethod + public void addListener(String eventName) { + // Keep: Required for RN built in Event Emitter Calls. + } + + @ReactMethod + public void removeListeners(Integer count) { + // Keep: Required for RN built in Event Emitter Calls. + } } diff --git a/ios/RNGoogleAds/RNGoogleAdsModule.m b/ios/RNGoogleAds/RNGoogleAdsModule.m index fd7f50ac..1e7c714d 100644 --- a/ios/RNGoogleAds/RNGoogleAdsModule.m +++ b/ios/RNGoogleAds/RNGoogleAdsModule.m @@ -71,4 +71,12 @@ - (void)setRequestConfiguration:(NSDictionary *)requestConfiguration { } } +RCT_EXPORT_METHOD(addListener : (NSString *)eventName) { + // Keep: Required for RN built in Event Emitter Calls. +} + +RCT_EXPORT_METHOD(removeListeners : (NSInteger)count) { + // Keep: Required for RN built in Event Emitter Calls. +} + @end diff --git a/jest.setup.ts b/jest.setup.ts index 2c65847b..9346a0dd 100644 --- a/jest.setup.ts +++ b/jest.setup.ts @@ -9,15 +9,15 @@ jest.doMock('react-native', () => { }, NativeModules: { ...ReactNative.NativeModules, - GoogleAdsModule: { + RNGoogleAdsModule: { addListener: jest.fn(), removeListeners: jest.fn(), eventsAddListener: jest.fn(), eventsNotifyReady: jest.fn(), }, - GoogleAdsInterstitialModule: {}, - GoogleAdsRewardedModule: {}, - GoogleAdsConsentModule: {}, + RNGoogleAdsInterstitialModule: {}, + RNGoogleAdsRewardedModule: {}, + RNGoogleAdsConsentModule: {}, }, }, ReactNative, diff --git a/lib/AdsConsent.js b/lib/AdsConsent.js index eee6f535..7102de6f 100644 --- a/lib/AdsConsent.js +++ b/lib/AdsConsent.js @@ -28,7 +28,7 @@ import { NativeModules } from 'react-native'; import AdsConsentDebugGeography from './AdsConsentDebugGeography'; import AdsConsentStatus from './AdsConsentStatus'; -const native = NativeModules.GoogleAdsConsentModule; +const native = NativeModules.RNGoogleAdsConsentModule; export default { /** diff --git a/lib/index.js b/lib/index.js index fecb590e..cc729d8b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -47,7 +47,10 @@ class GoogleAdsModule extends Module { // import { SDK_VERSION } from '@invertase/react-native-google-ads'; export const SDK_VERSION = version; -export default new GoogleAdsModule(); +export default new GoogleAdsModule('AppName', { + namespace: 'GoogleAds', + nativeModuleName: 'RNGoogleAdsModule' +}); export { default as AdsConsentDebugGeography } from './AdsConsentDebugGeography'; export { default as AdsConsentStatus } from './AdsConsentStatus'; diff --git a/lib/internal/GoogleAdsNativeEventEmitter.js b/lib/internal/GoogleAdsNativeEventEmitter.js index 6f89cb1c..7a2d2282 100644 --- a/lib/internal/GoogleAdsNativeEventEmitter.js +++ b/lib/internal/GoogleAdsNativeEventEmitter.js @@ -17,20 +17,20 @@ import { NativeEventEmitter, NativeModules } from 'react-native'; -const { GoogleAdsModule } = NativeModules; +const { RNGoogleAdsModule } = NativeModules; class GoogleAdsNativeEventEmitter extends NativeEventEmitter { constructor() { - super(GoogleAdsModule); + super(RNGoogleAdsModule); this.ready = false; } addListener(eventType, listener, context) { if (!this.ready) { - GoogleAdsModule.eventsNotifyReady(true); + RNGoogleAdsModule.eventsNotifyReady(true); this.ready = true; } - GoogleAdsModule.eventsAddListener(eventType); + RNGoogleAdsModule.eventsAddListener(eventType); let subscription = super.addListener(`google_ads_${eventType}`, listener, context); @@ -45,7 +45,7 @@ class GoogleAdsNativeEventEmitter extends NativeEventEmitter { // we will modify it to do our native unsubscription then call the original let originalRemove = subscription.remove; let newRemove = () => { - GoogleAdsModule.eventsRemoveListener(eventType, false); + RNGoogleAdsModule.eventsRemoveListener(eventType, false); if (super.removeSubscription != null) { // This is for RN <= 0.64 - 65 and greater no longer have removeSubscription super.removeSubscription(subscription); @@ -59,13 +59,13 @@ class GoogleAdsNativeEventEmitter extends NativeEventEmitter { } removeAllListeners(eventType) { - GoogleAdsModule.eventsRemoveListener(eventType, true); + RNGoogleAdsModule.eventsRemoveListener(eventType, true); super.removeAllListeners(`google_ads_${eventType}`); } // This is likely no longer ever called, but it is here for backwards compatibility with RN <= 0.64 removeSubscription(subscription) { - GoogleAdsModule.eventsRemoveListener(subscription.eventType.replace('google_ads_'), false); + RNGoogleAdsModule.eventsRemoveListener(subscription.eventType.replace('google_ads_'), false); if (super.removeSubscription) { super.removeSubscription(subscription); } diff --git a/lib/internal/registry/nativeModule.js b/lib/internal/registry/nativeModule.js index 55309df8..34fcedf3 100644 --- a/lib/internal/registry/nativeModule.js +++ b/lib/internal/registry/nativeModule.js @@ -24,7 +24,7 @@ const NATIVE_MODULE_REGISTRY = {}; const NATIVE_MODULE_EVENT_SUBSCRIPTIONS = {}; function nativeModuleKey(module) { - return `${module._customUrlOrRegion || ''}:${module.app.name}:${module._config.namespace}`; + return `${module._customUrlOrRegion || ''}:${module.app.name || ''}:${module._config.namespace || ''}`; } /** @@ -90,6 +90,8 @@ function initialiseNativeModule(module) { const { namespace, nativeEvents, nativeModuleName } = config; const nativeModuleNames = [nativeModuleName]; + let multiModuleRoot = {}, multiModule = false; + for (let i = 0; i < nativeModuleNames.length; i++) { const nativeModule = NativeModules[nativeModuleNames[i]];