This repository was archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwithNativeAd.js
215 lines (183 loc) · 8.47 KB
/
withNativeAd.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import * as React from 'react';
import {EmitterSubscription} from 'fbemitter';
import {requireNativeComponent, findNodeHandle, Text, View} from 'react-native';
import AdsManager from './NativeAdsManager';
import {NativeAdIconView} from './AdIconViewManager';
import {NativeMediaView} from './MediaViewManager';
import {MediaView, AdIconView, AdChoicesView} from './index'
const NativeAdView = requireNativeComponent('CTKNativeAd', null);
type NativeAd = {
advertiserName: ?string,
bodyText: ?string,
callToActionText: ?string,
headline: ?string,
linkDescription: ?string,
promotedTranslation: ?string,
socialContext: ?string,
sponsoredTranslation: ?string,
translation: ?string,
};
type NativeAdWrapperState = {
ad: ?NativeAd,
canRequestAds: boolean,
mediaViewNodeHandle: number,
adIconViewNodeHandle: number,
clickableChildren: Set<number>,
};
type NativeAdWrapperProps = {
adsManager: AdsManager,
onAdLoaded?: ?(?NativeAd) => void,
adChoicePosition?: ?string
};
type MultipleRegisterablesContextValueType = {
unregister: (React.Node => void) | null,
register: (React.Node => void) | null,
};
type RegisterableContextValueType = {
register: (React.Node => void) | null,
unregister: (() => void) | null,
};
export type TriggerableContextValueType = MultipleRegisterablesContextValueType;
export type AdIconViewContextValueType = RegisterableContextValueType;
export type MediaViewContextValueType = RegisterableContextValueType;
/**
* Higher order function that wraps given `Component` and provides `nativeAd` as a prop
*
* In case of an empty ad or adsManager not yet ready for displaying ads, null will be
* returned instead of a component provided.
*/
const defaultValue = {register: null, unregister: null};
export const TriggerableContext = React.createContext(defaultValue);
export const MediaViewContext = React.createContext(defaultValue);
export const AdIconViewContext = React.createContext(defaultValue);
export default <T>(Component: React.ComponentType<T>) =>
class NativeAdWrapper extends React.Component<NativeAdWrapperProps & T, NativeAdWrapperState> {
_subscription: ?EmitterSubscription;
_nativeAdViewRef: ?NativeAdView;
_registerFunctionsForTriggerables: TriggerableContextValueType;
_registerFunctionsForMediaView: MediaViewContextValueType;
_registerFunctionsForAdIconView: AdIconViewContextValueType;
_clickableChildrenNodeHandles: { [React.Node]: number };
constructor(props: NativeAdWrapperProps & T) {
super(props);
this._registerFunctionsForTriggerables = {
register: this._registerClickableChild,
unregister: this._unregisterClickableChild,
};
this._registerFunctionsForMediaView = {
unregister: this._unregisterMediaView,
register: this._registerMediaView,
};
this._registerFunctionsForAdIconView = {
unregister: this._unregisterAdIconView,
register: this._registerAdIconView,
};
this._clickableChildrenNodeHandles = {};
this.state = {
ad: null,
// iOS requires a nonnull value
mediaViewNodeHandle: -1,
adIconViewNodeHandle: -1,
clickableChildren: new Set(),
canRequestAds: false
};
}
/**
* On init, register for updates on `adsManager` to know when it becomes available
*/
componentDidMount() {
this._subscription = this.props.adsManager.onAdsLoaded(() =>
this.setState({canRequestAds: true})
);
}
componentDidUpdate(prevProps: NativeAdWrapperProps, prevState: NativeAdWrapperState) {
if (this.state.mediaViewNodeHandle !== -1 || this.state.adIconViewNodeHandle !== -1 || [...this.state.clickableChildren] > 0) {
const mediaViewNodeHandleChanged =
this.state.mediaViewNodeHandle !== prevState.mediaViewNodeHandle;
const adIconViewNodeHandleChanged =
this.state.adIconViewNodeHandle !== prevState.adIconViewNodeHandle;
const clickableChildrenChanged = [...prevState.clickableChildren].filter(
child => !this.state.clickableChildren.has(child)
);
if (mediaViewNodeHandleChanged || adIconViewNodeHandleChanged || clickableChildrenChanged) {
console.log('update');
AdsManager.registerViewsForInteractionAsync(
findNodeHandle(this._nativeAdViewRef),
this.state.mediaViewNodeHandle,
this.state.adIconViewNodeHandle,
[...this.state.clickableChildren]
);
}
}
}
/**
* Clear subscription when component goes off screen
*/
componentWillUnmount() {
if (this._subscription) {
this._subscription.remove();
}
}
_registerMediaView = (mediaView: NativeMediaView) =>
this.setState({mediaViewNodeHandle: findNodeHandle(mediaView)});
_unregisterMediaView = () => this.setState({mediaViewNodeHandle: -1});
_registerAdIconView = (adIconView: NativeAdIconView) =>
this.setState({adIconViewNodeHandle: findNodeHandle(adIconView)});
_unregisterAdIconView = () => this.setState({adIconViewNodeHandle: -1});
_registerClickableChild = (child: React.Node) => {
this._clickableChildrenNodeHandles[child] = findNodeHandle(child);
this.setState({clickableChildren: this.state.clickableChildren.add(findNodeHandle(child))});
};
_unregisterClickableChild = (child: React.Node) => {
this.setState(({clickableChildren}) => {
const newClickableChildren = new Set(clickableChildren);
newClickableChildren.delete(this._clickableChildrenNodeHandles[child]);
delete this._clickableChildrenNodeHandles[child];
return {clickableChildren: newClickableChildren};
});
};
_handleAdUpdated = () => this.props.onAdLoaded && this.props.onAdLoaded(this.state.ad);
_handleAdLoaded = ({nativeEvent}: { nativeEvent: NativeAd }) => {
this.setState({ad: nativeEvent}, this._handleAdUpdated);
};
_handleNativeAdViewMount = (ref: ?NativeAdView) => {
this._nativeAdViewRef = ref;
};
renderAdComponent(componentProps: T) {
const {adsManager,adChoicePosition,expandable} = this.props;
if (this.state.ad) {
return (
<AdIconViewContext.Provider value={this._registerFunctionsForAdIconView}>
<MediaViewContext.Provider value={this._registerFunctionsForMediaView}>
<TriggerableContext.Provider value={this._registerFunctionsForTriggerables}>
{/* In case of no AdIconView or MediaView in Custom layout,
It will keep Triggerable component Functional */}
<AdIconView nativeAd={this.state.ad} style={{width: 0, height: 0}}/>
<MediaView nativeAd={this.state.ad} style={{width: 0, height: 0}}/>
<Component {...componentProps} nativeAd={this.state.ad}/>
<AdChoicesView placementId={adsManager.toJSON()}
expandable={expandable || true}
position={adChoicePosition}/>
</TriggerableContext.Provider>
</MediaViewContext.Provider>
</AdIconViewContext.Provider>
);
}
return null;
}
render() {
const {adsManager, ...props} = this.props;
delete props.onAdLoaded;
if (!this.state.canRequestAds) {
return null;
}
return (
<NativeAdView
ref={this._handleNativeAdViewMount}
adsManager={adsManager.toJSON()}
onAdLoaded={this._handleAdLoaded}>
{this.renderAdComponent(props)}
</NativeAdView>
);
}
};