Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/add_support_for_fabric_native_components_in_nativeadview #424

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Versions

## x.x.x
* Add support for Fabric Native Components in NativeAdView.
* Add support for Turbo Native Modules.
## 8.2.0
* Depend on Android SDK 13.1.0 and iOS SDK 13.1.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import com.applovin.mediation.nativeAds.MaxNativeAdView;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.facebook.react.views.view.ReactViewGroup;

Expand All @@ -44,6 +45,7 @@ public class AppLovinMAXNativeAdView
private static final int ADVERTISER_VIEW_TAG = 8;

private final ReactContext reactContext;
private final int surfaceId;
@Nullable
private MaxNativeAdLoader adLoader;
@Nullable
Expand Down Expand Up @@ -76,6 +78,7 @@ public AppLovinMAXNativeAdView(final Context context)
{
super( context );
reactContext = (ReactContext) context;
surfaceId = UIManagerHelper.getSurfaceId( context );
}

public void destroy()
Expand Down Expand Up @@ -108,19 +111,26 @@ public void setCustomData(@Nullable final String value)
customData = value;
}

public void setExtraParameters(@Nullable final ReadableMap readableMap)
public void setExtraParameters(@Nullable final ReadableArray extraParameters)
{
if ( readableMap != null )
{
extraParameters = readableMap.toHashMap();
}
this.extraParameters = AppLovinMAXUtils.convertReadbleArrayToHashMap( extraParameters );
}

public void setLocalExtraParameters(@Nullable final ReadableMap readableMap)
public void setLocalExtraParameters(@Nullable final ReadableArray localExtraParameters)
{
if ( readableMap != null )
Map<String, Object> localExtraParametersMap = AppLovinMAXUtils.convertReadbleArrayToHashMap( localExtraParameters );

if ( localExtraParametersMap == null ) return;

// Accumulate the result since this function may be called multiple times
// to handle different value types, including string, number, boolean, and null.
if ( this.localExtraParameters != null )
{
localExtraParameters = readableMap.toHashMap();
this.localExtraParameters.putAll( localExtraParametersMap );
}
else
{
this.localExtraParameters = localExtraParametersMap;
}
}

Expand Down Expand Up @@ -170,6 +180,45 @@ public void loadAd()
}
}

public void updateAssetView(final int assetViewTag, final String assetViewName)
{
if ( "TitleView".equals( assetViewName ) )
{
setTitleView( assetViewTag );
}
else if ( "AdvertiserView".equals( assetViewName ) )
{
setAdvertiserView( assetViewTag );
}
else if ( "BodyView".equals( assetViewName ) )
{
setBodyView( assetViewTag );
}
else if ( "CallToActionView".equals( assetViewName ) )
{
setCallToActionView( assetViewTag );
}
else if ( "IconView".equals( assetViewName ) )
{
setIconView( assetViewTag );
}
else if ( "OptionsView".equals( assetViewName ) )
{
setOptionsView( assetViewTag );
}
else if ( "MediaView".equals( assetViewName ) )
{
setMediaView( assetViewTag );
}
}

public void renderNativeAd()
{
// Renders the ad only after the last asset view is set
renderNativeAdHandler.removeCallbacksAndMessages( null );
renderNativeAdHandler.postDelayed( renderNativeAdTask, 50 );
}

/// Ad Loader Listener

private class NativeAdListener
Expand Down Expand Up @@ -211,14 +260,14 @@ public void onNativeAdLoadFailed(@NonNull final String adUnitId, @NonNull final
AppLovinMAXModuleImpl.e( "Failed to load native ad for Ad Unit ID " + adUnitId + " with error: " + error );

WritableMap loadFailedInfo = AppLovinMAXModuleImpl.getInstance().getAdLoadFailedInfo( adUnitId, error );
reactContext.getJSModule( RCTEventEmitter.class ).receiveEvent( getId(), AppLovinMAXAdEvents.ON_AD_LOAD_FAILED_EVENT, loadFailedInfo );
AppLovinMAXModuleImpl.getInstance().sendReactNativeViewEvent( surfaceId, getId(), AppLovinMAXAdEvents.ON_AD_LOAD_FAILED_EVENT, loadFailedInfo );
}

@Override
public void onNativeAdClicked(@NonNull final MaxAd ad)
{
WritableMap adInfo = AppLovinMAXModuleImpl.getInstance().getAdInfo( ad );
reactContext.getJSModule( RCTEventEmitter.class ).receiveEvent( getId(), AppLovinMAXAdEvents.ON_AD_CLICKED_EVENT, adInfo );
AppLovinMAXModuleImpl.getInstance().sendReactNativeViewEvent( surfaceId, getId(), AppLovinMAXAdEvents.ON_AD_CLICKED_EVENT, adInfo );
}
}

Expand All @@ -228,7 +277,7 @@ public void onNativeAdClicked(@NonNull final MaxAd ad)
public void onAdRevenuePaid(@NonNull final MaxAd ad)
{
WritableMap adInfo = AppLovinMAXModuleImpl.getInstance().getAdInfo( ad );
reactContext.getJSModule( RCTEventEmitter.class ).receiveEvent( getId(), AppLovinMAXAdEvents.ON_AD_REVENUE_PAID_EVENT, adInfo );
AppLovinMAXModuleImpl.getInstance().sendReactNativeViewEvent( surfaceId, getId(), AppLovinMAXAdEvents.ON_AD_REVENUE_PAID_EVENT, adInfo );
}

/// Native Ad Component Methods
Expand Down Expand Up @@ -337,14 +386,6 @@ public void setIconView(final int tag)
view.setImageDrawable( icon.getDrawable() );
}
}
else
{
ImageView iconView = (ImageView) nativeAd.getNativeAd().getIconView();
if ( iconView != null )
{
view.setImageDrawable( iconView.getDrawable() );
}
}
}

public void setOptionsView(final int tag)
Expand Down Expand Up @@ -448,12 +489,6 @@ public void onSetProps()
{
loadAd();
}
else
{
// Renders the ad only after the last asset view is set
renderNativeAdHandler.removeCallbacksAndMessages( null );
renderNativeAdHandler.postDelayed( renderNativeAdTask, 50 );
}
}

@Override
Expand Down Expand Up @@ -516,9 +551,6 @@ private void sendAdLoadedReactNativeEventForAd(final MaxNativeAd ad)
nativeAdInfo.putBoolean( "isOptionsViewAvailable", ( ad.getOptionsView() != null ) );
nativeAdInfo.putBoolean( "isMediaViewAvailable", ( ad.getMediaView() != null ) );

WritableMap adInfo = AppLovinMAXModuleImpl.getInstance().getAdInfo( nativeAd );
adInfo.putMap( "nativeAd", nativeAdInfo );

// 2. NativeAd for `AppLovinNativeAdView.js` to render the views

WritableMap jsNativeAd = Arguments.createMap();
Expand All @@ -544,20 +576,16 @@ else if ( icon.getDrawable() != null )
jsNativeAd.putBoolean( "image", true );
}
}
else if ( ad.getIconView() != null )
{
jsNativeAd.putBoolean( "image", true );
}

jsNativeAd.putBoolean( "isOptionsViewAvailable", ( ad.getOptionsView() != null ) );
jsNativeAd.putBoolean( "isMediaViewAvailable", ( ad.getMediaView() != null ) );

WritableMap arg = Arguments.createMap();
arg.putMap( "adInfo", adInfo );
arg.putMap( "nativeAd", jsNativeAd );
WritableMap adInfo = AppLovinMAXModuleImpl.getInstance().getAdInfo( nativeAd );
adInfo.putMap( "nativeAd", nativeAdInfo );
adInfo.putMap( "nativeAdImpl", jsNativeAd );

// Send to `AppLovinNativeAdView.js`
reactContext.getJSModule( RCTEventEmitter.class ).receiveEvent( getId(), AppLovinMAXAdEvents.ON_AD_LOADED_EVENT, arg );
AppLovinMAXModuleImpl.getInstance().sendReactNativeViewEvent( surfaceId, getId(), AppLovinMAXAdEvents.ON_AD_LOADED_EVENT, adInfo );
}

private void maybeDestroyCurrentAd()
Expand Down

This file was deleted.

Loading