@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,205 @@
|
|
|
1
|
+
import { useEffect, useCallback } from 'react';
|
|
2
|
+
import { useRewardedAdStore } from '../stores/rewarded-ads-store';
|
|
3
|
+
import {
|
|
4
|
+
AdStatus,
|
|
5
|
+
type UseRewardedAdOptions,
|
|
6
|
+
type RewardedAdConfig,
|
|
7
|
+
} from '../types/ads-types';
|
|
8
|
+
import { rewardedAdsManager } from '../ads-manager/RewardedAdsManager';
|
|
9
|
+
|
|
10
|
+
// Merge RewardedAdConfig với UseRewardedAdOptions
|
|
11
|
+
type UseRewardedAdConfig = RewardedAdConfig & UseRewardedAdOptions;
|
|
12
|
+
|
|
13
|
+
export const useRewardedAd = (config: UseRewardedAdConfig) => {
|
|
14
|
+
const {
|
|
15
|
+
adKey,
|
|
16
|
+
adId,
|
|
17
|
+
timeout,
|
|
18
|
+
maxRetries,
|
|
19
|
+
adHighpriorityId,
|
|
20
|
+
showOnAppStateChange,
|
|
21
|
+
// UseRewardedAdOptions fields
|
|
22
|
+
autoLoad = false,
|
|
23
|
+
onLoad,
|
|
24
|
+
onDisplay,
|
|
25
|
+
onError,
|
|
26
|
+
onReward,
|
|
27
|
+
onOpened,
|
|
28
|
+
onClicked,
|
|
29
|
+
onClosed,
|
|
30
|
+
} = config;
|
|
31
|
+
|
|
32
|
+
// Zustand selectors
|
|
33
|
+
const ad = useRewardedAdStore((state) => state.getAd(adKey));
|
|
34
|
+
const isLoading = useRewardedAdStore((state) => state.isLoading(adKey));
|
|
35
|
+
const error = useRewardedAdStore((state) => state.getError(adKey));
|
|
36
|
+
const isUsingHighPriority = useRewardedAdStore((state) =>
|
|
37
|
+
state.isUsingHighPriority(adKey)
|
|
38
|
+
);
|
|
39
|
+
const hasTriedHighPriority = useRewardedAdStore((state) =>
|
|
40
|
+
state.hasTriedHighPriority(adKey)
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
// Zustand actions
|
|
44
|
+
const preloadAd = useRewardedAdStore((state) => state.preloadAd);
|
|
45
|
+
const loadAd = useRewardedAdStore((state) => state.loadAd);
|
|
46
|
+
const displayAd = useRewardedAdStore((state) => state.displayAd);
|
|
47
|
+
const removeAd = useRewardedAdStore((state) => state.removeAd);
|
|
48
|
+
const refreshAd = useRewardedAdStore((state) => state.refreshAd);
|
|
49
|
+
|
|
50
|
+
// Memoized callbacks
|
|
51
|
+
const handleLoadAd = useCallback(
|
|
52
|
+
async (customConfig?: Partial<RewardedAdConfig>) => {
|
|
53
|
+
const finalConfig: RewardedAdConfig = {
|
|
54
|
+
adKey,
|
|
55
|
+
adId,
|
|
56
|
+
showOnAppStateChange,
|
|
57
|
+
timeout,
|
|
58
|
+
maxRetries,
|
|
59
|
+
adHighpriorityId,
|
|
60
|
+
...customConfig,
|
|
61
|
+
};
|
|
62
|
+
await loadAd(finalConfig);
|
|
63
|
+
},
|
|
64
|
+
[
|
|
65
|
+
adKey,
|
|
66
|
+
adId,
|
|
67
|
+
loadAd,
|
|
68
|
+
showOnAppStateChange,
|
|
69
|
+
timeout,
|
|
70
|
+
maxRetries,
|
|
71
|
+
adHighpriorityId,
|
|
72
|
+
]
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const handlePreloadAd = useCallback(
|
|
76
|
+
async (customConfig?: Partial<RewardedAdConfig>) => {
|
|
77
|
+
const finalConfig: RewardedAdConfig = {
|
|
78
|
+
adKey,
|
|
79
|
+
adId,
|
|
80
|
+
showOnAppStateChange,
|
|
81
|
+
timeout,
|
|
82
|
+
maxRetries,
|
|
83
|
+
adHighpriorityId,
|
|
84
|
+
...customConfig,
|
|
85
|
+
};
|
|
86
|
+
await preloadAd(finalConfig);
|
|
87
|
+
},
|
|
88
|
+
[
|
|
89
|
+
adKey,
|
|
90
|
+
adId,
|
|
91
|
+
preloadAd,
|
|
92
|
+
showOnAppStateChange,
|
|
93
|
+
timeout,
|
|
94
|
+
maxRetries,
|
|
95
|
+
adHighpriorityId,
|
|
96
|
+
]
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const handleDisplayAd = useCallback(async () => {
|
|
100
|
+
const result = await displayAd(adKey);
|
|
101
|
+
if (result.ad && onDisplay) {
|
|
102
|
+
onDisplay(result.ad);
|
|
103
|
+
}
|
|
104
|
+
if (result.reward && onReward) {
|
|
105
|
+
onReward(result.reward);
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}, [adKey, displayAd, onDisplay, onReward]);
|
|
109
|
+
|
|
110
|
+
const handleRemoveAd = useCallback(() => {
|
|
111
|
+
removeAd(adKey);
|
|
112
|
+
}, [adKey, removeAd]);
|
|
113
|
+
|
|
114
|
+
const handleRefreshAd = useCallback(async () => {
|
|
115
|
+
await refreshAd(adKey);
|
|
116
|
+
}, [adKey, refreshAd]);
|
|
117
|
+
|
|
118
|
+
// Auto load effect
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
if (autoLoad && !ad) {
|
|
121
|
+
handleLoadAd();
|
|
122
|
+
}
|
|
123
|
+
}, [autoLoad, ad, handleLoadAd]);
|
|
124
|
+
|
|
125
|
+
// Event callbacks
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
if (ad?.status === AdStatus.LOADED && onLoad) {
|
|
128
|
+
onLoad(ad);
|
|
129
|
+
}
|
|
130
|
+
}, [ad?.status, ad, onLoad]);
|
|
131
|
+
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
if (ad?.status === AdStatus.DISPLAYED && onDisplay) {
|
|
134
|
+
onDisplay(ad);
|
|
135
|
+
}
|
|
136
|
+
}, [ad?.status, ad, onDisplay]);
|
|
137
|
+
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (ad?.status === AdStatus.ERROR && ad.error && onError) {
|
|
140
|
+
onError(ad.error, ad);
|
|
141
|
+
}
|
|
142
|
+
}, [ad?.status, ad, onError]);
|
|
143
|
+
|
|
144
|
+
// Lắng nghe các sự kiện từ RewardedAdsManager
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
if (!ad) return;
|
|
147
|
+
|
|
148
|
+
const handleAdOpened = (openedAd: any) => {
|
|
149
|
+
if (openedAd.adKey === adKey && onOpened) {
|
|
150
|
+
onOpened(openedAd);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const handleAdClicked = (clickedAd: any) => {
|
|
155
|
+
if (clickedAd.adKey === adKey && onClicked) {
|
|
156
|
+
onClicked(clickedAd);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const handleAdClosed = (closedAd: any) => {
|
|
161
|
+
if (closedAd.adKey === adKey && onClosed) {
|
|
162
|
+
onClosed(closedAd);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const handleEarnedReward = ({ ad: rewardAd, reward }: any) => {
|
|
167
|
+
if (rewardAd.adKey === adKey && onReward) {
|
|
168
|
+
onReward(reward);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
rewardedAdsManager.on('adOpened', handleAdOpened);
|
|
173
|
+
rewardedAdsManager.on('adClicked', handleAdClicked);
|
|
174
|
+
rewardedAdsManager.on('adClosed', handleAdClosed);
|
|
175
|
+
rewardedAdsManager.on('adEarnedReward', handleEarnedReward);
|
|
176
|
+
|
|
177
|
+
return () => {
|
|
178
|
+
rewardedAdsManager.off('adOpened', handleAdOpened);
|
|
179
|
+
rewardedAdsManager.off('adClicked', handleAdClicked);
|
|
180
|
+
rewardedAdsManager.off('adClosed', handleAdClosed);
|
|
181
|
+
rewardedAdsManager.off('adEarnedReward', handleEarnedReward);
|
|
182
|
+
};
|
|
183
|
+
}, [adKey, onOpened, onClicked, onClosed, onReward, ad]);
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
// State
|
|
187
|
+
ad,
|
|
188
|
+
isLoading,
|
|
189
|
+
error,
|
|
190
|
+
status: ad?.status || AdStatus.IDLE,
|
|
191
|
+
isLoaded: ad?.status === AdStatus.LOADED,
|
|
192
|
+
isDisplayed: ad?.status === AdStatus.DISPLAYED,
|
|
193
|
+
hasError: ad?.status === AdStatus.ERROR,
|
|
194
|
+
reward: ad?.reward,
|
|
195
|
+
isUsingHighPriority,
|
|
196
|
+
hasTriedHighPriority,
|
|
197
|
+
|
|
198
|
+
// Actions
|
|
199
|
+
loadAd: handleLoadAd,
|
|
200
|
+
preloadAd: handlePreloadAd,
|
|
201
|
+
displayAd: handleDisplayAd,
|
|
202
|
+
removeAd: handleRemoveAd,
|
|
203
|
+
refreshAd: handleRefreshAd,
|
|
204
|
+
};
|
|
205
|
+
};
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { useEffect, useCallback } from 'react';
|
|
2
|
+
import { useRewardedInterstitialAdStore } from '../stores/rewarded-interstitial-ads-store';
|
|
3
|
+
import {
|
|
4
|
+
AdStatus,
|
|
5
|
+
type UseRewardedInterstitialAdOptions,
|
|
6
|
+
type RewardedInterstitialAdConfig,
|
|
7
|
+
} from '../types/ads-types';
|
|
8
|
+
import { rewardedInterstitialAdsManager } from '../ads-manager/RewardedInterstitialAdsManager';
|
|
9
|
+
|
|
10
|
+
// Merge RewardedInterstitialAdConfig với UseRewardedInterstitialAdOptions
|
|
11
|
+
type UseRewardedInterstitialAdConfig = RewardedInterstitialAdConfig &
|
|
12
|
+
UseRewardedInterstitialAdOptions;
|
|
13
|
+
|
|
14
|
+
export const useRewardedInterstitialAd = (
|
|
15
|
+
config: UseRewardedInterstitialAdConfig
|
|
16
|
+
) => {
|
|
17
|
+
const {
|
|
18
|
+
adKey,
|
|
19
|
+
adId,
|
|
20
|
+
timeout,
|
|
21
|
+
maxRetries,
|
|
22
|
+
adHighpriorityId,
|
|
23
|
+
showOnAppStateChange,
|
|
24
|
+
// UseRewardedInterstitialAdOptions fields
|
|
25
|
+
autoLoad = false,
|
|
26
|
+
onLoad,
|
|
27
|
+
onDisplay,
|
|
28
|
+
onError,
|
|
29
|
+
onReward,
|
|
30
|
+
onOpened,
|
|
31
|
+
onClicked,
|
|
32
|
+
onClosed,
|
|
33
|
+
} = config;
|
|
34
|
+
|
|
35
|
+
// Zustand selectors
|
|
36
|
+
const ad = useRewardedInterstitialAdStore((state) => state.getAd(adKey));
|
|
37
|
+
const isLoading = useRewardedInterstitialAdStore((state) =>
|
|
38
|
+
state.isLoading(adKey)
|
|
39
|
+
);
|
|
40
|
+
const error = useRewardedInterstitialAdStore((state) =>
|
|
41
|
+
state.getError(adKey)
|
|
42
|
+
);
|
|
43
|
+
const isUsingHighPriority = useRewardedInterstitialAdStore((state) =>
|
|
44
|
+
state.isUsingHighPriority(adKey)
|
|
45
|
+
);
|
|
46
|
+
const hasTriedHighPriority = useRewardedInterstitialAdStore((state) =>
|
|
47
|
+
state.hasTriedHighPriority(adKey)
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
// Zustand actions
|
|
51
|
+
const preloadAd = useRewardedInterstitialAdStore((state) => state.preloadAd);
|
|
52
|
+
const loadAd = useRewardedInterstitialAdStore((state) => state.loadAd);
|
|
53
|
+
const displayAd = useRewardedInterstitialAdStore((state) => state.displayAd);
|
|
54
|
+
const removeAd = useRewardedInterstitialAdStore((state) => state.removeAd);
|
|
55
|
+
const refreshAd = useRewardedInterstitialAdStore((state) => state.refreshAd);
|
|
56
|
+
|
|
57
|
+
// Memoized callbacks
|
|
58
|
+
const handleLoadAd = useCallback(
|
|
59
|
+
async (customConfig?: Partial<RewardedInterstitialAdConfig>) => {
|
|
60
|
+
const finalConfig: RewardedInterstitialAdConfig = {
|
|
61
|
+
adKey,
|
|
62
|
+
adId,
|
|
63
|
+
showOnAppStateChange,
|
|
64
|
+
timeout,
|
|
65
|
+
maxRetries,
|
|
66
|
+
adHighpriorityId,
|
|
67
|
+
...customConfig,
|
|
68
|
+
};
|
|
69
|
+
await loadAd(finalConfig);
|
|
70
|
+
},
|
|
71
|
+
[
|
|
72
|
+
adKey,
|
|
73
|
+
adId,
|
|
74
|
+
loadAd,
|
|
75
|
+
showOnAppStateChange,
|
|
76
|
+
timeout,
|
|
77
|
+
maxRetries,
|
|
78
|
+
adHighpriorityId,
|
|
79
|
+
]
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const handlePreloadAd = useCallback(
|
|
83
|
+
async (customConfig?: Partial<RewardedInterstitialAdConfig>) => {
|
|
84
|
+
const finalConfig: RewardedInterstitialAdConfig = {
|
|
85
|
+
adKey,
|
|
86
|
+
adId,
|
|
87
|
+
showOnAppStateChange,
|
|
88
|
+
timeout,
|
|
89
|
+
maxRetries,
|
|
90
|
+
adHighpriorityId,
|
|
91
|
+
...customConfig,
|
|
92
|
+
};
|
|
93
|
+
await preloadAd(finalConfig);
|
|
94
|
+
},
|
|
95
|
+
[
|
|
96
|
+
adKey,
|
|
97
|
+
adId,
|
|
98
|
+
preloadAd,
|
|
99
|
+
showOnAppStateChange,
|
|
100
|
+
timeout,
|
|
101
|
+
maxRetries,
|
|
102
|
+
adHighpriorityId,
|
|
103
|
+
]
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const handleDisplayAd = useCallback(async () => {
|
|
107
|
+
const result = await displayAd(adKey);
|
|
108
|
+
if (result.ad && onDisplay) {
|
|
109
|
+
onDisplay(result.ad);
|
|
110
|
+
}
|
|
111
|
+
if (result.reward && onReward) {
|
|
112
|
+
onReward(result.reward);
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
}, [adKey, displayAd, onDisplay, onReward]);
|
|
116
|
+
|
|
117
|
+
const handleRemoveAd = useCallback(() => {
|
|
118
|
+
removeAd(adKey);
|
|
119
|
+
}, [adKey, removeAd]);
|
|
120
|
+
|
|
121
|
+
const handleRefreshAd = useCallback(async () => {
|
|
122
|
+
await refreshAd(adKey);
|
|
123
|
+
}, [adKey, refreshAd]);
|
|
124
|
+
|
|
125
|
+
// Auto load effect
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
if (autoLoad && !ad) {
|
|
128
|
+
handleLoadAd();
|
|
129
|
+
}
|
|
130
|
+
}, [autoLoad, ad, handleLoadAd]);
|
|
131
|
+
|
|
132
|
+
// Event callbacks
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
if (ad?.status === AdStatus.LOADED && onLoad) {
|
|
135
|
+
onLoad(ad);
|
|
136
|
+
}
|
|
137
|
+
}, [ad?.status, ad, onLoad]);
|
|
138
|
+
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
if (ad?.status === AdStatus.DISPLAYED && onDisplay) {
|
|
141
|
+
onDisplay(ad);
|
|
142
|
+
}
|
|
143
|
+
}, [ad?.status, ad, onDisplay]);
|
|
144
|
+
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
if (ad?.status === AdStatus.ERROR && ad.error && onError) {
|
|
147
|
+
onError(ad.error, ad);
|
|
148
|
+
}
|
|
149
|
+
}, [ad?.status, ad, onError]);
|
|
150
|
+
|
|
151
|
+
// Lắng nghe các sự kiện từ RewardedInterstitialAdsManager
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
if (!ad) return;
|
|
154
|
+
|
|
155
|
+
const handleAdOpened = (openedAd: any) => {
|
|
156
|
+
if (openedAd.adKey === adKey && onOpened) {
|
|
157
|
+
onOpened(openedAd);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const handleAdClicked = (clickedAd: any) => {
|
|
162
|
+
if (clickedAd.adKey === adKey && onClicked) {
|
|
163
|
+
onClicked(clickedAd);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const handleAdClosed = (closedAd: any) => {
|
|
168
|
+
if (closedAd.adKey === adKey && onClosed) {
|
|
169
|
+
onClosed(closedAd);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const handleEarnedReward = ({ ad: rewardAd, reward }: any) => {
|
|
174
|
+
if (rewardAd.adKey === adKey && onReward) {
|
|
175
|
+
onReward(reward);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
rewardedInterstitialAdsManager.on('adOpened', handleAdOpened);
|
|
180
|
+
rewardedInterstitialAdsManager.on('adClicked', handleAdClicked);
|
|
181
|
+
rewardedInterstitialAdsManager.on('adClosed', handleAdClosed);
|
|
182
|
+
rewardedInterstitialAdsManager.on('adEarnedReward', handleEarnedReward);
|
|
183
|
+
|
|
184
|
+
return () => {
|
|
185
|
+
rewardedInterstitialAdsManager.off('adOpened', handleAdOpened);
|
|
186
|
+
rewardedInterstitialAdsManager.off('adClicked', handleAdClicked);
|
|
187
|
+
rewardedInterstitialAdsManager.off('adClosed', handleAdClosed);
|
|
188
|
+
rewardedInterstitialAdsManager.off('adEarnedReward', handleEarnedReward);
|
|
189
|
+
};
|
|
190
|
+
}, [adKey, onOpened, onClicked, onClosed, onReward, ad]);
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
// State
|
|
194
|
+
ad,
|
|
195
|
+
isLoading,
|
|
196
|
+
error,
|
|
197
|
+
status: ad?.status || AdStatus.IDLE,
|
|
198
|
+
isLoaded: ad?.status === AdStatus.LOADED,
|
|
199
|
+
isDisplayed: ad?.status === AdStatus.DISPLAYED,
|
|
200
|
+
hasError: ad?.status === AdStatus.ERROR,
|
|
201
|
+
reward: ad?.reward,
|
|
202
|
+
isUsingHighPriority,
|
|
203
|
+
hasTriedHighPriority,
|
|
204
|
+
|
|
205
|
+
// Actions
|
|
206
|
+
loadAd: handleLoadAd,
|
|
207
|
+
preloadAd: handlePreloadAd,
|
|
208
|
+
displayAd: handleDisplayAd,
|
|
209
|
+
removeAd: handleRemoveAd,
|
|
210
|
+
refreshAd: handleRefreshAd,
|
|
211
|
+
};
|
|
212
|
+
};
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Export tất cả các components và hooks
|
|
2
|
+
export { useNativeAd } from './hook/useNativeAd';
|
|
3
|
+
export { useInterstitialAd } from './hook/useInterstitialAd';
|
|
4
|
+
export { useRewardedAd } from './hook/useRewardedAd';
|
|
5
|
+
export { useRewardedInterstitialAd } from './hook/useRewardedInterstitialAd';
|
|
6
|
+
export { useAppOpenAd } from './hook/useAppOpenAd';
|
|
7
|
+
|
|
8
|
+
export { BannerAdComponent } from './components/BannerAdComponent';
|
|
9
|
+
export { NativeAdComponent } from './components/NativeAdComponent';
|
|
10
|
+
export { NativeAdFullscreenComponent } from './components/NativeAdFullscreenComponent';
|
|
11
|
+
|
|
12
|
+
// Export types
|
|
13
|
+
export type {
|
|
14
|
+
AdStatus,
|
|
15
|
+
AdConfig,
|
|
16
|
+
AdData,
|
|
17
|
+
AdInstance,
|
|
18
|
+
AdManagerState,
|
|
19
|
+
AdManagerActions,
|
|
20
|
+
AdManagerContextType,
|
|
21
|
+
UseAdOptions,
|
|
22
|
+
UseNativeAdOptions,
|
|
23
|
+
UseBannerAdOptions,
|
|
24
|
+
UseInterstitialAdOptions,
|
|
25
|
+
UseRewardedAdOptions,
|
|
26
|
+
UseRewardedInterstitialAdOptions,
|
|
27
|
+
InterstitialAdConfig,
|
|
28
|
+
RewardedAdConfig,
|
|
29
|
+
RewardedInterstitialAdConfig,
|
|
30
|
+
RewardedAdInstance,
|
|
31
|
+
InterstitialAdInstance,
|
|
32
|
+
RewardedInterstitialAdInstance,
|
|
33
|
+
} from './types/ads-types';
|