Skip to content

Commit

Permalink
feat: inline adaptive banner with custom width
Browse files Browse the repository at this point in the history
  • Loading branch information
dylancom committed Dec 24, 2022
1 parent 1535ba5 commit c0db2f8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,21 @@ static AdSize getAdSize(String preDefinedAdSize, ReactViewGroup reactViewGroup)
return ReactNativeGoogleMobileAdsCommon.getAdSizeForAdaptiveBanner(
preDefinedAdSize, reactViewGroup);
} else {
return ReactNativeGoogleMobileAdsCommon.stringToAdSize(preDefinedAdSize);
return ReactNativeGoogleMobileAdsCommon.stringToAdSize(preDefinedAdSize, reactViewGroup);
}
}

static AdSize stringToAdSize(String value) {
Pattern pattern = Pattern.compile("([0-9]+)x([0-9]+)");
static AdSize stringToAdSize(String value, ReactViewGroup reactViewGroup) {
Pattern pattern = Pattern.compile("([0-9]+)x([0-9]+|)");
Matcher matcher = pattern.matcher(value);

// If size is "valXval"
if (matcher.find()) {
int width = Integer.parseInt(matcher.group(1));
if (matcher.group(2).isEmpty()) {
return AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(
reactViewGroup.getContext(), width);
}
int height = Integer.parseInt(matcher.group(2));
return new AdSize(width, height);
}
Expand Down
10 changes: 7 additions & 3 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsCommon.m
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ + (void)sendAdEvent:(NSString *)event
+ (GADAdSize)stringToAdSize:(NSString *)value {
NSError *error = nil;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"([0-9]+)x([0-9]+)"
[NSRegularExpression regularExpressionWithPattern:@"([0-9]+)x([0-9]+|)"
options:0
error:&error];
NSArray *matches = [regex matchesInString:value options:0 range:NSMakeRange(0, [value length])];
Expand All @@ -173,8 +173,12 @@ + (GADAdSize)stringToAdSize:(NSString *)value {
if (matchText) {
NSArray *values = [matchText componentsSeparatedByString:@"x"];
CGFloat width = (CGFloat)[values[0] intValue];
CGFloat height = (CGFloat)[values[1] intValue];
return GADAdSizeFromCGSize(CGSizeMake(width, height));
if ([values[1] isEqualToString:@""]) {
return GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth(width);
} else {
CGFloat height = (CGFloat)[values[1] intValue];
return GADAdSizeFromCGSize(CGSizeMake(width, height));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ads/BaseAd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type NativeEvent =
data?: string;
};

const sizeRegex = /([0-9]+)x([0-9]+)/;
const sizeRegex = /([0-9]+)x([0-9]+|)/;

export const BaseAd = React.forwardRef<GoogleMobileAdsBannerView, GAMBannerAdProps>(
({ unitId, sizes, requestOptions, manualImpressionsEnabled, ...props }, ref) => {
Expand Down
4 changes: 2 additions & 2 deletions src/types/BannerAdProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export interface BannerAdProps {
unitId: string;

/**
* The size of the banner. Can be a predefined size via `BannerAdSize` or custom dimensions, e.g. `300x200`.
* The size of the banner. Can be a predefined size via `BannerAdSize` or custom dimensions (WxH), e.g. `300x200` or `300x` for adaptive height.
*
* Inventory must be available for the banner size specified, otherwise a no-fill error will be sent to `onAdFailedToLoad`.
*/
size: BannerAdSize | string;
size: BannerAdSize | `${number}x${number | ''}`;

/**
* The request options for this banner.
Expand Down

0 comments on commit c0db2f8

Please sign in to comment.