This repository has been 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
/
NativeAdsManager.js
118 lines (98 loc) · 3.7 KB
/
NativeAdsManager.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
import {NativeModules, NativeEventEmitter} from 'react-native';
import {EventEmitter, EmitterSubscription} from 'fbemitter';
const {CTKNativeAdManager, CTKNativeAdEmitter} = NativeModules;
const nativeAdEmitter = new NativeEventEmitter(CTKNativeAdEmitter);
const EVENT_DID_BECOME_VALID = 'AdsManagerDidBecomeValid';
type AdManagerCachePolicy = 'none' | 'icon' | 'image' | 'all';
class NativeAdsManager {
/** {@string} with placement id of ads **/
placementId: string;
/** {@number} of ads to request at once **/
adsToRequest: number;
/** {@boolean} indicating whether AdsManager is ready to serve ads **/
isValid: boolean = false;
/** {@EventEmitter} used for sending out updates **/
eventEmitter: EventEmitter = new EventEmitter();
static async registerViewsForInteractionAsync(nativeAdViewTag: number,
mediaViewTag: number,
adIconViewTag: number,
clickable: Array<number>) {
if(adIconViewTag>0 && mediaViewTag>0 ){
clickable.push(mediaViewTag,adIconViewTag)
}else if(mediaViewTag > 0){
clickable.push(mediaViewTag)
}else if(adIconViewTag > 0){
clickable.push(adIconViewTag)
}
console.log(nativeAdViewTag);
console.log(mediaViewTag);
console.log(adIconViewTag);
console.log(clickable);
let result = await CTKNativeAdManager.registerViewsForInteraction(
nativeAdViewTag,
mediaViewTag,
adIconViewTag,
clickable);
return result
}
/**
* Creates an instance of AdsManager with a given placementId and adsToRequest.
* Default number of ads to request is `10`.
*
* AdsManager will become loading ads immediately
*/
constructor(placementId: string, adsToRequest: number = 10) {
this.placementId = placementId;
this.adsToRequest = adsToRequest;
this._listenForStateChanges();
CTKNativeAdManager.init(placementId, adsToRequest);
}
/**
* Listens for AdManager state changes and updates internal state. When it changes,
* callers will be notified of a change
*/
_listenForStateChanges() {
nativeAdEmitter.addListener('CTKNativeAdsManagersChanged', managers => {
const isValidNew = managers[this.placementId];
const isValid = this.isValid;
if (isValid !== isValidNew && isValidNew) {
this.isValid = true;
this.eventEmitter.emit(EVENT_DID_BECOME_VALID);
}
});
}
/**
* Used to listening for state changes
*
* If manager already became valid, it will call the function w/o registering
* handler for events
*/
onAdsLoaded(func: Function): EmitterSubscription {
if (this.isValid) {
setTimeout(func);
return {
remove: () => {
},
};
}
return this.eventEmitter.once(EVENT_DID_BECOME_VALID, func);
}
/**
* Disables auto refreshing for this native ad manager
*/
disableAutoRefresh() {
CTKNativeAdManager.disableAutoRefresh(this.placementId);
}
/**
* Set the native ads manager caching policy. This controls which media from
* the native ads are cached before the onAdsLoaded is called.
* The default is to not block on caching.
*/
setMediaCachePolicy(cachePolicy: AdManagerCachePolicy) {
CTKNativeAdManager.setMediaCachePolicy(this.placementId, cachePolicy);
}
toJSON() {
return this.placementId;
}
}
export default NativeAdsManager;