-
Notifications
You must be signed in to change notification settings - Fork 12
/
RNDFPBanner.js
136 lines (117 loc) · 3.99 KB
/
RNDFPBanner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import React from 'react';
import {
requireNativeComponent,
ViewPropTypes,
View,
} from 'react-native';
import PropTypes from 'prop-types';
const RNBanner = requireNativeComponent('RNDFPBanner', DFPBanner);
export default class DFPBanner extends React.Component {
constructor() {
super();
this.onSizeChange = this.onSizeChange.bind(this);
this.onDidFailToReceiveAdWithError = this.onDidFailToReceiveAdWithError.bind(this);
this.onAdmobDispatchAppEvent = this.onAdmobDispatchAppEvent.bind(this);
this.state = {
style: {},
};
}
onSizeChange({nativeEvent}) {
const { height, width } = nativeEvent;
this.setState({ style: { width, height } });
}
onDidFailToReceiveAdWithError({nativeEvent}) {
if (this.props.didFailToReceiveAdWithError) {
this.props.didFailToReceiveAdWithError( nativeEvent.error );
}
}
onAdmobDispatchAppEvent(event) {
if (this.props.admobDispatchAppEvent) {
this.props.admobDispatchAppEvent(event);
}
}
render() {
const { adUnitID, testDeviceID, dimensions, style } = this.props;
let { bannerSize, adSizes } = this.props;
// Dimensions gets highest priority
if (dimensions && dimensions.width && dimensions.height) {
bannerSize = undefined;
adSizes = undefined;
}
// AdSizes gets second priority
if (adSizes && adSizes.length > 0) {
bannerSize = undefined;
}
// Default to something if nothing is set
if (!bannerSize && (!dimensions || !dimensions.width || !dimensions.height) && (!adSizes || !adSizes.length > 0)) {
bannerSize = 'smartBannerPortrait';
}
return (
<View style={this.props.style}>
<RNBanner
style={this.state.style}
onSizeChange={this.onSizeChange}
onAdViewDidReceiveAd={this.props.adViewDidReceiveAd}
onDidFailToReceiveAdWithError={this.onDidFailToReceiveAdWithError}
onAdViewWillPresentScreen={this.props.adViewWillPresentScreen}
onAdViewWillDismissScreen={this.props.adViewWillDismissScreen}
onAdViewDidDismissScreen={this.props.adViewDidDismissScreen}
onAdViewWillLeaveApplication={this.props.adViewWillLeaveApplication}
onAdmobDispatchAppEvent={this.onAdmobDispatchAppEvent}
adSizes={adSizes}
dimensions={dimensions}
testDeviceID={testDeviceID}
adUnitID={adUnitID}
bannerSize={bannerSize} />
</View>
);
}
}
DFPBanner.propTypes = {
style: ViewPropTypes.style,
/**
* AdMob iOS library banner size constants
* (https://developers.google.com/admob/ios/banner)
* banner (320x50, Standard Banner for Phones and Tablets)
* largeBanner (320x100, Large Banner for Phones and Tablets)
* mediumRectangle (300x250, IAB Medium Rectangle for Phones and Tablets)
* fullBanner (468x60, IAB Full-Size Banner for Tablets)
* leaderboard (728x90, IAB Leaderboard for Tablets)
* smartBannerPortrait (Screen width x 32|50|90, Smart Banner for Phones and Tablets)
* smartBannerLandscape (Screen width x 32|50|90, Smart Banner for Phones and Tablets)
*
* banner is default
*/
bannerSize: PropTypes.string,
/**
* Custom banner size (instead of using bannerSize)
*/
dimensions: PropTypes.shape({
height: PropTypes.number,
width: PropTypes.number,
}),
/**
* Array of some combination of bannerSize and dimensions that are valid for the ad
* Example: ['mediumRectangle', { width: 320, height: 400 }, 'smartBannerPortrait']
*/
adSizes: PropTypes.array,
/**
* AdMob ad unit ID
*/
adUnitID: PropTypes.string,
/**
* Test device ID
*/
testDeviceID: PropTypes.string,
/**
* AdMob iOS library events
*/
adViewDidReceiveAd: PropTypes.func,
didFailToReceiveAdWithError: PropTypes.func,
adViewWillPresentScreen: PropTypes.func,
adViewWillDismissScreen: PropTypes.func,
adViewDidDismissScreen: PropTypes.func,
adViewWillLeaveApplication: PropTypes.func,
admobDispatchAppEvent: PropTypes.func,
// ...View.propTypes,
};