UNPKG

3.81 kBTypeScriptView Raw
1import { requireNativeViewManager } from '@unimodules/core';
2import PropTypes from 'prop-types';
3import * as React from 'react';
4import { View, ViewPropTypes } from 'react-native';
5
6type AppEvent = { [eventName: string]: any };
7
8type PropsType = React.ComponentProps<typeof View> & {
9 /**
10 * AdMob iOS library banner size constants
11 * (https://developers.google.com/admob/ios/banner)
12 * banner (320x50, Standard Banner for Phones and Tablets)
13 * largeBanner (320x100, Large Banner for Phones and Tablets)
14 * mediumRectangle (300x250, IAB Medium Rectangle for Phones and Tablets)
15 * fullBanner (468x60, IAB Full-Size Banner for Tablets)
16 * leaderboard (728x90, IAB Leaderboard for Tablets)
17 * smartBannerPortrait (Screen width x 32|50|90, Smart Banner for Phones and Tablets)
18 * smartBannerLandscape (Screen width x 32|50|90, Smart Banner for Phones and Tablets)
19 *
20 * banner is default
21 */
22 bannerSize:
23 | 'banner'
24 | 'largeBanner'
25 | 'mediumRectangle'
26 | 'fullBanner'
27 | 'leaderboard'
28 | 'smartBannerPortrait'
29 | 'smartBannerLandscape';
30 /**
31 * AdMob ad unit ID
32 */
33 adUnitID?: string;
34 /**
35 * Test device ID
36 */
37 testDeviceID?: string;
38 /**
39 * AdMob iOS library events
40 */
41 onAdViewDidReceiveAd?: () => void;
42 onDidFailToReceiveAdWithError?: (error: string) => void;
43 onAdViewWillPresentScreen?: () => void;
44 onAdViewWillDismissScreen?: () => void;
45 onAdViewDidDismissScreen?: () => void;
46 onAdViewWillLeaveApplication?: () => void;
47 onAdMobDispatchAppEvent?: (event: AppEvent) => void;
48};
49
50type StateType = {
51 style: { width?: number; height?: number };
52};
53
54export default class PublisherBanner extends React.Component<PropsType, StateType> {
55 static propTypes = {
56 bannerSize: PropTypes.oneOf([
57 'banner',
58 'largeBanner',
59 'mediumRectangle',
60 'fullBanner',
61 'leaderboard',
62 'smartBannerPortrait',
63 'smartBannerLandscape',
64 ]),
65 adUnitID: PropTypes.string,
66 testDeviceID: PropTypes.string,
67 onAdViewDidReceiveAd: PropTypes.func,
68 onDidFailToReceiveAdWithError: PropTypes.func,
69 onAdViewWillPresentScreen: PropTypes.func,
70 onAdViewWillDismissScreen: PropTypes.func,
71 onAdViewDidDismissScreen: PropTypes.func,
72 onAdViewWillLeaveApplication: PropTypes.func,
73 onAdmobDispatchAppEvent: PropTypes.func,
74 ...ViewPropTypes,
75 };
76
77 static defaultProps = { bannerSize: 'smartBannerPortrait' };
78
79 state = { style: {} };
80
81 _handleSizeChange = ({ nativeEvent }: { nativeEvent: { width: number; height: number } }) => {
82 const { height, width } = nativeEvent;
83 this.setState({ style: { width, height } });
84 };
85
86 _handleDidFailToReceiveAdWithError = ({ nativeEvent }: { nativeEvent: { error: string } }) =>
87 this.props.onDidFailToReceiveAdWithError &&
88 this.props.onDidFailToReceiveAdWithError(nativeEvent.error);
89
90 render() {
91 return (
92 <View style={this.props.style}>
93 <ExpoBannerView
94 style={this.state.style}
95 adUnitID={this.props.adUnitID}
96 bannerSize={this.props.bannerSize}
97 testDeviceID={this.props.testDeviceID}
98 onSizeChange={this._handleSizeChange}
99 onAdViewDidReceiveAd={this.props.onAdViewDidReceiveAd}
100 onDidFailToReceiveAdWithError={this._handleDidFailToReceiveAdWithError}
101 onAdViewWillPresentScreen={this.props.onAdViewWillPresentScreen}
102 onAdViewWillDismissScreen={this.props.onAdViewWillDismissScreen}
103 onAdViewDidDismissScreen={this.props.onAdViewDidDismissScreen}
104 onAdViewWillLeaveApplication={this.props.onAdViewWillLeaveApplication}
105 onAdmobDispatchAppEvent={this.props.onAdMobDispatchAppEvent}
106 />
107 </View>
108 );
109 }
110}
111
112const ExpoBannerView = requireNativeViewManager('ExpoAdsPublisherBannerView');