/
google_ad_interstitial.dart
279 lines (262 loc) · 8.08 KB
/
google_ad_interstitial.dart
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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
part of masamune_ads_google.others;
/// This class displays interstitial advertisements.
///
/// Display interstitial ads by specifying [adUnitId].
///
/// After [loading] finishes, [show] displays the advertisement.
///
/// インタースティシャル広告を表示するクラスです。
///
/// [adUnitId]を指定してインタースティシャル広告を表示します。
///
/// [loading]が終了した後、[show]で広告を表示します。
class GoogleAdInterstitial
extends MasamuneControllerBase<void, GoogleAdsMasamuneAdapter> {
/// This class displays interstitial advertisements.
///
/// Display interstitial ads by specifying [adUnitId].
///
/// After [loading] finishes, [show] displays the advertisement.
///
/// インタースティシャル広告を表示するクラスです。
///
/// [adUnitId]を指定してインタースティシャル広告を表示します。
///
/// [loading]が終了した後、[show]で広告を表示します。
GoogleAdInterstitial(
this.adUnitId, {
super.adapter,
}) {
_initialize();
}
@override
GoogleAdsMasamuneAdapter get primaryAdapter =>
GoogleAdsMasamuneAdapter.primary;
/// Ad unit ID.
///
/// 広告ユニットID。
final String adUnitId;
/// Returns `true` if initialization is complete.
///
/// 初期化が完了している場合`true`を返します。
bool get initialized => _ad != null;
InterstitialAd? _ad;
/// Returns [Future] if reading is in progress.
///
/// 読み込み中の場合は[Future]を返します。
Future<void>? get loading => _loadCompleter?.future;
Completer<void>? _loadCompleter;
/// Returns [Future] if displaying is in progress.
///
/// 表示中の場合は[Future]を返します。
Future<void>? get showing => _showCompleter?.future;
Completer<void>? _showCompleter;
Future<void> _initialize() async {
_loadCompleter = Completer<void>();
try {
await InterstitialAd.load(
adUnitId: adUnitId,
request: const AdManagerAdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (ad) {
_ad = ad;
_ad!.setImmersiveMode(true);
},
onAdFailedToLoad: (error) {
debugPrint("InterstitialAd failed to load: $error.");
_ad = null;
},
),
);
notifyListeners();
_loadCompleter?.complete();
_loadCompleter = null;
} catch (e) {
_loadCompleter?.completeError(e);
_loadCompleter = null;
} finally {
_loadCompleter?.complete();
_loadCompleter = null;
}
}
/// Display the advertisement.
///
/// You can specify a callback when an ad is clicked with [onAdClicked].
///
/// 広告を表示します。
///
/// [onAdClicked]で広告がクリックされた時のコールバックを指定できます。
Future<void> show({
VoidCallback? onAdClicked,
}) async {
if (!initialized) {
throw Exception("InterstitialAd is not initialized.");
}
if (_showCompleter != null) {
return _showCompleter!.future;
}
_showCompleter = Completer<void>();
try {
_ad!.fullScreenContentCallback = FullScreenContentCallback(
onAdClicked: (ad) {
onAdClicked?.call();
},
);
await _ad!.show();
notifyListeners();
_showCompleter?.complete();
_showCompleter = null;
} catch (e) {
_showCompleter?.completeError(e);
_showCompleter = null;
} finally {
_showCompleter?.complete();
_showCompleter = null;
}
}
@override
Future<void> dispose() async {
super.dispose();
await _ad?.dispose();
_ad = null;
}
}
/// This class displays a rewarded interstitial ad.
///
/// Display interstitial ads by specifying [adUnitId].
///
/// After [loading] finishes, [show] displays the advertisement.
///
/// リワード付きのインタースティシャル広告を表示するクラスです。
///
/// [adUnitId]を指定してインタースティシャル広告を表示します。
///
/// [loading]が終了した後、[show]で広告を表示します。
class GoogleAdRewardedInterstitial extends ChangeNotifier {
/// This class displays a rewarded interstitial ad.
///
/// Display interstitial ads by specifying [adUnitId].
///
/// After [loading] finishes, [show] displays the advertisement.
///
/// リワード付きのインタースティシャル広告を表示するクラスです。
///
/// [adUnitId]を指定してインタースティシャル広告を表示します。
///
/// [loading]が終了した後、[show]で広告を表示します。
GoogleAdRewardedInterstitial(
this.adUnitId,
) {
_initialize();
}
/// Ad unit ID.
///
/// 広告ユニットID。
final String adUnitId;
/// Returns `true` if initialization is complete.
///
/// 初期化が完了している場合`true`を返します。
bool get initialized => _ad != null;
RewardedInterstitialAd? _ad;
/// Returns [Future] if reading is in progress.
///
/// 読み込み中の場合は[Future]を返します。
Future<void>? get loading => _loadCompleter?.future;
Completer<void>? _loadCompleter;
/// Returns [Future] if displaying is in progress.
///
/// 表示中の場合は[Future]を返します。
Future<void>? get showing => _showCompleter?.future;
Completer<void>? _showCompleter;
Completer<void>? _rewardCompleter;
Future<void> _initialize() async {
_loadCompleter = Completer<void>();
try {
await RewardedInterstitialAd.load(
adUnitId: adUnitId,
request: const AdManagerAdRequest(),
rewardedInterstitialAdLoadCallback: RewardedInterstitialAdLoadCallback(
onAdLoaded: (ad) {
_ad = ad;
_ad!.setImmersiveMode(true);
},
onAdFailedToLoad: (error) {
debugPrint("RewardedInterstitialAd failed to load: $error");
_ad = null;
},
),
);
notifyListeners();
_loadCompleter?.complete();
_loadCompleter = null;
} catch (e) {
_loadCompleter?.completeError(e);
_loadCompleter = null;
} finally {
_loadCompleter?.complete();
_loadCompleter = null;
}
}
/// Display the advertisement.
///
/// You can specify a callback when a reward is earned with [onEarnedReward].
///
/// You can specify a callback when an ad is clicked with [onAdClicked].
///
/// 広告を表示します。
///
/// [onEarnedReward]でリワードを獲得した時のコールバックを指定できます。
///
/// [onAdClicked]で広告がクリックされた時のコールバックを指定できます。
Future<void> show({
required FutureOr<void> Function(double amount, String type) onEarnedReward,
VoidCallback? onAdClicked,
}) async {
if (!initialized) {
throw Exception("RewardedInterstitialAd is not initialized.");
}
if (_showCompleter != null) {
return _showCompleter!.future;
}
_showCompleter = Completer<void>();
try {
_ad!.fullScreenContentCallback = FullScreenContentCallback(
onAdClicked: (ad) {
onAdClicked?.call();
},
);
await _ad!.show(
onUserEarnedReward: (ad, reward) async {
_rewardCompleter = Completer<void>();
try {
await onEarnedReward.call(reward.amount.toDouble(), reward.type);
_rewardCompleter?.complete();
_rewardCompleter = null;
} catch (e) {
_rewardCompleter?.completeError(e);
_rewardCompleter = null;
} finally {
_rewardCompleter?.complete();
_rewardCompleter = null;
}
},
);
await _rewardCompleter?.future;
notifyListeners();
_showCompleter?.complete();
_showCompleter = null;
} catch (e) {
_showCompleter?.completeError(e);
_showCompleter = null;
} finally {
_showCompleter?.complete();
_showCompleter = null;
}
}
@override
Future<void> dispose() async {
super.dispose();
await _ad?.dispose();
_ad = null;
}
}