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