Skip to content

Commit

Permalink
release v0.7.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarahm-ed committed Aug 29, 2024
1 parent 23e9f4c commit 9fa9c55
Show file tree
Hide file tree
Showing 18 changed files with 255 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import androidx.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class EventEmitter {

public static void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params != null ? params : Arguments.createMap());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,20 @@ 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 + ":" + name, event);

EventEmitter.sendEvent(mContext, CacheManager.EVENT_AD_PRELOAD_ERROR + ":" + name, event);
notifyOnAdsLoadFailed(adError);
return;
}

if (retryCount >= totalRetryCount) {
WritableMap event = Arguments.createMap();
WritableMap event = getDefaultEventData();
WritableMap error = Arguments.createMap();
error.putString("message", "reach maximum retry");
error.putInt("code", AdRequest.ERROR_CODE_INTERNAL_ERROR);
error.putString("domain", "");
event.putMap("error", error);
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_PRELOAD_ERROR + ":" + name, event);
EventEmitter.sendEvent( mContext, CacheManager.EVENT_AD_PRELOAD_ERROR + ":" + name, event);
notifyOnAdsLoadFailed(adError);
return;
}
Expand All @@ -110,36 +111,38 @@ public void run() {
@Override
public void onAdImpression() {
super.onAdImpression();
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_IMPRESSION + ":" + name, null);
EventEmitter.sendEvent(mContext, CacheManager.EVENT_AD_IMPRESSION + ":" + name, getDefaultEventData());

}

@Override
public void onAdClosed() {
super.onAdClosed();
EventEmitter.sendEvent((ReactContext) mContext, CacheManager.EVENT_AD_CLOSED + ":" + name, null);
WritableMap map = Arguments.createMap();
EventEmitter.sendEvent(mContext, CacheManager.EVENT_AD_CLOSED + ":" + name, getDefaultEventData());

}

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

}

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

}

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

EventEmitter.sendEvent(mContext, CacheManager.EVENT_AD_PRELOAD_LOADED + ":" + name, getDefaultEventData());

retryCount = 0;
if (mediation) {
loadingAdRequestCount--;
Expand All @@ -154,7 +157,13 @@ public void onAdLoaded() {
};

setConfiguration(config);
}

private WritableMap getDefaultEventData() {
WritableMap map = Arguments.createMap();
map.putString("adUnitId", adUnitId);
map.putString("repo", name);
return map;
}

private void notifyOnAdsLoadFailed(LoadAdError adError){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.ammarahmed.rnadmob.nativeads;

import android.content.Context;
import android.graphics.Color;
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 All @@ -18,15 +16,13 @@
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MediaContent;
import com.google.android.gms.ads.VideoOptions;
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
import com.google.android.gms.ads.nativead.AdChoicesView;
import com.google.android.gms.ads.nativead.MediaView;
import com.google.android.gms.ads.nativead.NativeAd;
import com.google.android.gms.ads.nativead.NativeAdOptions;
Expand Down Expand Up @@ -67,7 +63,6 @@ public class RNAdmobNativeView extends LinearLayout {
private Handler handler;

AdListener adListener = new AdListener() {

@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
Expand Down Expand Up @@ -126,11 +121,6 @@ public void onNativeAdLoaded(NativeAd ad) {
if (nativeAd != null) {
nativeAd.destroy();
}

if (ad != null) {
nativeAd = ad;
setNativeAd();
}
loadingAd = false;
setNativeAdToJS(ad);
}
Expand Down Expand Up @@ -185,9 +175,10 @@ private Method getDeclaredMethod(Object obj, String name) {
return null;
}

private void setNativeAdToJS(NativeAd nativeAd) {

private void setNativeAdToJS(NativeAd ad) {
try {
nativeAd = ad;
setNativeAd();
WritableMap args = Arguments.createMap();
args.putString("headline", nativeAd.getHeadline());
args.putString("tagline", nativeAd.getBody());
Expand Down Expand Up @@ -307,12 +298,13 @@ private void getAdFromRepository() {
unifiedNativeAdContainer = CacheManager.instance.getNativeAd(adRepo);

if (unifiedNativeAdContainer != null) {
if (nativeAd != null) {
nativeAd.destroy();
}
nativeAd = unifiedNativeAdContainer.unifiedNativeAd;
nativeAdView.setNativeAd(nativeAd);
if (mediaView != null) {
nativeAdView.setMediaView(mediaView);
mediaView.requestLayout();
setNativeAd();
}
setNativeAdToJS(nativeAd);
}
Expand Down Expand Up @@ -394,6 +386,9 @@ public void setNativeAd() {
mediaView.setMedia(nativeAd.getMediaContent());
}
}
handler.postDelayed(() -> {
nativeAdView.getRootView().requestLayout();
}, 1000);

}
}
Expand Down
5 changes: 4 additions & 1 deletion example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import List from "./src/List";
import { routes } from "./src/utils";
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
const App = () => {
const [currentRoute, setCurrentRoute] = useState(null);
const [currentRoute, setCurrentRoute] = useState({
type: "banner",
index: 0
});
const [loading, setLoading] = useState(true);

useEffect(() => {
Expand Down
8 changes: 4 additions & 4 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ AdManager.registerRepository({
console.log('registered: ', result);
});

AdManager.subscribe('imageAd', 'onAdPreloadClicked', () => {
console.log('click', 'imageAd');
AdManager.subscribe('imageAd', 'onAdPreloadClicked', (event) => {
console.log('click', 'imageAd', event);
});

AdManager.subscribe("imageAd", "onAdPreloadImpression", () => {
console.log('impression recorded', 'imageAd');
AdManager.subscribe("imageAd", "onAdPreloadImpression", (event) => {
console.log('impression recorded', 'imageAd', event);
});

AppRegistry.registerComponent(appName, () => App);
16 changes: 7 additions & 9 deletions example/src/AdView.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, {useCallback, useEffect, useRef, useState} from 'react';
import React, { useEffect, useRef, useState } from 'react';
import {
ActivityIndicator,
DeviceEventEmitter,
Platform,
Text,
View,
View
} from 'react-native';
import NativeAdView, {
AdvertiserView,
Expand All @@ -13,11 +12,10 @@ import NativeAdView, {
IconView,
StarRatingView,
StoreView,
TaglineView,
TestIds,
TaglineView
} from 'react-native-admob-native-ads';
import {MediaView} from './MediaView';
import {Events, Logger} from './utils';
import { MediaView } from './MediaView';
import { Logger } from './utils';

export const AdView = React.memo(({index, media, type, loadOnMount = true}) => {
const [aspectRatio, setAspectRatio] = useState(1.5);
Expand Down Expand Up @@ -101,8 +99,8 @@ export const AdView = React.memo(({index, media, type, loadOnMount = true}) => {
mediationOptions={{
nativeBanner: true,
}}
adUnitID={type === 'image' ? TestIds.Image : TestIds.Video} // REPLACE WITH NATIVE_AD_VIDEO_ID for video ads.
// repository={type === 'image' ? 'imageAd' : 'videoAd'}
// adUnitID={type === 'image' ? TestIds.Image : TestIds.Video} // REPLACE WITH NATIVE_AD_VIDEO_ID for video ads.
repository={type === 'image' ? 'imageAd' : 'videoAd'}
>
<View
style={{
Expand Down
21 changes: 14 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ type AdRepositoryConfig = {
targetingOptions?: TargetingOptions;
adChoicesPlacement?: "topLeft" | "topRight" | "bottomLeft" | "bottomRight";
mediaAspectRatio?: "any" | "landscape" | "portrait" | "square" | "unknown";
swipeGestureDirection?: "right" | "left" | "up" | "down",
tapsAllowed?: boolean
swipeGestureDirection?: "right" | "left" | "up" | "down";
tapsAllowed?: boolean;
};

type ImagePropsWithOptionalSource = Omit<ImageProps, "source"> &
Expand Down Expand Up @@ -255,8 +255,8 @@ type NativeAdViewProps = {
targetingOptions?: TargetingOptions;
enableSwipeGestureOptions: {
tapsAllowed?: boolean;
swipeGestureDirection?: "right" | "left" | "up" | "down";
}
swipeGestureDirection?: "right" | "left" | "up" | "down";
};

testDevices?: Array<string>;
onAdOpened?: () => void;
Expand All @@ -267,7 +267,6 @@ type NativeAdViewProps = {
onAdLoaded?: () => void;
onNativeAdLoaded?: (event: NativeAd) => void;
onAdFailedToLoad?: (error: { message: string }) => void;
adBadgeProps: NestedTextProps
};

type SimpleViewProps = {
Expand Down Expand Up @@ -415,11 +414,19 @@ declare module "react-native-admob-native-ads" {
| "onAdPreloadClosed"
| "onAdPreloadClicked"
| "onAdPreloadImpression",
listener
listener: (event: {
adUnitId: string;
repo: string;
error?: {
message: string;
code: number;
domain: string;
};
}) => void
) => EmitterSubscription;

openAdInspector: () => void;
openDebugMenu: (adUnitId: string) => void
openDebugMenu: (adUnitId: string) => void;
};

export const AdOptions: options;
Expand Down
2 changes: 1 addition & 1 deletion ios/RNAdMobManager/EventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ - (void)sendEvent:(NSString *) name dict:(NSDictionary *)dict
{

if (hasListeners) { // Only send events if anyone is listening
[self sendEventWithName:name body:dict];
[self sendEventWithName:name body:dict ? dict : [NSDictionary dictionary]];
}
}
@end
Expand Down
Loading

0 comments on commit 9fa9c55

Please sign in to comment.