Skip to content

Commit

Permalink
chore: fix styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Almouro committed Mar 27, 2024
1 parent 3fd0e29 commit cfc4e5c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 70 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
},
env: {
'jest/globals': true,
node: true,
},
rules: {
'jest/no-identical-title': 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ public void onAdLoaded() {
height = reactViewGroup.getHeight();

adView.addOnLayoutChangeListener(
(v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
WritableMap payload = Arguments.createMap();
payload.putDouble("width", PixelUtil.toDIPFromPixel(right - left));
payload.putDouble("height", PixelUtil.toDIPFromPixel(bottom - top));
sendEvent(reactViewGroup, EVENT_SIZE_CHANGE, payload);
});
(v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
WritableMap payload = Arguments.createMap();
payload.putDouble("width", PixelUtil.toDIPFromPixel(right - left));
payload.putDouble("height", PixelUtil.toDIPFromPixel(bottom - top));
sendEvent(reactViewGroup, EVENT_SIZE_CHANGE, payload);
});
} else {
int left = adView.getLeft();
int top = adView.getTop();
Expand All @@ -232,42 +232,42 @@ public void onAdLoaded() {
adView.layout(left, top, left + width, top + height);
}

WritableMap payload = Arguments.createMap();
WritableMap payload = Arguments.createMap();
payload.putDouble("width", PixelUtil.toDIPFromPixel(width));
payload.putDouble("height", PixelUtil.toDIPFromPixel(height));

sendEvent(reactViewGroup, EVENT_AD_LOADED, payload);
}
sendEvent(reactViewGroup, EVENT_AD_LOADED, payload);
}

@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
int errorCode = loadAdError.getCode();
WritableMap payload = ReactNativeGoogleMobileAdsCommon.errorCodeToMap(errorCode);
sendEvent(reactViewGroup, EVENT_AD_FAILED_TO_LOAD, payload);
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
int errorCode = loadAdError.getCode();
WritableMap payload = ReactNativeGoogleMobileAdsCommon.errorCodeToMap(errorCode);
sendEvent(reactViewGroup, EVENT_AD_FAILED_TO_LOAD, payload);
}

@Override
public void onAdOpened() {
sendEvent(reactViewGroup, EVENT_AD_OPENED, null);
}
@Override
public void onAdOpened() {
sendEvent(reactViewGroup, EVENT_AD_OPENED, null);
}

@Override
public void onAdClosed() {
sendEvent(reactViewGroup, EVENT_AD_CLOSED, null);
}
});
@Override
public void onAdClosed() {
sendEvent(reactViewGroup, EVENT_AD_CLOSED, null);
}
});
if (adView instanceof AdManagerAdView) {
((AdManagerAdView) adView)
.setAppEventListener(
new AppEventListener() {
@Override
public void onAppEvent(@NonNull String name, @Nullable String data) {
WritableMap payload = Arguments.createMap();
payload.putString("name", name);
payload.putString("data", data);
sendEvent(reactViewGroup, EVENT_APP_EVENT, payload);
}
});
.setAppEventListener(
new AppEventListener() {
@Override
public void onAppEvent(@NonNull String name, @Nullable String data) {
WritableMap payload = Arguments.createMap();
payload.putString("name", name);
payload.putString("data", data);
sendEvent(reactViewGroup, EVENT_APP_EVENT, payload);
}
});
}
reactViewGroup.addView(adView);
return adView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

import android.content.Context;
import android.widget.FrameLayout;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import java.util.List;

/**
* Using FrameLayout instead of ReactViewGroup
*
* This is because in the case of fluid ads,
* - JS side will usually not specify the ad height
* - Also, after loading the fluid ad, we need to measure the ad height and update the layout
* Which isn't possible with ReactViewGroup since it overrides requestLayout by a noop
* <p>This is because in the case of fluid ads, - JS side will usually not specify the ad height -
* Also, after loading the fluid ad, we need to measure the ad height and update the layout Which
* isn't possible with ReactViewGroup since it overrides requestLayout by a noop
*
* See https://github.com/facebook/react-native/issues/17968 for more details
* <p>See https://github.com/facebook/react-native/issues/17968 for more details
*/
public class ReactNativeAdView extends FrameLayout {
private AdRequest request;
Expand All @@ -32,25 +30,29 @@ public void requestLayout() {
}

/**
* This ensures the adview is properly measured and laid out if its layout changed after being loaded
* This happens everytime for fluid ads, but cannot happen for fixed size ads loading additional content
* This ensures the adview is properly measured and laid out if its layout changed after being
* loaded This happens everytime for fluid ads, but cannot happen for fixed size ads loading
* additional content
*
* See https://github.com/facebook/react-native/issues/17968 for more details
* <p>See https://github.com/facebook/react-native/issues/17968 for more details
*/
private final Runnable measureAndLayout = () -> {
/**
* For fluid ads, we usually don't specify the ad height from JS side, so mark it as
* unspecified and let it dynamically determine its size
*
* See https://developers.google.com/ad-manager/mobile-ads-sdk/android/native/styles#fluid_size
*/
int heightMeasureSpec = isFluid ?
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED) :
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY);

measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY), heightMeasureSpec);
layout(getLeft(), getTop(), getRight(), getTop() + getHeight());
};
private final Runnable measureAndLayout =
() -> {
/**
* For fluid ads, we usually don't specify the ad height from JS side, so mark it as
* unspecified and let it dynamically determine its size
*
* <p>See
* https://developers.google.com/ad-manager/mobile-ads-sdk/android/native/styles#fluid_size
*/
int heightMeasureSpec =
isFluid
? MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
: MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY);

measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY), heightMeasureSpec);
layout(getLeft(), getTop(), getRight(), getTop() + getHeight());
};

public ReactNativeAdView(final Context context) {
super(context);
Expand Down
17 changes: 8 additions & 9 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsBannerComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ - (void)initBanner:(GADAdSize)adSize {
if ([RNGoogleMobileAdsCommon isAdManagerUnit:_unitId]) {
_banner = [[GAMBannerView alloc] initWithAdSize:adSize];


if(GADAdSizeEqualToSize(adSize,GADAdSizeFluid)) {
if (GADAdSizeEqualToSize(adSize, GADAdSizeFluid)) {
_banner.frame = self.bounds;
_banner.autoresizingMask = (UIViewAutoresizingFlexibleWidth);
}
Expand Down Expand Up @@ -94,14 +93,14 @@ - (void)setManualImpressionsEnabled:(BOOL *)manualImpressionsEnabled {
_propsChanged = true;
}

- (GADAdSize) getInitialAdSize {
for (NSValue *sizeValue in _sizes) {
GADAdSize adSize = GADAdSizeFromNSValue(sizeValue);
if (GADAdSizeEqualToSize(adSize, GADAdSizeFluid)) {
return GADAdSizeFluid;
}
- (GADAdSize)getInitialAdSize {
for (NSValue *sizeValue in _sizes) {
GADAdSize adSize = GADAdSizeFromNSValue(sizeValue);
if (GADAdSizeEqualToSize(adSize, GADAdSizeFluid)) {
return GADAdSizeFluid;
}
return GADAdSizeFromNSValue(_sizes[0]);
}
return GADAdSizeFromNSValue(_sizes[0]);
}

- (void)requestAd {
Expand Down
10 changes: 6 additions & 4 deletions src/common/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
export const debounce = <F extends (...args: any[]) => any>(func: F, waitFor: number) => {
export const debounce = <Arguments extends unknown[], Result>(
func: (...args: Arguments) => Result,
waitFor: number,
): ((...args: Arguments) => void) => {
let timeout: ReturnType<typeof setTimeout> | null = null;

const debounced = (...args: Parameters<F>) => {
const debounced = (...args: Arguments) => {
if (timeout !== null) {
clearTimeout(timeout);

Check warning on line 9 in src/common/debounce.ts

View check run for this annotation

Codecov / codecov/patch

src/common/debounce.ts#L9

Added line #L9 was not covered by tests
timeout = null;
}
timeout = setTimeout(() => func(...args), waitFor);

Check warning on line 11 in src/common/debounce.ts

View check run for this annotation

Codecov / codecov/patch

src/common/debounce.ts#L11

Added line #L11 was not covered by tests
};

return debounced as (...args: Parameters<F>) => ReturnType<F>;
return debounced;
};

0 comments on commit cfc4e5c

Please sign in to comment.