Skip to content

Commit 0cf6300

Browse files
author
Dominik Hinc
committed
Fix enum bindings on android, fix wrong property being extracted on iOS, pass values specified in documentation to the native side, add customControlsRequested prop
1 parent b7d7c99 commit 0cf6300

File tree

5 files changed

+59
-8
lines changed

5 files changed

+59
-8
lines changed

android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsNativeModule.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ class ReactNativeGoogleMobileAdsNativeModule(
145145
val mediaAspectRatio = if (requestOptions.hasKey("aspectRatio")) {
146146
when (requestOptions.getInt("aspectRatio")) {
147147
1 -> MediaAspectRatio.ANY
148-
2 -> MediaAspectRatio.PORTRAIT
149-
3 -> MediaAspectRatio.LANDSCAPE
148+
2 -> MediaAspectRatio.LANDSCAPE
149+
3 -> MediaAspectRatio.PORTRAIT
150150
4 -> MediaAspectRatio.SQUARE
151151
else -> MediaAspectRatio.UNKNOWN
152152
}
@@ -155,8 +155,8 @@ class ReactNativeGoogleMobileAdsNativeModule(
155155
}
156156
val adChoicesPlacement = if (requestOptions.hasKey("adChoicesPlacement")) {
157157
when (requestOptions.getInt("adChoicesPlacement")) {
158-
0 -> NativeAdOptions.ADCHOICES_TOP_RIGHT
159-
1 -> NativeAdOptions.ADCHOICES_TOP_LEFT
158+
0 -> NativeAdOptions.ADCHOICES_TOP_LEFT
159+
1 -> NativeAdOptions.ADCHOICES_TOP_RIGHT
160160
2 -> NativeAdOptions.ADCHOICES_BOTTOM_RIGHT
161161
3 -> NativeAdOptions.ADCHOICES_BOTTOM_LEFT
162162
else -> NativeAdOptions.ADCHOICES_TOP_RIGHT
@@ -169,8 +169,14 @@ class ReactNativeGoogleMobileAdsNativeModule(
169169
} else {
170170
true
171171
}
172+
val customControlsRequested = if (requestOptions.hasKey("customControlsRequested")) {
173+
requestOptions.getBoolean("customControlsRequested")
174+
} else {
175+
false
176+
}
172177
val videoOptions = VideoOptions.Builder()
173178
.setStartMuted(startVideoMuted)
179+
.setCustomControlsRequested(customControlsRequested)
174180
.build()
175181
val nativeAdOptions = NativeAdOptions.Builder()
176182
// .setReturnUrlsForImageAssets(true)

ios/RNGoogleMobileAds/RNGoogleMobileAdsNativeModule.mm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ - (instancetype)initWithNativeModule:(RNGoogleMobileAdsNativeModule *)nativeModu
162162
}
163163
}
164164
GADNativeAdViewAdOptions *adViewOptions = [[GADNativeAdViewAdOptions alloc] init];
165-
if (requestOptions[@"aspectRatio"]) {
166-
switch ([requestOptions[@"aspectRatio"] intValue]) {
165+
if (requestOptions[@"adChoicesPlacement"]) {
166+
switch ([requestOptions[@"adChoicesPlacement"] intValue]) {
167167
case 0:
168168
adViewOptions.preferredAdChoicesPosition = GADAdChoicesPositionTopLeftCorner;
169169
break;
@@ -182,6 +182,9 @@ - (instancetype)initWithNativeModule:(RNGoogleMobileAdsNativeModule *)nativeModu
182182
if (requestOptions[@"startVideoMuted"]) {
183183
videoOptions.startMuted = [requestOptions[@"startVideoMuted"] boolValue];
184184
}
185+
if (requestOptions[@"customControlsRequested"]) {
186+
videoOptions.customControlsRequested = [requestOptions[@"customControlsRequested"] boolValue];
187+
}
185188

186189
_adLoader = [[GADAdLoader alloc]
187190
initWithAdUnitID:adUnitId

src/common/validate.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,7 @@ export function isOneOf(value: unknown, oneOf: unknown[] = []) {
105105
}
106106
return oneOf.includes(value);
107107
}
108+
109+
export function isNumber(value: unknown) {
110+
return typeof value === 'number';
111+
}

src/types/NativeAdRequestOptions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,12 @@ export interface NativeAdRequestOptions extends RequestOptions {
5252
* - When enabled, your app requests that the video should begin with audio muted.
5353
*/
5454
startVideoMuted?: boolean;
55+
56+
/**
57+
* Disables or enables the custom controls for the video.
58+
* - The custom controls are enabled by default.
59+
* - When disabled, your app requests that the video should not have custom controls.
60+
* - When enabled, your app requests that the video should have custom controls.
61+
*/
62+
customControlsRequested?: boolean;
5563
}

src/validateAdRequestOptions.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ import {
2323
isString,
2424
isUndefined,
2525
isValidUrl,
26+
isNumber,
2627
} from './common';
2728
import { version } from './version';
2829
import { RequestOptions } from './types/RequestOptions';
30+
import { NativeAdRequestOptions } from './types/NativeAdRequestOptions';
2931

30-
export function validateAdRequestOptions(options?: RequestOptions) {
31-
const out: RequestOptions = {
32+
export function validateAdRequestOptions(options?: RequestOptions & NativeAdRequestOptions) {
33+
const out: RequestOptions & NativeAdRequestOptions = {
3234
requestAgent: `rn-invertase-${version}`,
3335
};
3436

@@ -136,5 +138,33 @@ export function validateAdRequestOptions(options?: RequestOptions) {
136138
out.publisherProvidedId = options.publisherProvidedId;
137139
}
138140

141+
if (!isUndefined(options?.adChoicesPlacement)) {
142+
if (!isNumber(options.adChoicesPlacement)) {
143+
throw new Error("'options.adChoicesPlacement' expected a number value");
144+
}
145+
out.adChoicesPlacement = options.adChoicesPlacement;
146+
}
147+
148+
if (!isUndefined(options?.aspectRatio)) {
149+
if (!isNumber(options.aspectRatio)) {
150+
throw new Error("'options.aspectRatio' expected a number value");
151+
}
152+
out.aspectRatio = options.aspectRatio;
153+
}
154+
155+
if (!isUndefined(options?.startVideoMuted)) {
156+
if (!isBoolean(options.startVideoMuted)) {
157+
throw new Error("'options.startVideoMuted' expected a boolean value");
158+
}
159+
out.startVideoMuted = options.startVideoMuted;
160+
}
161+
162+
if (!isUndefined(options?.customControlsRequested)) {
163+
if (!isBoolean(options.customControlsRequested)) {
164+
throw new Error("'options.customControlsRequested' expected a boolean value");
165+
}
166+
out.customControlsRequested = options.customControlsRequested;
167+
}
168+
139169
return out;
140170
}

0 commit comments

Comments
 (0)