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 AppEvent = { [eventName: string]: any };
|
7 |
|
8 | type PropsType = React.ComponentProps<typeof View> & {
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | bannerSize:
|
23 | | 'banner'
|
24 | | 'largeBanner'
|
25 | | 'mediumRectangle'
|
26 | | 'fullBanner'
|
27 | | 'leaderboard'
|
28 | | 'smartBannerPortrait'
|
29 | | 'smartBannerLandscape';
|
30 | |
31 |
|
32 |
|
33 | adUnitID?: string;
|
34 | |
35 |
|
36 |
|
37 | testDeviceID?: string;
|
38 | |
39 |
|
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 |
|
50 | type StateType = {
|
51 | style: { width?: number; height?: number };
|
52 | };
|
53 |
|
54 | export 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 |
|
112 | const ExpoBannerView = requireNativeViewManager('ExpoAdsPublisherBannerView');
|