@vynix-org/react-native-ads-sdk 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AdsSdk.podspec +52 -0
- package/LICENSE +20 -0
- package/README.md +95 -0
- package/android/build.gradle +108 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +5 -0
- package/android/src/main/java/com/adssdk/AdsSdkModule.kt +23 -0
- package/android/src/main/java/com/adssdk/AdsSdkPackage.kt +33 -0
- package/ios/AdsSdk.h +5 -0
- package/ios/AdsSdk.mm +18 -0
- package/lib/module/ads-manager/AppOpenAdsManager.js +131 -0
- package/lib/module/ads-manager/AppOpenAdsManager.js.map +1 -0
- package/lib/module/ads-manager/GoogleMobileAdsManager.js +62 -0
- package/lib/module/ads-manager/GoogleMobileAdsManager.js.map +1 -0
- package/lib/module/ads-manager/InterstitialAdsManager.js +291 -0
- package/lib/module/ads-manager/InterstitialAdsManager.js.map +1 -0
- package/lib/module/ads-manager/NativeAdsManager.js +405 -0
- package/lib/module/ads-manager/NativeAdsManager.js.map +1 -0
- package/lib/module/ads-manager/RewardedAdsManager.js +328 -0
- package/lib/module/ads-manager/RewardedAdsManager.js.map +1 -0
- package/lib/module/ads-manager/RewardedInterstitialAdsManager.js +328 -0
- package/lib/module/ads-manager/RewardedInterstitialAdsManager.js.map +1 -0
- package/lib/module/components/BannerAdComponent.js +159 -0
- package/lib/module/components/BannerAdComponent.js.map +1 -0
- package/lib/module/components/FullscreenStyles.js +162 -0
- package/lib/module/components/FullscreenStyles.js.map +1 -0
- package/lib/module/components/NativeAdComponent.js +172 -0
- package/lib/module/components/NativeAdComponent.js.map +1 -0
- package/lib/module/components/NativeAdFullscreenComponent.js +205 -0
- package/lib/module/components/NativeAdFullscreenComponent.js.map +1 -0
- package/lib/module/components/NativeStyles.js +143 -0
- package/lib/module/components/NativeStyles.js.map +1 -0
- package/lib/module/event/custom-event.js +29 -0
- package/lib/module/event/custom-event.js.map +1 -0
- package/lib/module/hook/useAppOpenAd.js +138 -0
- package/lib/module/hook/useAppOpenAd.js.map +1 -0
- package/lib/module/hook/useInterstitialAd.js +157 -0
- package/lib/module/hook/useInterstitialAd.js.map +1 -0
- package/lib/module/hook/useNativeAd.js +147 -0
- package/lib/module/hook/useNativeAd.js.map +1 -0
- package/lib/module/hook/useRewardedAd.js +166 -0
- package/lib/module/hook/useRewardedAd.js.map +1 -0
- package/lib/module/hook/useRewardedInterstitialAd.js +166 -0
- package/lib/module/hook/useRewardedInterstitialAd.js.map +1 -0
- package/lib/module/index.js +14 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/stores/interstitial-ads-store.js +205 -0
- package/lib/module/stores/interstitial-ads-store.js.map +1 -0
- package/lib/module/stores/native-ads-store.js +204 -0
- package/lib/module/stores/native-ads-store.js.map +1 -0
- package/lib/module/stores/rewarded-ads-store.js +216 -0
- package/lib/module/stores/rewarded-ads-store.js.map +1 -0
- package/lib/module/stores/rewarded-interstitial-ads-store.js +216 -0
- package/lib/module/stores/rewarded-interstitial-ads-store.js.map +1 -0
- package/lib/module/types/ads-types.js +25 -0
- package/lib/module/types/ads-types.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/ads-manager/AppOpenAdsManager.d.ts +38 -0
- package/lib/typescript/src/ads-manager/AppOpenAdsManager.d.ts.map +1 -0
- package/lib/typescript/src/ads-manager/GoogleMobileAdsManager.d.ts +22 -0
- package/lib/typescript/src/ads-manager/GoogleMobileAdsManager.d.ts.map +1 -0
- package/lib/typescript/src/ads-manager/InterstitialAdsManager.d.ts +22 -0
- package/lib/typescript/src/ads-manager/InterstitialAdsManager.d.ts.map +1 -0
- package/lib/typescript/src/ads-manager/NativeAdsManager.d.ts +59 -0
- package/lib/typescript/src/ads-manager/NativeAdsManager.d.ts.map +1 -0
- package/lib/typescript/src/ads-manager/RewardedAdsManager.d.ts +25 -0
- package/lib/typescript/src/ads-manager/RewardedAdsManager.d.ts.map +1 -0
- package/lib/typescript/src/ads-manager/RewardedInterstitialAdsManager.d.ts +25 -0
- package/lib/typescript/src/ads-manager/RewardedInterstitialAdsManager.d.ts.map +1 -0
- package/lib/typescript/src/components/BannerAdComponent.d.ts +22 -0
- package/lib/typescript/src/components/BannerAdComponent.d.ts.map +1 -0
- package/lib/typescript/src/components/FullscreenStyles.d.ts +155 -0
- package/lib/typescript/src/components/FullscreenStyles.d.ts.map +1 -0
- package/lib/typescript/src/components/NativeAdComponent.d.ts +24 -0
- package/lib/typescript/src/components/NativeAdComponent.d.ts.map +1 -0
- package/lib/typescript/src/components/NativeAdFullscreenComponent.d.ts +26 -0
- package/lib/typescript/src/components/NativeAdFullscreenComponent.d.ts.map +1 -0
- package/lib/typescript/src/components/NativeStyles.d.ts +139 -0
- package/lib/typescript/src/components/NativeStyles.d.ts.map +1 -0
- package/lib/typescript/src/event/custom-event.d.ts +8 -0
- package/lib/typescript/src/event/custom-event.d.ts.map +1 -0
- package/lib/typescript/src/hook/useAppOpenAd.d.ts +19 -0
- package/lib/typescript/src/hook/useAppOpenAd.d.ts.map +1 -0
- package/lib/typescript/src/hook/useInterstitialAd.d.ts +20 -0
- package/lib/typescript/src/hook/useInterstitialAd.d.ts.map +1 -0
- package/lib/typescript/src/hook/useNativeAd.d.ts +20 -0
- package/lib/typescript/src/hook/useNativeAd.d.ts.map +1 -0
- package/lib/typescript/src/hook/useRewardedAd.d.ts +27 -0
- package/lib/typescript/src/hook/useRewardedAd.d.ts.map +1 -0
- package/lib/typescript/src/hook/useRewardedInterstitialAd.d.ts +27 -0
- package/lib/typescript/src/hook/useRewardedInterstitialAd.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +10 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/stores/interstitial-ads-store.d.ts +36 -0
- package/lib/typescript/src/stores/interstitial-ads-store.d.ts.map +1 -0
- package/lib/typescript/src/stores/native-ads-store.d.ts +36 -0
- package/lib/typescript/src/stores/native-ads-store.d.ts.map +1 -0
- package/lib/typescript/src/stores/rewarded-ads-store.d.ts +39 -0
- package/lib/typescript/src/stores/rewarded-ads-store.d.ts.map +1 -0
- package/lib/typescript/src/stores/rewarded-interstitial-ads-store.d.ts +39 -0
- package/lib/typescript/src/stores/rewarded-interstitial-ads-store.d.ts.map +1 -0
- package/lib/typescript/src/types/ads-types.d.ts +164 -0
- package/lib/typescript/src/types/ads-types.d.ts.map +1 -0
- package/package.json +177 -0
- package/src/ads-manager/AppOpenAdsManager.ts +155 -0
- package/src/ads-manager/GoogleMobileAdsManager.ts +68 -0
- package/src/ads-manager/InterstitialAdsManager.ts +371 -0
- package/src/ads-manager/NativeAdsManager.ts +445 -0
- package/src/ads-manager/RewardedAdsManager.ts +383 -0
- package/src/ads-manager/RewardedInterstitialAdsManager.ts +410 -0
- package/src/components/BannerAdComponent.tsx +205 -0
- package/src/components/FullscreenStyles.ts +158 -0
- package/src/components/NativeAdComponent.tsx +208 -0
- package/src/components/NativeAdFullscreenComponent.tsx +261 -0
- package/src/components/NativeStyles.ts +141 -0
- package/src/event/custom-event.ts +30 -0
- package/src/hook/useAppOpenAd.ts +161 -0
- package/src/hook/useInterstitialAd.ts +195 -0
- package/src/hook/useNativeAd.ts +173 -0
- package/src/hook/useRewardedAd.ts +205 -0
- package/src/hook/useRewardedInterstitialAd.ts +212 -0
- package/src/index.tsx +33 -0
- package/src/stores/interstitial-ads-store.ts +287 -0
- package/src/stores/native-ads-store.ts +272 -0
- package/src/stores/rewarded-ads-store.ts +290 -0
- package/src/stores/rewarded-interstitial-ads-store.ts +318 -0
- package/src/types/ads-types.ts +239 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { AdStatus } from "../types/ads-types.js";
|
|
4
|
+
import { googleMobileAdsManager } from "./GoogleMobileAdsManager.js";
|
|
5
|
+
import { CustomEventEmitter } from "../event/custom-event.js";
|
|
6
|
+
export class RewardedAdsManager extends CustomEventEmitter {
|
|
7
|
+
state = new Map();
|
|
8
|
+
configs = new Map();
|
|
9
|
+
loadPromises = new Map();
|
|
10
|
+
constructor() {
|
|
11
|
+
super();
|
|
12
|
+
this.initializeGoogleMobileAds();
|
|
13
|
+
}
|
|
14
|
+
async initializeGoogleMobileAds() {
|
|
15
|
+
try {
|
|
16
|
+
await googleMobileAdsManager.initialize();
|
|
17
|
+
} catch (error) {
|
|
18
|
+
console.warn('Failed to initialize Google Mobile Ads SDK:', error);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Thay đổi signature: chỉ nhận config
|
|
23
|
+
async preloadAd(config) {
|
|
24
|
+
const fullConfig = {
|
|
25
|
+
...config,
|
|
26
|
+
timeout: config.timeout ?? 10000,
|
|
27
|
+
maxRetries: config.maxRetries ?? 3,
|
|
28
|
+
showOnAppStateChange: config.showOnAppStateChange ?? false
|
|
29
|
+
};
|
|
30
|
+
const {
|
|
31
|
+
adKey,
|
|
32
|
+
adId
|
|
33
|
+
} = fullConfig;
|
|
34
|
+
this.configs.set(adKey, fullConfig);
|
|
35
|
+
if (!this.state.has(adKey)) {
|
|
36
|
+
const adInstance = {
|
|
37
|
+
adKey,
|
|
38
|
+
adId,
|
|
39
|
+
status: AdStatus.IDLE,
|
|
40
|
+
data: null,
|
|
41
|
+
error: null,
|
|
42
|
+
loadTime: null,
|
|
43
|
+
displayTime: null,
|
|
44
|
+
retryCount: 0,
|
|
45
|
+
type: 'rewarded',
|
|
46
|
+
config: fullConfig,
|
|
47
|
+
isUsingHighPriority: false,
|
|
48
|
+
hasTriedHighPriority: false
|
|
49
|
+
};
|
|
50
|
+
this.state.set(adKey, adInstance);
|
|
51
|
+
}
|
|
52
|
+
if (this.loadPromises.has(adKey)) {
|
|
53
|
+
return this.loadPromises.get(adKey);
|
|
54
|
+
}
|
|
55
|
+
const loadPromise = this.loadAdInternal(adKey);
|
|
56
|
+
this.loadPromises.set(adKey, loadPromise);
|
|
57
|
+
try {
|
|
58
|
+
await loadPromise;
|
|
59
|
+
} finally {
|
|
60
|
+
this.loadPromises.delete(adKey);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Thay đổi signature: chỉ nhận config
|
|
65
|
+
async loadAd(config) {
|
|
66
|
+
return this.preloadAd(config);
|
|
67
|
+
}
|
|
68
|
+
async displayAd(adKey) {
|
|
69
|
+
const ad = this.state.get(adKey);
|
|
70
|
+
if (!ad) {
|
|
71
|
+
console.warn(`Rewarded ad with key "${adKey}" not found`);
|
|
72
|
+
return {
|
|
73
|
+
ad: null,
|
|
74
|
+
reward: null
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (ad.status === AdStatus.LOADED) {
|
|
78
|
+
try {
|
|
79
|
+
const reward = await this.showRewardedAd(ad);
|
|
80
|
+
ad.status = AdStatus.DISPLAYED;
|
|
81
|
+
ad.displayTime = Date.now();
|
|
82
|
+
ad.reward = reward;
|
|
83
|
+
this.emit('adDisplayed', ad);
|
|
84
|
+
|
|
85
|
+
// Tự động preload lại quảng cáo mới
|
|
86
|
+
// const config = this.configs.get(adKey);
|
|
87
|
+
// if (config) {
|
|
88
|
+
// this.preloadAd(adKey, config.adId, config).catch((error) => {
|
|
89
|
+
// console.warn(
|
|
90
|
+
// `Failed to preload new rewarded ad for key "${adKey}":`,
|
|
91
|
+
// error
|
|
92
|
+
// );
|
|
93
|
+
// });
|
|
94
|
+
// }
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
ad,
|
|
98
|
+
reward
|
|
99
|
+
};
|
|
100
|
+
} catch (error) {
|
|
101
|
+
ad.status = AdStatus.ERROR;
|
|
102
|
+
ad.error = error instanceof Error ? error.message : String(error);
|
|
103
|
+
this.emit('adError', ad);
|
|
104
|
+
return {
|
|
105
|
+
ad: null,
|
|
106
|
+
reward: null
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (ad.status === AdStatus.LOADING) {
|
|
111
|
+
await this.waitForAdLoad(adKey);
|
|
112
|
+
return this.displayAd(adKey);
|
|
113
|
+
}
|
|
114
|
+
if (ad.status === AdStatus.ERROR) {
|
|
115
|
+
await this.refreshAd(adKey);
|
|
116
|
+
return this.displayAd(adKey);
|
|
117
|
+
}
|
|
118
|
+
console.warn(`Cannot display rewarded ad with key "${adKey}" in status: ${ad.status}`);
|
|
119
|
+
return {
|
|
120
|
+
ad: null,
|
|
121
|
+
reward: null
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
getAd(adKey) {
|
|
125
|
+
return this.state.get(adKey) || null;
|
|
126
|
+
}
|
|
127
|
+
removeAd(adKey) {
|
|
128
|
+
this.state.delete(adKey);
|
|
129
|
+
this.configs.delete(adKey);
|
|
130
|
+
this.loadPromises.delete(adKey);
|
|
131
|
+
this.emit('adRemoved', adKey);
|
|
132
|
+
}
|
|
133
|
+
async refreshAd(adKey) {
|
|
134
|
+
const config = this.configs.get(adKey);
|
|
135
|
+
if (!config) {
|
|
136
|
+
throw new Error(`Config for key "${adKey}" not found`);
|
|
137
|
+
}
|
|
138
|
+
console.log('Refresh ad:', adKey);
|
|
139
|
+
const ad = this.state.get(adKey);
|
|
140
|
+
if (ad) {
|
|
141
|
+
ad.status = AdStatus.IDLE;
|
|
142
|
+
ad.error = null;
|
|
143
|
+
ad.retryCount = 0;
|
|
144
|
+
}
|
|
145
|
+
return this.preloadAd(config); // Truyền config thay vì adKey, adId, config
|
|
146
|
+
}
|
|
147
|
+
clearAllAds() {
|
|
148
|
+
const keys = Array.from(this.state.keys());
|
|
149
|
+
keys.forEach(adKey => this.removeAd(adKey));
|
|
150
|
+
this.emit('allAdsCleared');
|
|
151
|
+
}
|
|
152
|
+
async loadAdInternal(adKey) {
|
|
153
|
+
const ad = this.state.get(adKey);
|
|
154
|
+
const config = this.configs.get(adKey);
|
|
155
|
+
if (!ad || !config) {
|
|
156
|
+
throw new Error(`Ad or config for key "${adKey}" not found`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Xác định adId sẽ được sử dụng
|
|
160
|
+
let adIdToLoad = config.adId;
|
|
161
|
+
|
|
162
|
+
// Nếu có adHighpriorityId và chưa thử load high priority
|
|
163
|
+
if (config.adHighpriorityId && !ad.hasTriedHighPriority) {
|
|
164
|
+
adIdToLoad = config.adHighpriorityId;
|
|
165
|
+
ad.isUsingHighPriority = true;
|
|
166
|
+
} else {
|
|
167
|
+
ad.isUsingHighPriority = false;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Kiểm tra retry limit
|
|
171
|
+
if (ad.retryCount >= config.maxRetries) {
|
|
172
|
+
// Nếu đang dùng high priority và đã hết retry, fallback về adId bình thường
|
|
173
|
+
if (ad.isUsingHighPriority) {
|
|
174
|
+
console.log(`High priority ad failed after ${config.maxRetries} retries, falling back to normal ad`);
|
|
175
|
+
ad.hasTriedHighPriority = true;
|
|
176
|
+
ad.retryCount = 0;
|
|
177
|
+
ad.isUsingHighPriority = false;
|
|
178
|
+
return this.loadAdInternal(adKey);
|
|
179
|
+
} else {
|
|
180
|
+
ad.status = AdStatus.ERROR;
|
|
181
|
+
ad.error = `Max retries (${config.maxRetries}) exceeded`;
|
|
182
|
+
this.emit('adError', ad);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
ad.status = AdStatus.LOADING;
|
|
187
|
+
ad.retryCount++;
|
|
188
|
+
this.emit('adLoading', ad);
|
|
189
|
+
try {
|
|
190
|
+
await googleMobileAdsManager.initialize();
|
|
191
|
+
const rewardedAd = await this.loadRewardedAd(adIdToLoad, config.timeout);
|
|
192
|
+
ad.status = AdStatus.LOADED;
|
|
193
|
+
ad.rewardedAd = rewardedAd;
|
|
194
|
+
ad.error = null;
|
|
195
|
+
ad.loadTime = Date.now();
|
|
196
|
+
ad.retryCount = 0;
|
|
197
|
+
if (ad.isUsingHighPriority) {
|
|
198
|
+
ad.hasTriedHighPriority = true;
|
|
199
|
+
}
|
|
200
|
+
this.emit('adLoaded', ad);
|
|
201
|
+
} catch (error) {
|
|
202
|
+
ad.status = AdStatus.ERROR;
|
|
203
|
+
ad.error = error instanceof Error ? error.message : String(error);
|
|
204
|
+
this.emit('adError', ad);
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
async loadRewardedAd(adId, timeout) {
|
|
209
|
+
try {
|
|
210
|
+
const googleMobileAdsModule = await import('react-native-google-mobile-ads');
|
|
211
|
+
const {
|
|
212
|
+
RewardedAd,
|
|
213
|
+
TestIds,
|
|
214
|
+
RewardedAdEventType,
|
|
215
|
+
AdEventType
|
|
216
|
+
} = googleMobileAdsModule;
|
|
217
|
+
const finalAdId = adId === 'test' ? TestIds.REWARDED : adId;
|
|
218
|
+
return new Promise((resolve, reject) => {
|
|
219
|
+
const timeoutId = setTimeout(() => {
|
|
220
|
+
reject(new Error(`Timeout loading rewarded ad after ${timeout}ms`));
|
|
221
|
+
}, timeout);
|
|
222
|
+
|
|
223
|
+
// Tạo rewarded ad instance
|
|
224
|
+
const rewardedAd = RewardedAd.createForAdRequest(finalAdId);
|
|
225
|
+
|
|
226
|
+
// Thêm event listeners
|
|
227
|
+
const unsubscribeLoaded = rewardedAd.addAdEventListener(RewardedAdEventType.LOADED, () => {
|
|
228
|
+
clearTimeout(timeoutId);
|
|
229
|
+
unsubscribeLoaded();
|
|
230
|
+
unsubscribeError();
|
|
231
|
+
resolve(rewardedAd);
|
|
232
|
+
});
|
|
233
|
+
const unsubscribeError = rewardedAd.addAdEventListener(AdEventType.ERROR, error => {
|
|
234
|
+
clearTimeout(timeoutId);
|
|
235
|
+
unsubscribeLoaded();
|
|
236
|
+
unsubscribeError();
|
|
237
|
+
reject(new Error(`Failed to load rewarded ad: ${error.message || error}`));
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// Bắt đầu load quảng cáo
|
|
241
|
+
rewardedAd.load();
|
|
242
|
+
});
|
|
243
|
+
} catch (error) {
|
|
244
|
+
console.warn('Failed to load rewarded ad:', error);
|
|
245
|
+
throw error;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async showRewardedAd(ad) {
|
|
249
|
+
if (!ad.rewardedAd) {
|
|
250
|
+
throw new Error('Rewarded ad not loaded');
|
|
251
|
+
}
|
|
252
|
+
return new Promise((resolve, reject) => {
|
|
253
|
+
const googleMobileAdsModule = require('react-native-google-mobile-ads');
|
|
254
|
+
const {
|
|
255
|
+
AdEventType,
|
|
256
|
+
RewardedAdEventType
|
|
257
|
+
} = googleMobileAdsModule;
|
|
258
|
+
let earnedReward = null;
|
|
259
|
+
|
|
260
|
+
// Thêm event listeners cho việc hiển thị
|
|
261
|
+
const unsubscribeEarnedReward = ad.rewardedAd.addAdEventListener(RewardedAdEventType.EARNED_REWARD, reward => {
|
|
262
|
+
earnedReward = reward;
|
|
263
|
+
// Emit sự kiện earnedReward
|
|
264
|
+
this.emit('adEarnedReward', {
|
|
265
|
+
ad,
|
|
266
|
+
reward
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
const unsubscribeClosed = ad.rewardedAd.addAdEventListener(AdEventType.CLOSED, () => {
|
|
270
|
+
unsubscribeEarnedReward();
|
|
271
|
+
unsubscribeClosed();
|
|
272
|
+
unsubscribeError();
|
|
273
|
+
unsubscribeOpened();
|
|
274
|
+
unsubscribeClicked();
|
|
275
|
+
|
|
276
|
+
// Emit sự kiện adClosed
|
|
277
|
+
this.emit('adClosed', ad);
|
|
278
|
+
resolve(earnedReward);
|
|
279
|
+
});
|
|
280
|
+
const unsubscribeError = ad.rewardedAd.addAdEventListener(AdEventType.ERROR, error => {
|
|
281
|
+
unsubscribeEarnedReward();
|
|
282
|
+
unsubscribeClosed();
|
|
283
|
+
unsubscribeError();
|
|
284
|
+
unsubscribeOpened();
|
|
285
|
+
unsubscribeClicked();
|
|
286
|
+
reject(new Error(`Failed to show rewarded ad: ${error.message || error}`));
|
|
287
|
+
});
|
|
288
|
+
const unsubscribeOpened = ad.rewardedAd.addAdEventListener(AdEventType.OPENED, () => {
|
|
289
|
+
// Emit sự kiện adOpened
|
|
290
|
+
this.emit('adOpened', ad);
|
|
291
|
+
});
|
|
292
|
+
const unsubscribeClicked = ad.rewardedAd.addAdEventListener(AdEventType.CLICKED, () => {
|
|
293
|
+
// Emit sự kiện adClicked
|
|
294
|
+
this.emit('adClicked', ad);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
// Hiển thị quảng cáo
|
|
298
|
+
ad.rewardedAd.show();
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
async waitForAdLoad(adKey) {
|
|
302
|
+
return new Promise((resolve, reject) => {
|
|
303
|
+
const timeout = setTimeout(() => {
|
|
304
|
+
reject(new Error(`Timeout waiting for rewarded ad "${adKey}" to load`));
|
|
305
|
+
}, 10000);
|
|
306
|
+
const checkAd = () => {
|
|
307
|
+
const ad = this.state.get(adKey);
|
|
308
|
+
if (!ad) {
|
|
309
|
+
clearTimeout(timeout);
|
|
310
|
+
reject(new Error(`Rewarded ad "${adKey}" not found`));
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (ad.status === AdStatus.LOADED) {
|
|
314
|
+
clearTimeout(timeout);
|
|
315
|
+
resolve();
|
|
316
|
+
} else if (ad.status === AdStatus.ERROR) {
|
|
317
|
+
clearTimeout(timeout);
|
|
318
|
+
reject(new Error(ad.error || 'Unknown error'));
|
|
319
|
+
} else {
|
|
320
|
+
setTimeout(checkAd, 100);
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
checkAd();
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
export const rewardedAdsManager = new RewardedAdsManager();
|
|
328
|
+
//# sourceMappingURL=RewardedAdsManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["AdStatus","googleMobileAdsManager","CustomEventEmitter","RewardedAdsManager","state","Map","configs","loadPromises","constructor","initializeGoogleMobileAds","initialize","error","console","warn","preloadAd","config","fullConfig","timeout","maxRetries","showOnAppStateChange","adKey","adId","set","has","adInstance","status","IDLE","data","loadTime","displayTime","retryCount","type","isUsingHighPriority","hasTriedHighPriority","get","loadPromise","loadAdInternal","delete","loadAd","displayAd","ad","reward","LOADED","showRewardedAd","DISPLAYED","Date","now","emit","ERROR","Error","message","String","LOADING","waitForAdLoad","refreshAd","getAd","removeAd","log","clearAllAds","keys","Array","from","forEach","adIdToLoad","adHighpriorityId","rewardedAd","loadRewardedAd","googleMobileAdsModule","RewardedAd","TestIds","RewardedAdEventType","AdEventType","finalAdId","REWARDED","Promise","resolve","reject","timeoutId","setTimeout","createForAdRequest","unsubscribeLoaded","addAdEventListener","clearTimeout","unsubscribeError","load","require","earnedReward","unsubscribeEarnedReward","EARNED_REWARD","unsubscribeClosed","CLOSED","unsubscribeOpened","unsubscribeClicked","OPENED","CLICKED","show","checkAd","rewardedAdsManager"],"sourceRoot":"../../../src","sources":["ads-manager/RewardedAdsManager.ts"],"mappings":";;AAAA,SACEA,QAAQ,QAGH,uBAAoB;AAC3B,SAASC,sBAAsB,QAAQ,6BAA0B;AACjE,SAASC,kBAAkB,QAAQ,0BAAuB;AAE1D,OAAO,MAAMC,kBAAkB,SAASD,kBAAkB,CAAC;EACjDE,KAAK,GAAoC,IAAIC,GAAG,CAAC,CAAC;EAClDC,OAAO,GAAkC,IAAID,GAAG,CAAC,CAAC;EAClDE,YAAY,GAA+B,IAAIF,GAAG,CAAC,CAAC;EAE5DG,WAAWA,CAAA,EAAG;IACZ,KAAK,CAAC,CAAC;IACP,IAAI,CAACC,yBAAyB,CAAC,CAAC;EAClC;EAEA,MAAcA,yBAAyBA,CAAA,EAAkB;IACvD,IAAI;MACF,MAAMR,sBAAsB,CAACS,UAAU,CAAC,CAAC;IAC3C,CAAC,CAAC,OAAOC,KAAK,EAAE;MACdC,OAAO,CAACC,IAAI,CAAC,6CAA6C,EAAEF,KAAK,CAAC;IACpE;EACF;;EAEA;EACA,MAAMG,SAASA,CAACC,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ACP0C,YAAY,CAACjE,OAAO,CAAC;UACrB2D,MAAM,CAAC,IAAI3B,KAAK,CAAC,gBAAgB7B,KAAK,aAAa,CAAC,CAAC;UACrD;QACF;QAEA,IAAIoB,EAAE,CAACf,MAAM,KAAKzB,QAAQ,CAAC0C,MAAM,EAAE;UACjCwC,YAAY,CAACjE,OAAO,CAAC;UACrB0D,OAAO,CAAC,CAAC;QACX,CAAC,MAAM,IAAInC,EAAE,CAACf,MAAM,KAAKzB,QAAQ,CAACgD,KAAK,EAAE;UACvCkC,YAAY,CAACjE,OAAO,CAAC;UACrB2D,MAAM,CAAC,IAAI3B,KAAK,CAACT,EAAE,CAAC7B,KAAK,IAAI,eAAe,CAAC,CAAC;QAChD,CAAC,MAAM;UACLmE,UAAU,CAACkB,OAAO,EAAE,GAAG,CAAC;QAC1B;MACF,CAAC;MAEDA,OAAO,CAAC,CAAC;IACX,CAAC,CAAC;EACJ;AACF;AAEA,OAAO,MAAMC,kBAAkB,GAAG,IAAI9F,kBAAkB,CAAC,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { AdStatus } from "../types/ads-types.js";
|
|
4
|
+
import { googleMobileAdsManager } from "./GoogleMobileAdsManager.js";
|
|
5
|
+
import { CustomEventEmitter } from "../event/custom-event.js";
|
|
6
|
+
export class RewardedInterstitialAdsManager extends CustomEventEmitter {
|
|
7
|
+
state = new Map();
|
|
8
|
+
configs = new Map();
|
|
9
|
+
loadPromises = new Map();
|
|
10
|
+
constructor() {
|
|
11
|
+
super();
|
|
12
|
+
this.initializeGoogleMobileAds();
|
|
13
|
+
}
|
|
14
|
+
async initializeGoogleMobileAds() {
|
|
15
|
+
try {
|
|
16
|
+
await googleMobileAdsManager.initialize();
|
|
17
|
+
} catch (error) {
|
|
18
|
+
console.warn('Failed to initialize Google Mobile Ads SDK:', error);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Thay đổi signature: chỉ nhận config
|
|
23
|
+
async preloadAd(config) {
|
|
24
|
+
const fullConfig = {
|
|
25
|
+
...config,
|
|
26
|
+
timeout: config.timeout ?? 10000,
|
|
27
|
+
maxRetries: config.maxRetries ?? 3,
|
|
28
|
+
showOnAppStateChange: config.showOnAppStateChange ?? false
|
|
29
|
+
};
|
|
30
|
+
const {
|
|
31
|
+
adKey,
|
|
32
|
+
adId
|
|
33
|
+
} = fullConfig;
|
|
34
|
+
this.configs.set(adKey, fullConfig);
|
|
35
|
+
if (!this.state.has(adKey)) {
|
|
36
|
+
const adInstance = {
|
|
37
|
+
adKey,
|
|
38
|
+
adId,
|
|
39
|
+
status: AdStatus.IDLE,
|
|
40
|
+
data: null,
|
|
41
|
+
error: null,
|
|
42
|
+
loadTime: null,
|
|
43
|
+
displayTime: null,
|
|
44
|
+
retryCount: 0,
|
|
45
|
+
type: 'rewarded_interstitial',
|
|
46
|
+
config: fullConfig,
|
|
47
|
+
isUsingHighPriority: false,
|
|
48
|
+
hasTriedHighPriority: false
|
|
49
|
+
};
|
|
50
|
+
this.state.set(adKey, adInstance);
|
|
51
|
+
}
|
|
52
|
+
if (this.loadPromises.has(adKey)) {
|
|
53
|
+
return this.loadPromises.get(adKey);
|
|
54
|
+
}
|
|
55
|
+
const loadPromise = this.loadAdInternal(adKey);
|
|
56
|
+
this.loadPromises.set(adKey, loadPromise);
|
|
57
|
+
try {
|
|
58
|
+
await loadPromise;
|
|
59
|
+
} finally {
|
|
60
|
+
this.loadPromises.delete(adKey);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Thay đổi signature: chỉ nhận config
|
|
65
|
+
async loadAd(config) {
|
|
66
|
+
return this.preloadAd(config);
|
|
67
|
+
}
|
|
68
|
+
async displayAd(adKey) {
|
|
69
|
+
const ad = this.state.get(adKey);
|
|
70
|
+
if (!ad) {
|
|
71
|
+
console.warn(`Rewarded interstitial ad with key "${adKey}" not found`);
|
|
72
|
+
return {
|
|
73
|
+
ad: null,
|
|
74
|
+
reward: null
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (ad.status === AdStatus.LOADED) {
|
|
78
|
+
try {
|
|
79
|
+
const reward = await this.showRewardedInterstitialAd(ad);
|
|
80
|
+
ad.status = AdStatus.DISPLAYED;
|
|
81
|
+
ad.displayTime = Date.now();
|
|
82
|
+
ad.reward = reward;
|
|
83
|
+
this.emit('adDisplayed', ad);
|
|
84
|
+
|
|
85
|
+
// Tự động preload lại quảng cáo mới
|
|
86
|
+
// const config = this.configs.get(adKey);
|
|
87
|
+
// if (config) {
|
|
88
|
+
// this.preloadAd(adKey, config.adId, config).catch((error) => {
|
|
89
|
+
// console.warn(
|
|
90
|
+
// `Failed to preload new rewarded interstitial ad for key "${adKey}":`,
|
|
91
|
+
// error
|
|
92
|
+
// );
|
|
93
|
+
// });
|
|
94
|
+
// }
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
ad,
|
|
98
|
+
reward
|
|
99
|
+
};
|
|
100
|
+
} catch (error) {
|
|
101
|
+
ad.status = AdStatus.ERROR;
|
|
102
|
+
ad.error = error instanceof Error ? error.message : String(error);
|
|
103
|
+
this.emit('adError', ad);
|
|
104
|
+
return {
|
|
105
|
+
ad: null,
|
|
106
|
+
reward: null
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (ad.status === AdStatus.LOADING) {
|
|
111
|
+
await this.waitForAdLoad(adKey);
|
|
112
|
+
return this.displayAd(adKey);
|
|
113
|
+
}
|
|
114
|
+
if (ad.status === AdStatus.ERROR) {
|
|
115
|
+
await this.refreshAd(adKey);
|
|
116
|
+
return this.displayAd(adKey);
|
|
117
|
+
}
|
|
118
|
+
console.warn(`Cannot display rewarded interstitial ad with key "${adKey}" in status: ${ad.status}`);
|
|
119
|
+
return {
|
|
120
|
+
ad: null,
|
|
121
|
+
reward: null
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
getAd(adKey) {
|
|
125
|
+
return this.state.get(adKey) || null;
|
|
126
|
+
}
|
|
127
|
+
removeAd(adKey) {
|
|
128
|
+
this.state.delete(adKey);
|
|
129
|
+
this.configs.delete(adKey);
|
|
130
|
+
this.loadPromises.delete(adKey);
|
|
131
|
+
this.emit('adRemoved', adKey);
|
|
132
|
+
}
|
|
133
|
+
async refreshAd(adKey) {
|
|
134
|
+
const config = this.configs.get(adKey);
|
|
135
|
+
if (!config) {
|
|
136
|
+
throw new Error(`Config for key "${adKey}" not found`);
|
|
137
|
+
}
|
|
138
|
+
console.log('Refresh ad:', adKey);
|
|
139
|
+
const ad = this.state.get(adKey);
|
|
140
|
+
if (ad) {
|
|
141
|
+
ad.status = AdStatus.IDLE;
|
|
142
|
+
ad.error = null;
|
|
143
|
+
ad.retryCount = 0;
|
|
144
|
+
}
|
|
145
|
+
return this.preloadAd(config); // Truyền config thay vì adKey, adId, config
|
|
146
|
+
}
|
|
147
|
+
clearAllAds() {
|
|
148
|
+
const keys = Array.from(this.state.keys());
|
|
149
|
+
keys.forEach(adKey => this.removeAd(adKey));
|
|
150
|
+
this.emit('allAdsCleared');
|
|
151
|
+
}
|
|
152
|
+
async loadAdInternal(adKey) {
|
|
153
|
+
const ad = this.state.get(adKey);
|
|
154
|
+
const config = this.configs.get(adKey);
|
|
155
|
+
if (!ad || !config) {
|
|
156
|
+
throw new Error(`Ad or config for key "${adKey}" not found`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Xác định adId sẽ được sử dụng
|
|
160
|
+
let adIdToLoad = config.adId;
|
|
161
|
+
|
|
162
|
+
// Nếu có adHighpriorityId và chưa thử load high priority
|
|
163
|
+
if (config.adHighpriorityId && !ad.hasTriedHighPriority) {
|
|
164
|
+
adIdToLoad = config.adHighpriorityId;
|
|
165
|
+
ad.isUsingHighPriority = true;
|
|
166
|
+
} else {
|
|
167
|
+
ad.isUsingHighPriority = false;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Kiểm tra retry limit
|
|
171
|
+
if (ad.retryCount >= config.maxRetries) {
|
|
172
|
+
// Nếu đang dùng high priority và đã hết retry, fallback về adId bình thường
|
|
173
|
+
if (ad.isUsingHighPriority) {
|
|
174
|
+
console.log(`High priority ad failed after ${config.maxRetries} retries, falling back to normal ad`);
|
|
175
|
+
ad.hasTriedHighPriority = true;
|
|
176
|
+
ad.retryCount = 0;
|
|
177
|
+
ad.isUsingHighPriority = false;
|
|
178
|
+
return this.loadAdInternal(adKey);
|
|
179
|
+
} else {
|
|
180
|
+
ad.status = AdStatus.ERROR;
|
|
181
|
+
ad.error = `Max retries (${config.maxRetries}) exceeded`;
|
|
182
|
+
this.emit('adError', ad);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
ad.status = AdStatus.LOADING;
|
|
187
|
+
ad.retryCount++;
|
|
188
|
+
this.emit('adLoading', ad);
|
|
189
|
+
try {
|
|
190
|
+
await googleMobileAdsManager.initialize();
|
|
191
|
+
const rewardedInterstitialAd = await this.loadRewardedInterstitialAd(adIdToLoad, config.timeout);
|
|
192
|
+
ad.status = AdStatus.LOADED;
|
|
193
|
+
ad.rewardedInterstitialAd = rewardedInterstitialAd;
|
|
194
|
+
ad.error = null;
|
|
195
|
+
ad.loadTime = Date.now();
|
|
196
|
+
ad.retryCount = 0;
|
|
197
|
+
if (ad.isUsingHighPriority) {
|
|
198
|
+
ad.hasTriedHighPriority = true;
|
|
199
|
+
}
|
|
200
|
+
this.emit('adLoaded', ad);
|
|
201
|
+
} catch (error) {
|
|
202
|
+
ad.status = AdStatus.ERROR;
|
|
203
|
+
ad.error = error instanceof Error ? error.message : String(error);
|
|
204
|
+
this.emit('adError', ad);
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
async loadRewardedInterstitialAd(adId, timeout) {
|
|
209
|
+
try {
|
|
210
|
+
const googleMobileAdsModule = await import('react-native-google-mobile-ads');
|
|
211
|
+
const {
|
|
212
|
+
RewardedInterstitialAd,
|
|
213
|
+
TestIds,
|
|
214
|
+
AdEventType,
|
|
215
|
+
RewardedAdEventType
|
|
216
|
+
} = googleMobileAdsModule;
|
|
217
|
+
const finalAdId = adId === 'test' ? TestIds.REWARDED_INTERSTITIAL : adId;
|
|
218
|
+
return new Promise((resolve, reject) => {
|
|
219
|
+
const timeoutId = setTimeout(() => {
|
|
220
|
+
reject(new Error(`Timeout loading rewarded interstitial ad after ${timeout}ms`));
|
|
221
|
+
}, timeout);
|
|
222
|
+
|
|
223
|
+
// Tạo rewarded interstitial ad instance
|
|
224
|
+
const rewardedInterstitialAd = RewardedInterstitialAd.createForAdRequest(finalAdId);
|
|
225
|
+
|
|
226
|
+
// Thêm event listeners
|
|
227
|
+
const unsubscribeLoaded = rewardedInterstitialAd.addAdEventListener(RewardedAdEventType.LOADED, () => {
|
|
228
|
+
clearTimeout(timeoutId);
|
|
229
|
+
unsubscribeLoaded();
|
|
230
|
+
unsubscribeError();
|
|
231
|
+
resolve(rewardedInterstitialAd);
|
|
232
|
+
});
|
|
233
|
+
const unsubscribeError = rewardedInterstitialAd.addAdEventListener(AdEventType.ERROR, error => {
|
|
234
|
+
clearTimeout(timeoutId);
|
|
235
|
+
unsubscribeLoaded();
|
|
236
|
+
unsubscribeError();
|
|
237
|
+
reject(new Error(`Failed to load rewarded interstitial ad: ${error.message || error}`));
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// Bắt đầu load quảng cáo
|
|
241
|
+
rewardedInterstitialAd.load();
|
|
242
|
+
});
|
|
243
|
+
} catch (error) {
|
|
244
|
+
console.warn('Failed to load rewarded interstitial ad:', error);
|
|
245
|
+
throw error;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async showRewardedInterstitialAd(ad) {
|
|
249
|
+
if (!ad.rewardedInterstitialAd) {
|
|
250
|
+
throw new Error('Rewarded interstitial ad not loaded');
|
|
251
|
+
}
|
|
252
|
+
return new Promise((resolve, reject) => {
|
|
253
|
+
const googleMobileAdsModule = require('react-native-google-mobile-ads');
|
|
254
|
+
const {
|
|
255
|
+
AdEventType,
|
|
256
|
+
RewardedAdEventType
|
|
257
|
+
} = googleMobileAdsModule;
|
|
258
|
+
let earnedReward = null;
|
|
259
|
+
|
|
260
|
+
// Thêm event listeners cho việc hiển thị
|
|
261
|
+
const unsubscribeEarnedReward = ad.rewardedInterstitialAd.addAdEventListener(RewardedAdEventType.EARNED_REWARD, reward => {
|
|
262
|
+
earnedReward = reward;
|
|
263
|
+
// Emit sự kiện earnedReward
|
|
264
|
+
this.emit('adEarnedReward', {
|
|
265
|
+
ad,
|
|
266
|
+
reward
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
const unsubscribeClosed = ad.rewardedInterstitialAd.addAdEventListener(AdEventType.CLOSED, () => {
|
|
270
|
+
unsubscribeEarnedReward();
|
|
271
|
+
unsubscribeClosed();
|
|
272
|
+
unsubscribeError();
|
|
273
|
+
unsubscribeOpened();
|
|
274
|
+
unsubscribeClicked();
|
|
275
|
+
|
|
276
|
+
// Emit sự kiện adClosed
|
|
277
|
+
this.emit('adClosed', ad);
|
|
278
|
+
resolve(earnedReward);
|
|
279
|
+
});
|
|
280
|
+
const unsubscribeError = ad.rewardedInterstitialAd.addAdEventListener(AdEventType.ERROR, error => {
|
|
281
|
+
unsubscribeEarnedReward();
|
|
282
|
+
unsubscribeClosed();
|
|
283
|
+
unsubscribeError();
|
|
284
|
+
unsubscribeOpened();
|
|
285
|
+
unsubscribeClicked();
|
|
286
|
+
reject(new Error(`Failed to show rewarded interstitial ad: ${error.message || error}`));
|
|
287
|
+
});
|
|
288
|
+
const unsubscribeOpened = ad.rewardedInterstitialAd.addAdEventListener(AdEventType.OPENED, () => {
|
|
289
|
+
// Emit sự kiện adOpened
|
|
290
|
+
this.emit('adOpened', ad);
|
|
291
|
+
});
|
|
292
|
+
const unsubscribeClicked = ad.rewardedInterstitialAd.addAdEventListener(AdEventType.CLICKED, () => {
|
|
293
|
+
// Emit sự kiện adClicked
|
|
294
|
+
this.emit('adClicked', ad);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
// Hiển thị quảng cáo
|
|
298
|
+
ad.rewardedInterstitialAd.show();
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
async waitForAdLoad(adKey) {
|
|
302
|
+
return new Promise((resolve, reject) => {
|
|
303
|
+
const timeout = setTimeout(() => {
|
|
304
|
+
reject(new Error(`Timeout waiting for rewarded interstitial ad "${adKey}" to load`));
|
|
305
|
+
}, 10000);
|
|
306
|
+
const checkAd = () => {
|
|
307
|
+
const ad = this.state.get(adKey);
|
|
308
|
+
if (!ad) {
|
|
309
|
+
clearTimeout(timeout);
|
|
310
|
+
reject(new Error(`Rewarded interstitial ad "${adKey}" not found`));
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (ad.status === AdStatus.LOADED) {
|
|
314
|
+
clearTimeout(timeout);
|
|
315
|
+
resolve();
|
|
316
|
+
} else if (ad.status === AdStatus.ERROR) {
|
|
317
|
+
clearTimeout(timeout);
|
|
318
|
+
reject(new Error(ad.error || 'Unknown error'));
|
|
319
|
+
} else {
|
|
320
|
+
setTimeout(checkAd, 100);
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
checkAd();
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
export const rewardedInterstitialAdsManager = new RewardedInterstitialAdsManager();
|
|
328
|
+
//# sourceMappingURL=RewardedInterstitialAdsManager.js.map
|