Skip to content

Commit

Permalink
add .subscribe function to AdManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarahm-ed committed Jul 5, 2023
1 parent 703616c commit e276958
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void onAdFailedToLoad(LoadAdError adError) {
error.putInt("code", adError.getCode());
error.putString("domain", adError.getDomain());
event.putMap("error", error);
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_PRELOAD_ERROR, event);
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_PRELOAD_ERROR + ":" + name, event);
notifyOnAdsLoadFailed(adError);
return;
}
Expand All @@ -91,7 +91,7 @@ public void onAdFailedToLoad(LoadAdError adError) {
error.putInt("code", AdRequest.ERROR_CODE_INTERNAL_ERROR);
error.putString("domain", "");
event.putMap("error", error);
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_PRELOAD_ERROR, event);
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_PRELOAD_ERROR + ":" + name, event);
notifyOnAdsLoadFailed(adError);
return;
}
Expand All @@ -108,31 +108,36 @@ public void run() {
@Override
public void onAdImpression() {
super.onAdImpression();
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_IMPRESSION, null);
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_IMPRESSION + ":" + name, null);

}

@Override
public void onAdClosed() {
super.onAdClosed();
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_CLOSED, null);
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_CLOSED + ":" + name, null);

}

@Override
public void onAdOpened() {
super.onAdOpened();
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_OPEN, null);
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_OPEN + ":" + name, null);

}

@Override
public void onAdClicked() {
super.onAdClicked();
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_CLICKED, null);
Log.d("RNADMOB", CacheManager.EVENT_AD_CLICKED + ":" + name);
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_CLICKED + ":" + name, null);

}

@Override
public void onAdLoaded() {
super.onAdLoaded();

retryCount = 0;
if (mediation) {
loadingAdRequestCount--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -304,6 +305,7 @@ private void getAdFromRepository() {
if (adListener != null)
adListener.onAdFailedToLoad(new LoadAdError(3, "The requested repo is not registered", "", null, null));
} else {
CacheManager.instance.attachAdListener(adRepo, adListener);
if (CacheManager.instance.numberOfAds(adRepo) != 0) {
unifiedNativeAdContainer = CacheManager.instance.getNativeAd(adRepo);

Expand All @@ -319,10 +321,7 @@ private void getAdFromRepository() {
}
} else {
if (!CacheManager.instance.isLoading(adRepo)) {
CacheManager.instance.attachAdListener(adRepo, adListener);
CacheManager.instance.requestAds(adRepo);
} else {
CacheManager.instance.attachAdListener(adRepo, adListener);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ public void onDropViewInstance(@NonNull RNAdmobNativeView nativeAdWrapper) {
}
}
if (nativeAdWrapper.nativeAdView != null){
nativeAdWrapper.nativeAdView.removeAllViews();
nativeAdWrapper.nativeAdView.destroy();
nativeAdWrapper.removeAllViews();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public static void setTargetingOptions(ReadableMap options, AdManagerAdRequest.B
if (options == null) return;
if (options.hasKey("targets")) {
ReadableArray targets = options.getArray("targets");

for (int i = 0; i < targets.size(); i++) {
ReadableMap target = targets.getMap(i);
String key = target.getString("key");
if (target.getType("value") == ReadableType.Array) {
List list = Arguments.toList(target.getArray("value"));

adRequest.addCustomTargeting(key, list);
} else {
adRequest.addCustomTargeting(key, target.getString("value"));
Expand Down
9 changes: 7 additions & 2 deletions ios/RNAdMobManager/CacheManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
@interface CacheManager:NSObject

+ (CacheManager*)sharedInstance;
+ (NSString*) EVENT_AD_PRELOAD_LOADED;
+ (NSString*) EVENT_AD_PRELOAD_ERROR;
+ (NSString *)EVENT_AD_PRELOAD_LOADED:(NSString *)name;
+ (NSString *)EVENT_AD_PRELOAD_ERROR:(NSString *)name;
+ (NSString *)EVENT_AD_CLOSED:(NSString *)name;
+ (NSString *)EVENT_AD_OPEN:(NSString *)name;
+ (NSString *)EVENT_AD_CLICKED:(NSString *)name;
+ (NSString *)EVENT_AD_IMPRESSION:(NSString *)name;


-(BOOL) isLoading:(NSString*) id;
-(NSInteger) numberOfAds:(NSString*) id;
Expand Down
8 changes: 6 additions & 2 deletions ios/RNAdMobManager/CacheManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ @implementation CacheManager{
NSMutableDictionary *repositoriesMap;
}

+ (NSString *)EVENT_AD_PRELOAD_LOADED { return @"onAdPreloadLoaded"; }
+ (NSString *)EVENT_AD_PRELOAD_ERROR { return @"onAdPreloadError"; }
+ (NSString *)EVENT_AD_PRELOAD_LOADED:(NSString *)name { return [@"onAdPreloadLoaded" stringByAppendingString:[@":" stringByAppendingString:name]]; }
+ (NSString *)EVENT_AD_PRELOAD_ERROR:(NSString *)name { return [@"onAdPreloadError" stringByAppendingString:[@":" stringByAppendingString:name]]; }
+ (NSString *)EVENT_AD_CLOSED:(NSString *)name { return [@"onAdPreloadClosed" stringByAppendingString:[@":" stringByAppendingString:name]]; }
+ (NSString *)EVENT_AD_OPEN:(NSString *)name { return [@"onAdPreloadOpen" stringByAppendingString:[@":" stringByAppendingString:name]]; }
+ (NSString *)EVENT_AD_CLICKED:(NSString *)name { return [@"onAdPreloadClicked" stringByAppendingString:[@":" stringByAppendingString:name]]; }
+ (NSString *)EVENT_AD_IMPRESSION:(NSString *)name { return [@"onAdPreloadImpression" stringByAppendingString:[@":" stringByAppendingString:name]]; }

static CacheManager *_sharedInstance = nil;

Expand Down
21 changes: 19 additions & 2 deletions ios/RNAdMobManager/RNAdMobUnifiedAdQueueWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,29 @@ - (void)adLoader:(nonnull GADAdLoader *)adLoader didReceiveNativeAd:(nonnull GAD
for (id<AdListener> listener in [attachedAdListeners copy]){
[listener didAdLoaded:nativeAd];
}
[EventEmitter.sharedInstance sendEvent:[CacheManager EVENT_AD_PRELOAD_LOADED:_name] dict:nil];

if (loadingAdRequestCount == 0){
[self fillAds];//fill up repository if need
}
// The adLoader has finished loading ads, and a new request can be sent.
}

- (void)nativeAdDidRecordClick:(GADNativeAd *)nativeAd {
[EventEmitter.sharedInstance sendEvent:[CacheManager EVENT_AD_CLICKED:_name] dict:nil];
}

- (void)nativeAdDidRecordImpression:(GADNativeAd *)nativeAd {
[EventEmitter.sharedInstance sendEvent:[CacheManager EVENT_AD_IMPRESSION:_name] dict:nil];
}

- (void)nativeAdWillPresentScreen:(GADNativeAd *)nativeAd {
[EventEmitter.sharedInstance sendEvent:[CacheManager EVENT_AD_OPEN:_name] dict:nil];
}

- (void)nativeAdDidDismissScreen:(GADNativeAd *)nativeAd {
[EventEmitter.sharedInstance sendEvent:[CacheManager EVENT_AD_CLOSED:_name] dict:nil];
}

- (void)adLoader:(nonnull GADAdLoader *)adLoader didFailToReceiveAdWithError:(nonnull NSError *)error {
if(_isMediationEnabled){
Expand Down Expand Up @@ -230,7 +246,8 @@ - (void)adLoader:(nonnull GADAdLoader *)adLoader didFailToReceiveAdWithError:(no
@"error":errorDic,
};

[EventEmitter.sharedInstance sendEvent:CacheManager.EVENT_AD_PRELOAD_ERROR dict:event];
[EventEmitter.sharedInstance sendEvent:[CacheManager EVENT_AD_PRELOAD_ERROR:_name] dict:event];

for (id<AdListener> listener in [attachedAdListeners copy]){
[listener didFailToReceiveAdWithError:error];
}
Expand All @@ -246,7 +263,7 @@ - (void)adLoader:(nonnull GADAdLoader *)adLoader didFailToReceiveAdWithError:(no
NSDictionary *event = @{
@"error":errorDic,
};
[EventEmitter.sharedInstance sendEvent:CacheManager.EVENT_AD_PRELOAD_ERROR dict:event];
[EventEmitter.sharedInstance sendEvent:[CacheManager EVENT_AD_PRELOAD_ERROR:_name] dict:event];
for (id<AdListener> listener in [attachedAdListeners copy]){
[listener didFailToReceiveAdWithError:error];
}
Expand Down
7 changes: 6 additions & 1 deletion ios/RNAdMobManager/RNGADNativeView.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ - (void)setTargetingOptions:(NSDictionary *)targetingOptions {
NSArray *allKeys = [targetingOptions allKeys];

if ([allKeys containsObject:@"targets"]) {
[adRequest setCustomTargeting:(NSDictionary *) [targetingOptions objectForKey:@"targets"]];
NSArray *array = [targetingOptions objectForKey:@"targets"];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
for (NSDictionary *object in array) {
[dic setValue:[object valueForKey:@"key"] forKey:[object valueForKey:@"value"]];
}
[adRequest setCustomTargeting:dic];
}

if ([allKeys containsObject:@"categoryExclusions"]) {
Expand Down
2 changes: 1 addition & 1 deletion ios/RNAdMobManager/UnifiedNativeAdLoadedListener.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ - (void)adLoader:(nonnull GADAdLoader *)adLoader didReceiveNativeAd:(nonnull GAD

NSMutableDictionary* args = [[NSMutableDictionary alloc] init];
[args setObject:[NSNumber numberWithInteger:_nativeAds.count] forKey:_repo];
[EventEmitter.sharedInstance sendEvent:CacheManager.EVENT_AD_PRELOAD_LOADED dict:args];
[EventEmitter.sharedInstance sendEvent:[CacheManager EVENT_AD_PRELOAD_LOADED:_repo] dict:args];
}

- (void)adLoader:(nonnull GADAdLoader *)adLoader didFailToReceiveAdWithError:(nonnull NSError *)error {
Expand Down
17 changes: 12 additions & 5 deletions src/AdManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {NativeModules} from 'react-native';
import { AdOptions } from './utils';
import { DeviceEventEmitter, NativeModules } from "react-native";
import { AdOptions } from "./utils";

const RNAdmobNativeAdsManager = NativeModules.RNAdmobNativeAdsManager;

Expand All @@ -12,8 +12,10 @@ async function isTestDevice() {
}

function registerRepository(config) {
config.mediaAspectRatio = AdOptions.mediaAspectRatio[config.mediaAspectRatio || "unknown"];
config.adChoicesPlacement = AdOptions.adChoicesPlacement[config.adChoicesPlacement || "topRight"];
config.mediaAspectRatio =
AdOptions.mediaAspectRatio[config.mediaAspectRatio || "unknown"];
config.adChoicesPlacement =
AdOptions.adChoicesPlacement[config.adChoicesPlacement || "topRight"];
return RNAdmobNativeAdsManager.registerRepository(config);
}

Expand All @@ -29,11 +31,16 @@ async function resetCache() {
return RNAdmobNativeAdsManager.resetCache();
}

function subscribe(repo, eventName, listener) {
return DeviceEventEmitter.addListener(`${eventName}:${repo}`, listener);
}

export default {
setRequestConfiguration,
isTestDevice,
registerRepository,
hasAd,
unRegisterRepository,
resetCache,
}
subscribe,
};

0 comments on commit e276958

Please sign in to comment.