@cardsjd/portal 0.1.16 → 0.1.17
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/lib/NextGenAdMobPortal.js +504 -442
- package/package.json +1 -1
|
@@ -1,451 +1,513 @@
|
|
|
1
|
-
import { BasePortal } from './BasePortal.js';
|
|
2
|
-
|
|
3
|
-
export class NextGenAdMobPortal extends BasePortal {
|
|
4
|
-
name = 'NextGenAdMobPortal';
|
|
5
|
-
autoLogin = false;
|
|
6
|
-
|
|
7
|
-
constructor(appConfig) {
|
|
8
|
-
super();
|
|
9
|
-
this.appConfig = appConfig;
|
|
10
|
-
|
|
11
|
-
// Ad callback properties
|
|
12
|
-
this.onAdLoaded = null;
|
|
13
|
-
this.onAdOpened = null;
|
|
14
|
-
this.onAdClosed = null;
|
|
15
|
-
this.onAdError = null;
|
|
16
|
-
this.onAdRewarded = null;
|
|
17
|
-
this.onBeforeTrackingAuthorization = null;
|
|
18
|
-
this.onPrivacyOptionsRequirementChanged = null;
|
|
19
|
-
|
|
20
|
-
this.adProviderName = 'CapgoAdmob';
|
|
21
|
-
this.isSetup = false;
|
|
22
|
-
this.isSetupLoading = false;
|
|
23
|
-
this._setupPromise = null;
|
|
24
|
-
this.isInterstitialAdReady = false;
|
|
25
|
-
this._isRewardedAdLoaded = false;
|
|
26
|
-
this._receivedReward = false;
|
|
27
|
-
this._rewardInfo = null;
|
|
1
|
+
import { BasePortal } from './BasePortal.js';
|
|
2
|
+
|
|
3
|
+
export class NextGenAdMobPortal extends BasePortal {
|
|
4
|
+
name = 'NextGenAdMobPortal';
|
|
5
|
+
autoLogin = false;
|
|
6
|
+
|
|
7
|
+
constructor(appConfig) {
|
|
8
|
+
super();
|
|
9
|
+
this.appConfig = appConfig;
|
|
10
|
+
|
|
11
|
+
// Ad callback properties
|
|
12
|
+
this.onAdLoaded = null;
|
|
13
|
+
this.onAdOpened = null;
|
|
14
|
+
this.onAdClosed = null;
|
|
15
|
+
this.onAdError = null;
|
|
16
|
+
this.onAdRewarded = null;
|
|
17
|
+
this.onBeforeTrackingAuthorization = null;
|
|
18
|
+
this.onPrivacyOptionsRequirementChanged = null;
|
|
19
|
+
|
|
20
|
+
this.adProviderName = 'CapgoAdmob';
|
|
21
|
+
this.isSetup = false;
|
|
22
|
+
this.isSetupLoading = false;
|
|
23
|
+
this._setupPromise = null;
|
|
24
|
+
this.isInterstitialAdReady = false;
|
|
25
|
+
this._isRewardedAdLoaded = false;
|
|
26
|
+
this._receivedReward = false;
|
|
27
|
+
this._rewardInfo = null;
|
|
28
28
|
this._interstitialAd = null;
|
|
29
|
-
this
|
|
30
|
-
this.
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
33
|
-
this.
|
|
34
|
-
this.
|
|
35
|
-
this.
|
|
36
|
-
this.
|
|
37
|
-
this.
|
|
38
|
-
this.
|
|
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
|
-
this.onPrivacyOptionsRequirementChanged
|
|
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
|
-
this.
|
|
119
|
-
this.
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
this.
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
this.
|
|
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
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
await AdMob.
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
29
|
+
// Native show resolves on opening; keep this until dismissal or show failure.
|
|
30
|
+
this._showingAd = null;
|
|
31
|
+
this._interstitialLoadPromise = null;
|
|
32
|
+
this._rewardedAd = null;
|
|
33
|
+
this._adMobModule = null;
|
|
34
|
+
this._umpModule = null;
|
|
35
|
+
this._consentInfoUpdated = false;
|
|
36
|
+
this._consentGathered = false;
|
|
37
|
+
this._canRequestAds = false;
|
|
38
|
+
this._limitedAdsFallback = false;
|
|
39
|
+
this._privacyOptionsRequired = false;
|
|
40
|
+
this.isTesting = false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async getAdMob() {
|
|
44
|
+
if (!this._adMobModule) {
|
|
45
|
+
// eslint-disable-next-line import/no-unresolved
|
|
46
|
+
this._adMobModule = await import('@capgo/capacitor-admob');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return this._adMobModule;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async loadUmp() {
|
|
53
|
+
if (!this._umpModule) {
|
|
54
|
+
// eslint-disable-next-line import/no-unresolved
|
|
55
|
+
const { GoogleUmpConsent } = await import('@cardsjd/google-ump');
|
|
56
|
+
this._umpModule = GoogleUmpConsent;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async requestTrackingAuthorization(_consentInfo) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async canStartAdsWithoutPrompt() {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
setConsentInfo(consentInfo) {
|
|
69
|
+
const wasRequired = this._privacyOptionsRequired;
|
|
70
|
+
if (typeof consentInfo?.canRequestAds === 'boolean') {
|
|
71
|
+
this._canRequestAds = consentInfo.canRequestAds;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
this._privacyOptionsRequired = consentInfo?.privacyOptionsRequired === true;
|
|
75
|
+
|
|
76
|
+
if (
|
|
77
|
+
wasRequired !== this._privacyOptionsRequired &&
|
|
78
|
+
this.onPrivacyOptionsRequirementChanged
|
|
79
|
+
) {
|
|
80
|
+
this.onPrivacyOptionsRequirementChanged(this._privacyOptionsRequired);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
isPrivacyOptionsRequired() {
|
|
85
|
+
return this._privacyOptionsRequired;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async showPrivacyOptions() {
|
|
89
|
+
await this.loadUmp();
|
|
90
|
+
const consentInfo = await this._umpModule.showPrivacyOptions();
|
|
91
|
+
if (consentInfo.canRequestAds === true) {
|
|
92
|
+
this._limitedAdsFallback = false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
this.setConsentInfo(consentInfo);
|
|
96
|
+
return consentInfo;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async requestConsent(method) {
|
|
100
|
+
try {
|
|
101
|
+
await this.loadUmp();
|
|
102
|
+
return await this._umpModule[method]();
|
|
103
|
+
} catch (error) {
|
|
104
|
+
// UMP may still allow ads using consent from a previous session.
|
|
105
|
+
let consentInfo;
|
|
106
|
+
try {
|
|
107
|
+
consentInfo = await this._umpModule.getConsentInfo();
|
|
108
|
+
} catch (statusError) {
|
|
109
|
+
console.warn('Unable to read UMP consent status:', statusError);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (consentInfo?.canRequestAds === true) {
|
|
113
|
+
console.warn('UMP consent request failed; using existing consent:', error);
|
|
114
|
+
return consentInfo;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Set Google's native limited-ad signal before allowing any ad requests.
|
|
118
|
+
await this.loadUmp();
|
|
119
|
+
await this._umpModule.enableLimitedAds();
|
|
120
|
+
this._limitedAdsFallback = true;
|
|
121
|
+
this._consentGathered = true;
|
|
122
|
+
console.warn('UMP consent request failed; requesting limited ads:', error);
|
|
123
|
+
return {
|
|
124
|
+
canRequestAds: true,
|
|
125
|
+
formShown: false,
|
|
126
|
+
privacyOptionsRequired: consentInfo?.privacyOptionsRequired ?? this._privacyOptionsRequired,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async updateConsentInfo() {
|
|
132
|
+
const consentInfo = await this.requestConsent('updateConsentInfo');
|
|
133
|
+
this._consentInfoUpdated = true;
|
|
134
|
+
this.setConsentInfo(consentInfo);
|
|
135
|
+
return consentInfo;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async gatherConsent() {
|
|
139
|
+
const consentInfo = await this.requestConsent('gatherConsent');
|
|
140
|
+
this._consentInfoUpdated = true;
|
|
141
|
+
this._consentGathered = true;
|
|
142
|
+
this.setConsentInfo(consentInfo);
|
|
143
|
+
return consentInfo;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
getAdError(info, fallbackMessage) {
|
|
147
|
+
return new Error(info?.error?.message || info?.message || fallbackMessage);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ----- Ad methods -----
|
|
151
|
+
|
|
152
|
+
async setupAds() {
|
|
153
|
+
if (this.isSetup || this._consentInfoUpdated) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (!this._setupPromise) {
|
|
158
|
+
this._setupPromise = (async () => {
|
|
159
|
+
this.isSetupLoading = true;
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
await this.updateConsentInfo();
|
|
163
|
+
} catch (error) {
|
|
164
|
+
console.error('Failed to update UMP consent info:', error);
|
|
165
|
+
} finally {
|
|
166
|
+
this.isSetupLoading = false;
|
|
167
|
+
}
|
|
168
|
+
})();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const setupPromise = this._setupPromise;
|
|
172
|
+
try {
|
|
173
|
+
await setupPromise;
|
|
174
|
+
} finally {
|
|
175
|
+
if (this._setupPromise === setupPromise) {
|
|
176
|
+
this._setupPromise = null;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async ensureAdsReady() {
|
|
182
|
+
if (this.isSetup) {
|
|
183
|
+
return {
|
|
184
|
+
canRequestAds: true,
|
|
185
|
+
promptShown: false,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
let consentInfo;
|
|
190
|
+
try {
|
|
191
|
+
if (!this._consentInfoUpdated) {
|
|
192
|
+
await this.updateConsentInfo();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
consentInfo = this._consentGathered ? {
|
|
196
|
+
canRequestAds: this._canRequestAds,
|
|
197
|
+
} : await this.gatherConsent();
|
|
198
|
+
} catch (error) {
|
|
199
|
+
if (this.onAdError) {
|
|
200
|
+
this.onAdError(error);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
canRequestAds: false,
|
|
205
|
+
promptShown: false,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const trackingPromptShown = !this._limitedAdsFallback && await this.requestTrackingAuthorization(consentInfo);
|
|
210
|
+
const promptShown = consentInfo?.formShown === true || trackingPromptShown;
|
|
211
|
+
|
|
212
|
+
if (consentInfo.canRequestAds !== true) {
|
|
213
|
+
return {
|
|
214
|
+
canRequestAds: false,
|
|
215
|
+
promptShown,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
await this.startAds();
|
|
220
|
+
return {
|
|
221
|
+
canRequestAds: true,
|
|
222
|
+
promptShown,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async prepareAdsWithoutPrompt() {
|
|
227
|
+
if (this.isSetup) {
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (!this._consentInfoUpdated) {
|
|
232
|
+
await this.setupAds();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (!this._canRequestAds || (!this._limitedAdsFallback && !await this.canStartAdsWithoutPrompt())) {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
await this.startAds();
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async startAds() {
|
|
244
|
+
if (this.isSetup) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (this.isSetupLoading) {
|
|
249
|
+
throw Error('Capgo AdMob setup is in progress already');
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
this.isSetupLoading = true;
|
|
253
|
+
|
|
254
|
+
try {
|
|
255
|
+
const { AdMob } = await this.getAdMob();
|
|
256
|
+
await AdMob.start();
|
|
257
|
+
|
|
258
|
+
await AdMob.addListener('interstitial.show', (info) => {
|
|
259
|
+
if ((info.id ?? info.adId) === this._interstitialAd?.id) {
|
|
260
|
+
this.isInterstitialAdReady = false;
|
|
261
|
+
if (this.onAdOpened) {
|
|
262
|
+
this.onAdOpened({
|
|
263
|
+
adType: 'interstitial',
|
|
264
|
+
adProvider: this.adProviderName,
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
await AdMob.addListener('interstitial.dismiss', (info) => {
|
|
270
|
+
if ((info.id ?? info.adId) === this._interstitialAd?.id) {
|
|
271
|
+
if (this._showingAd === this._interstitialAd) {
|
|
272
|
+
this._showingAd = null;
|
|
264
273
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
if ((info.id ?? info.adId) === this._interstitialAd?.id) {
|
|
290
|
-
this.isInterstitialAdReady = false;
|
|
291
|
-
this._interstitialAd = null;
|
|
292
|
-
if (this.onAdError) {
|
|
293
|
-
this.onAdError(this.getAdError(info, 'InterstitialAd failed to show'));
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
});
|
|
297
|
-
await AdMob.addListener('rewarded.dismiss', (info) => {
|
|
298
|
-
if ((info.id ?? info.adId) === this._rewardedAd?.id) {
|
|
299
|
-
this._isRewardedAdLoaded = false;
|
|
300
|
-
this._rewardedAd = null;
|
|
301
|
-
if (this.onAdClosed) {
|
|
302
|
-
this.onAdClosed({
|
|
303
|
-
adType: 'rewarded',
|
|
304
|
-
adProvider: this.adProviderName,
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
if (this._receivedReward && this.onAdRewarded) {
|
|
309
|
-
this.onAdRewarded(this._rewardInfo);
|
|
274
|
+
|
|
275
|
+
this.isInterstitialAdReady = false;
|
|
276
|
+
this._interstitialAd = null;
|
|
277
|
+
if (this.onAdClosed) {
|
|
278
|
+
this.onAdClosed({
|
|
279
|
+
adType: 'interstitial',
|
|
280
|
+
adProvider: this.adProviderName,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
await AdMob.addListener('interstitial.loadfail', (info) => {
|
|
286
|
+
if ((info.id ?? info.adId) === this._interstitialAd?.id) {
|
|
287
|
+
this.isInterstitialAdReady = false;
|
|
288
|
+
this._interstitialAd = null;
|
|
289
|
+
if (this.onAdError) {
|
|
290
|
+
this.onAdError(this.getAdError(info, 'InterstitialAd failed to load'));
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
await AdMob.addListener('interstitial.showfail', (info) => {
|
|
295
|
+
if ((info.id ?? info.adId) === this._interstitialAd?.id) {
|
|
296
|
+
if (this._showingAd === this._interstitialAd) {
|
|
297
|
+
this._showingAd = null;
|
|
310
298
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
299
|
+
|
|
300
|
+
this.isInterstitialAdReady = false;
|
|
301
|
+
this._interstitialAd = null;
|
|
302
|
+
if (this.onAdError) {
|
|
303
|
+
this.onAdError(this.getAdError(info, 'InterstitialAd failed to show'));
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
await AdMob.addListener('rewarded.dismiss', (info) => {
|
|
308
|
+
if ((info.id ?? info.adId) === this._rewardedAd?.id) {
|
|
309
|
+
if (this._showingAd === this._rewardedAd) {
|
|
310
|
+
this._showingAd = null;
|
|
319
311
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
312
|
+
|
|
313
|
+
this._isRewardedAdLoaded = false;
|
|
314
|
+
this._rewardedAd = null;
|
|
315
|
+
if (this.onAdClosed) {
|
|
316
|
+
this.onAdClosed({
|
|
317
|
+
adType: 'rewarded',
|
|
318
|
+
adProvider: this.adProviderName,
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (this._receivedReward && this.onAdRewarded) {
|
|
323
|
+
this.onAdRewarded(this._rewardInfo);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
await AdMob.addListener('rewarded.loadfail', (info) => {
|
|
328
|
+
if ((info.id ?? info.adId) === this._rewardedAd?.id) {
|
|
329
|
+
this._isRewardedAdLoaded = false;
|
|
330
|
+
this._rewardedAd = null;
|
|
331
|
+
if (this.onAdError) {
|
|
332
|
+
this.onAdError(this.getAdError(info, 'RewardAd failed to load'));
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
await AdMob.addListener('rewarded.showfail', (info) => {
|
|
337
|
+
if ((info.id ?? info.adId) === this._rewardedAd?.id) {
|
|
338
|
+
if (this._showingAd === this._rewardedAd) {
|
|
339
|
+
this._showingAd = null;
|
|
328
340
|
}
|
|
341
|
+
|
|
342
|
+
this._isRewardedAdLoaded = false;
|
|
343
|
+
this._rewardedAd = null;
|
|
344
|
+
if (this.onAdError) {
|
|
345
|
+
this.onAdError(this.getAdError(info, 'RewardAd failed to show'));
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
await AdMob.addListener('rewarded.reward', (info) => {
|
|
350
|
+
if ((info.id ?? info.adId) === this._rewardedAd?.id) {
|
|
351
|
+
this._receivedReward = true;
|
|
352
|
+
this._rewardInfo = info.reward;
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
this.isSetup = true;
|
|
357
|
+
} finally {
|
|
358
|
+
this.isSetupLoading = false;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
isAdLoaded() {
|
|
363
|
+
return this.isInterstitialAdReady;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
isRewardedAdLoaded() {
|
|
367
|
+
return this._isRewardedAdLoaded;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
async loadInterstitialAd() {
|
|
371
|
+
if (this._showingAd || this.isAdLoaded()) {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (!this._interstitialLoadPromise) {
|
|
376
|
+
this._interstitialLoadPromise = (async () => {
|
|
377
|
+
if (!await this.prepareAdsWithoutPrompt()) {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const { InterstitialAd } = await this.getAdMob();
|
|
382
|
+
if (this._showingAd) {
|
|
383
|
+
return;
|
|
329
384
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
385
|
+
|
|
386
|
+
this._interstitialAd = new InterstitialAd({
|
|
387
|
+
adUnitId: this.appConfig?.interstitialAdId,
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
await this._interstitialAd.load();
|
|
391
|
+
this.isInterstitialAdReady = true;
|
|
392
|
+
})();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const loadPromise = this._interstitialLoadPromise;
|
|
396
|
+
try {
|
|
397
|
+
await loadPromise;
|
|
398
|
+
} catch (error) {
|
|
399
|
+
this._interstitialAd = null;
|
|
400
|
+
throw error;
|
|
401
|
+
} finally {
|
|
402
|
+
if (this._interstitialLoadPromise === loadPromise) {
|
|
403
|
+
this._interstitialLoadPromise = null;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
async showInterstitialAd() {
|
|
409
|
+
if (this._showingAd) {
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const setup = await this.ensureAdsReady();
|
|
414
|
+
if (this._showingAd || !setup.canRequestAds || setup.promptShown) {
|
|
415
|
+
return false;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (!this.isAdLoaded()) {
|
|
419
|
+
let timeoutId;
|
|
420
|
+
await Promise.race([this.loadInterstitialAd(), new Promise(resolve => {
|
|
421
|
+
timeoutId = setTimeout(resolve, 2000);
|
|
422
|
+
})]);
|
|
423
|
+
clearTimeout(timeoutId);
|
|
424
|
+
if (!this.isAdLoaded()) {
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (this._showingAd) {
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const ad = this._interstitialAd;
|
|
434
|
+
this._showingAd = ad;
|
|
435
|
+
this.isInterstitialAdReady = false;
|
|
436
|
+
try {
|
|
437
|
+
await ad.show();
|
|
438
|
+
} catch (error) {
|
|
439
|
+
if (this._showingAd === ad) {
|
|
440
|
+
this._showingAd = null;
|
|
382
441
|
}
|
|
442
|
+
|
|
443
|
+
console.error('Interstitial ad failed to show', error);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
return true;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
async loadRewardedAd() {
|
|
450
|
+
if (this._showingAd) {
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
if (!await this.prepareAdsWithoutPrompt()) {
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (this.isRewardedAdLoaded()) {
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const { RewardedAd } = await this.getAdMob();
|
|
463
|
+
if (this._showingAd) {
|
|
464
|
+
return;
|
|
383
465
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
466
|
+
|
|
467
|
+
this._rewardedAd = new RewardedAd({
|
|
468
|
+
adUnitId: this.appConfig?.rewardedAdId,
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
try {
|
|
472
|
+
await this._rewardedAd.load();
|
|
473
|
+
this._isRewardedAdLoaded = true;
|
|
474
|
+
this._receivedReward = false;
|
|
475
|
+
this._rewardInfo = null;
|
|
476
|
+
} catch (error) {
|
|
477
|
+
this._rewardedAd = null;
|
|
478
|
+
throw error;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
async showRewardedAd() {
|
|
483
|
+
if (this._showingAd) {
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const setup = await this.ensureAdsReady();
|
|
488
|
+
if (this._showingAd || !setup.canRequestAds || setup.promptShown) {
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (!this.isRewardedAdLoaded()) {
|
|
493
|
+
await this.loadRewardedAd();
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
if (this._showingAd) {
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const ad = this._rewardedAd;
|
|
501
|
+
this._showingAd = ad;
|
|
502
|
+
this._isRewardedAdLoaded = false;
|
|
503
|
+
try {
|
|
504
|
+
return await ad.show();
|
|
505
|
+
} catch (error) {
|
|
506
|
+
if (this._showingAd === ad) {
|
|
507
|
+
this._showingAd = null;
|
|
400
508
|
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
} catch (error) {
|
|
407
|
-
console.error('Interstitial ad failed to show', error);
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
return true;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
async loadRewardedAd() {
|
|
414
|
-
if (!await this.prepareAdsWithoutPrompt()) {
|
|
415
|
-
return;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
if (this.isRewardedAdLoaded()) {
|
|
419
|
-
return;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
const { RewardedAd } = await this.getAdMob();
|
|
423
|
-
this._rewardedAd = new RewardedAd({
|
|
424
|
-
adUnitId: this.appConfig?.rewardedAdId,
|
|
425
|
-
});
|
|
426
|
-
|
|
427
|
-
try {
|
|
428
|
-
await this._rewardedAd.load();
|
|
429
|
-
this._isRewardedAdLoaded = true;
|
|
430
|
-
this._receivedReward = false;
|
|
431
|
-
this._rewardInfo = null;
|
|
432
|
-
} catch (error) {
|
|
433
|
-
this._rewardedAd = null;
|
|
434
|
-
throw error;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
async showRewardedAd() {
|
|
439
|
-
const setup = await this.ensureAdsReady();
|
|
440
|
-
if (!setup.canRequestAds || setup.promptShown) {
|
|
441
|
-
return;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
if (!this.isRewardedAdLoaded()) {
|
|
445
|
-
await this.loadRewardedAd();
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
this._isRewardedAdLoaded = false;
|
|
449
|
-
return this._rewardedAd.show();
|
|
450
|
-
}
|
|
451
|
-
}
|
|
509
|
+
|
|
510
|
+
throw error;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|