forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathads_loader.dart
More file actions
191 lines (170 loc) · 6.38 KB
/
ads_loader.dart
File metadata and controls
191 lines (170 loc) · 6.38 KB
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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/foundation.dart';
import 'ad_display_container.dart';
import 'ads_manager_delegate.dart';
import 'ads_request.dart';
import 'platform_interface/platform_interface.dart';
/// Allows publishers to request ads from ad servers or a dynamic ad insertion
/// stream.
///
/// ## Platform-Specific Features
/// This class contains an underlying implementation provided by the current
/// platform. Once a platform implementation is imported, the examples below
/// can be followed to use features provided by a platform's implementation.
///
/// {@macro interactive_media_ads.AdsLoader.fromPlatformCreationParams}
///
/// Below is an example of accessing the platform-specific implementation for
/// iOS and Android:
///
/// ```dart
/// final AdsLoader loader = AdsLoader();
///
/// if (InteractiveMediaAdsPlatform.instance is IOSInteractiveMediaAdsPlatform) {
/// final IOSAdsLoader iosLoader = loader.platform as IOSAdsLoader;
/// } else if (InteractiveMediaAdsPlatform.instance is AndroidInteractiveMediaAdsPlatform) {
/// final AndroidAdsLoader androidLoader =
/// loader.platform as AndroidAdsLoader;
/// }
/// ```
class AdsLoader {
/// Constructs an [AdsLoader].
///
/// See [AdsLoader.fromPlatformCreationParams] for setting parameters for a
/// specific platform.
AdsLoader({
required AdDisplayContainer container,
required void Function(OnAdsLoadedData data) onAdsLoaded,
required void Function(AdsLoadErrorData data) onAdsLoadError,
}) : this.fromPlatformCreationParams(
PlatformAdsLoaderCreationParams(
container: container.platform,
onAdsLoaded: (PlatformOnAdsLoadedData data) {
onAdsLoaded(OnAdsLoadedData._(platform: data));
},
onAdsLoadError: onAdsLoadError,
),
);
/// Constructs an [AdsLoader] from creation params for a specific platform.
///
/// {@template interactive_media_ads.AdsLoader.fromPlatformCreationParams}
/// Below is an example of setting platform-specific creation parameters for
/// iOS and Android:
///
/// ```dart
/// PlatformAdsLoaderCreationParams params =
/// const PlatformAdsLoaderCreationParams();
///
/// if (InteractiveMediaAdsPlatform.instance is IOSInteractiveMediaAdsPlatform) {
/// params = IOSAdsLoaderCreationParams
/// .fromPlatformAdsLoaderCreationParams(
/// params,
/// );
/// } else if (InteractiveMediaAdsPlatform.instance is AndroidInteractiveMediaAdsPlatform) {
/// params = AndroidAdsLoaderCreationParams
/// .fromPlatformAdsLoaderCreationParams(
/// params,
/// );
/// }
///
/// final AdsLoader loader = AdsLoader.fromPlatformCreationParams(
/// params,
/// );
/// ```
/// {@endtemplate}
AdsLoader.fromPlatformCreationParams(
PlatformAdsLoaderCreationParams params,
) : this.fromPlatform(PlatformAdsLoader(params));
/// Constructs a [AdsLoader] from a specific platform implementation.
AdsLoader.fromPlatform(this.platform);
/// Implementation of [PlatformAdsLoader] for the current platform.
final PlatformAdsLoader platform;
/// Signals to the SDK that the content has completed.
Future<void> contentComplete() {
return platform.contentComplete();
}
/// Requests ads from a server.
///
/// Ads cannot be requested until the `AdDisplayContainer` has been added to
/// the native View hierarchy. See [AdDisplayContainer.onContainerAdded].
Future<void> requestAds(AdsRequest request) {
return platform.requestAds(request.platform);
}
}
/// Data when ads are successfully loaded from the ad server through an
/// [AdsLoader].
@immutable
class OnAdsLoadedData {
OnAdsLoadedData._({required this.platform});
/// Implementation of [PlatformOnAdsLoadedData] for the current platform.
final PlatformOnAdsLoadedData platform;
/// The ads manager instance created by the ads loader.
late final AdsManager manager = AdsManager._fromPlatform(platform.manager);
}
/// Handles playing ads after they've been received from the server.
///
/// ## Platform-Specific Features
/// This class contains an underlying implementation provided by the current
/// platform. Once a platform implementation is imported, the examples below
/// can be followed to use features provided by a platform's implementation.
///
/// {@macro interactive_media_ads.AdsManager.fromPlatformCreationParams}
///
/// Below is an example of accessing the platform-specific implementation for
/// iOS and Android:
///
/// ```dart
/// final AdsManager manager = AdsManager();
///
/// if (InteractiveMediaAdsPlatform.instance is IOSInteractiveMediaAdsPlatform) {
/// final IOSAdsManager iosManager = manager.platform as IOSAdsManager;
/// } else if (InteractiveMediaAdsPlatform.instance is AndroidInteractiveMediaAdsPlatform) {
/// final AndroidAdsManager androidManager =
/// manager.platform as AndroidAdsManager;
/// }
/// ```
class AdsManager {
/// Constructs a [AdsManager] from a specific platform implementation.
AdsManager._fromPlatform(this.platform);
/// Implementation of [PlatformAdsManager] for the current platform.
final PlatformAdsManager platform;
/// Initializes the ad experience using default rendering settings.
Future<void> init() {
return platform.init(AdsManagerInitParams());
}
/// Starts playing the ads.
Future<void> start() {
return platform.start(AdsManagerStartParams());
}
/// The [AdsManagerDelegate] to notify with events during ad playback.
Future<void> setAdsManagerDelegate(AdsManagerDelegate delegate) {
return platform.setAdsManagerDelegate(delegate.platform);
}
/// Pauses the current ad.
Future<void> pause() {
return platform.pause();
}
/// Resumes the current ad.
Future<void> resume() {
return platform.resume();
}
/// Skips the current ad.
///
/// This only skips ads if IMA does not render the 'Skip ad' button.
Future<void> skip() {
return platform.skip();
}
/// Discards current ad break and resumes content.
///
/// If there is no current ad then the next ad break is discarded.
Future<void> discardAdBreak() {
return platform.discardAdBreak();
}
/// Stops the ad and all tracking, then releases all assets that were loaded
/// to play the ad.
Future<void> destroy() {
return platform.destroy();
}
}