1 | import { requireNativeViewManager } from '@unimodules/core';
|
2 | import * as React from 'react';
|
3 | import { View } from 'react-native';
|
4 |
|
5 | type AppEvent = { [eventName: string]: any };
|
6 |
|
7 | type PropsType = React.ComponentProps<typeof View> & {
|
8 | |
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | bannerSize:
|
22 | | 'banner'
|
23 | | 'largeBanner'
|
24 | | 'mediumRectangle'
|
25 | | 'fullBanner'
|
26 | | 'leaderboard'
|
27 | | 'smartBannerPortrait'
|
28 | | 'smartBannerLandscape';
|
29 | |
30 |
|
31 |
|
32 | adUnitID?: string;
|
33 | |
34 |
|
35 |
|
36 | additionalRequestParams?: { [key: string]: string };
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | servePersonalizedAds?: boolean;
|
45 | |
46 |
|
47 |
|
48 | onAdViewDidReceiveAd?: () => void;
|
49 | onDidFailToReceiveAdWithError?: (error: string) => void;
|
50 | onAdViewWillPresentScreen?: () => void;
|
51 | onAdViewWillDismissScreen?: () => void;
|
52 | onAdViewDidDismissScreen?: () => void;
|
53 | onAdViewWillLeaveApplication?: () => void;
|
54 | onAdMobDispatchAppEvent?: (event: AppEvent) => void;
|
55 | };
|
56 |
|
57 | type StateType = {
|
58 | style: { width?: number; height?: number };
|
59 | };
|
60 |
|
61 | let _hasWarnedAboutTestDeviceID = false;
|
62 |
|
63 | export default class PublisherBanner extends React.Component<PropsType, StateType> {
|
64 | static defaultProps = { bannerSize: 'smartBannerPortrait' };
|
65 |
|
66 | state = { style: {} };
|
67 |
|
68 | _handleSizeChange = ({ nativeEvent }: { nativeEvent: { width: number; height: number } }) => {
|
69 | const { height, width } = nativeEvent;
|
70 | this.setState({ style: { width, height } });
|
71 | };
|
72 |
|
73 | _handleDidFailToReceiveAdWithError = ({ nativeEvent }: { nativeEvent: { error: string } }) =>
|
74 | this.props.onDidFailToReceiveAdWithError &&
|
75 | this.props.onDidFailToReceiveAdWithError(nativeEvent.error);
|
76 |
|
77 | render() {
|
78 | const additionalRequestParams: { [key: string]: string } = {
|
79 | ...this.props.additionalRequestParams,
|
80 | };
|
81 | if (!this.props.servePersonalizedAds) {
|
82 | additionalRequestParams.npa = '1';
|
83 | }
|
84 | if ((this.props as any).testDeviceID && !_hasWarnedAboutTestDeviceID) {
|
85 | console.warn(
|
86 | 'The `testDeviceID` prop of PublisherBanner is deprecated. Test device IDs are now set globally. Use AdMob.setTestDeviceID instead.'
|
87 | );
|
88 | _hasWarnedAboutTestDeviceID = true;
|
89 | }
|
90 | return (
|
91 | <View style={this.props.style}>
|
92 | <ExpoBannerView
|
93 | style={this.state.style}
|
94 | adUnitID={this.props.adUnitID}
|
95 | bannerSize={this.props.bannerSize}
|
96 | onSizeChange={this._handleSizeChange}
|
97 | additionalRequestParams={additionalRequestParams}
|
98 | onAdViewDidReceiveAd={this.props.onAdViewDidReceiveAd}
|
99 | onDidFailToReceiveAdWithError={this._handleDidFailToReceiveAdWithError}
|
100 | onAdViewWillPresentScreen={this.props.onAdViewWillPresentScreen}
|
101 | onAdViewWillDismissScreen={this.props.onAdViewWillDismissScreen}
|
102 | onAdViewDidDismissScreen={this.props.onAdViewDidDismissScreen}
|
103 | onAdViewWillLeaveApplication={this.props.onAdViewWillLeaveApplication}
|
104 | onAdmobDispatchAppEvent={this.props.onAdMobDispatchAppEvent}
|
105 | />
|
106 | </View>
|
107 | );
|
108 | }
|
109 | }
|
110 |
|
111 | const ExpoBannerView = requireNativeViewManager('ExpoAdsPublisherBannerView');
|