|
1 | | -import Foundation |
2 | | -import UIKit |
3 | | -import GoogleMobileAds |
4 | | - |
5 | | -enum AMBCoreError: Error { |
6 | | - case notImplemented |
7 | | - case unknown |
8 | | -} |
9 | | - |
10 | | -protocol AMBHelperAdapter { |
11 | | -} |
12 | | - |
13 | | -extension AMBHelperAdapter { |
14 | | -} |
15 | | - |
16 | | -class AMBHelper { |
17 | | - static let window = UIApplication.shared.windows.first(where: { $0.isKeyWindow })! |
18 | | - |
19 | | - static var topAnchor: NSLayoutYAxisAnchor { |
20 | | - if #available(iOS 11.0, *) { |
21 | | - return window.safeAreaLayoutGuide.topAnchor |
22 | | - } else { |
23 | | - return window.topAnchor |
24 | | - } |
25 | | - } |
26 | | - |
27 | | - static var bottomAnchor: NSLayoutYAxisAnchor { |
28 | | - if #available(iOS 11.0, *) { |
29 | | - return window.safeAreaLayoutGuide.bottomAnchor |
30 | | - } else { |
31 | | - return window.bottomAnchor |
32 | | - } |
33 | | - } |
34 | | - |
35 | | - static var frame: CGRect { |
36 | | - if #available(iOS 11.0, *) { |
37 | | - return window.frame.inset(by: window.safeAreaInsets) |
38 | | - } else { |
39 | | - return window.frame |
40 | | - } |
41 | | - } |
42 | | - |
43 | | - let adapter: AMBHelperAdapter |
44 | | - |
45 | | - init(_ adapter: AMBHelperAdapter) { |
46 | | - self.adapter = adapter |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -protocol AMBCoreContext { |
51 | | - func has(_ name: String) -> Bool |
52 | | - func optBool(_ name: String) -> Bool? |
53 | | - func optFloat(_ name: String) -> Float? |
54 | | - func optInt(_ name: String) -> Int? |
55 | | - func optString(_ name: String, _ defaultValue: String) -> String |
56 | | - func optStringArray(_ name: String) -> [String]? |
57 | | - |
58 | | - func resolve(_ data: [String: Any]) |
59 | | - func resolve(_ data: Bool) |
60 | | - |
61 | | - func reject(_ msg: String) |
62 | | -} |
63 | | - |
64 | | -extension AMBCoreContext { |
65 | | - func optString(_ name: String) -> String? { |
66 | | - if has(name) { |
67 | | - return optString(name, "") |
68 | | - } |
69 | | - return nil |
70 | | - } |
71 | | - |
72 | | - func optAppMuted() -> Bool? { |
73 | | - return optBool("appMuted") |
74 | | - } |
75 | | - |
76 | | - func optAppVolume() -> Float? { |
77 | | - return optFloat("appVolume") |
78 | | - } |
79 | | - |
80 | | - func optId() -> String? { |
81 | | - return optString("id") |
82 | | - } |
83 | | - |
84 | | - func optPosition() -> String { |
85 | | - return optString("position", "bottom") |
86 | | - } |
87 | | - |
88 | | - func optAdUnitID() -> String? { |
89 | | - return optString("adUnitId") |
90 | | - } |
91 | | - |
92 | | - func optAd() -> AMBCoreAd? { |
93 | | - guard let id = optId(), |
94 | | - let ad = AMBCoreAd.ads[id] |
95 | | - else { |
96 | | - return nil |
97 | | - } |
98 | | - return ad |
99 | | - } |
100 | | - |
101 | | - func optAdOrError() -> AMBCoreAd? { |
102 | | - if let ad = optAd() { |
103 | | - return ad |
104 | | - } else { |
105 | | - reject("Ad not found: \(optId() ?? "-")") |
106 | | - return nil |
107 | | - } |
108 | | - } |
109 | | - |
110 | | - func optMaxAdContentRating() -> MaxAdContentRating? { |
111 | | - switch optString("maxAdContentRating") { |
112 | | - case "G": |
113 | | - return MaxAdContentRating.general |
114 | | - case "MA": |
115 | | - return MaxAdContentRating.matureAudience |
116 | | - case "PG": |
117 | | - return MaxAdContentRating.parentalGuidance |
118 | | - case "T": |
119 | | - return MaxAdContentRating.teen |
120 | | - default: |
121 | | - return nil |
122 | | - } |
123 | | - } |
124 | | - |
125 | | - func optChildDirectedTreatmentTag() -> Bool? { |
126 | | - return optBool("tagForChildDirectedTreatment") |
127 | | - } |
128 | | - |
129 | | - func optUnderAgeOfConsentTag() -> Bool? { |
130 | | - return optBool("tagForUnderAgeOfConsent") |
131 | | - } |
132 | | - |
133 | | - func optTestDeviceIds() -> [String]? { |
134 | | - return optStringArray("testDeviceIds") |
135 | | - } |
136 | | - |
137 | | - func optRequest() -> Request { |
138 | | - let request = Request() |
139 | | - if let contentURL = optString("contentUrl") { |
140 | | - request.contentURL = contentURL |
141 | | - } |
142 | | - if let keywords = optStringArray("keywords") { |
143 | | - request.keywords = keywords |
144 | | - } |
145 | | - let extras = Extras() |
146 | | - if let npa = optString("npa") { |
147 | | - extras.additionalParameters = ["npa": npa] |
148 | | - } |
149 | | - request.register(extras) |
150 | | - return request |
151 | | - } |
152 | | - |
153 | | - func resolve() { |
154 | | - resolve([:]) |
155 | | - } |
156 | | - |
157 | | - func resolve(_ data: Bool) { |
158 | | - resolve(["value": data]) |
159 | | - } |
160 | | - |
161 | | - func reject() { |
162 | | - return reject(AMBCoreError.unknown) |
163 | | - } |
164 | | - |
165 | | - func reject(_ error: Error) { |
166 | | - reject(error.localizedDescription) |
167 | | - } |
168 | | - |
169 | | - func configure() { |
170 | | - if let muted = optAppMuted() { |
171 | | - MobileAds.shared.applicationMuted = muted |
172 | | - } |
173 | | - if let volume = optAppVolume() { |
174 | | - MobileAds.shared.applicationVolume = volume |
175 | | - } |
176 | | - |
177 | | - let requestConfiguration = MobileAds.shared.requestConfiguration |
178 | | - if let maxAdContentRating = optMaxAdContentRating() { |
179 | | - requestConfiguration.maxAdContentRating = maxAdContentRating |
180 | | - } |
181 | | - if let tag = optChildDirectedTreatmentTag() { |
182 | | - requestConfiguration.tagForChildDirectedTreatment = NSNumber(value: tag) |
183 | | - } |
184 | | - if let tag = optUnderAgeOfConsentTag() { |
185 | | - requestConfiguration.tagForUnderAgeOfConsent = NSNumber(value: tag) |
186 | | - } |
187 | | - if let testDevices = optTestDeviceIds() { |
188 | | - requestConfiguration.testDeviceIdentifiers = testDevices |
189 | | - } |
190 | | - if let sameAppKey = optBool("sameAppKey") { |
191 | | - requestConfiguration.setPublisherFirstPartyIDEnabled(sameAppKey) |
192 | | - } |
193 | | - if let |
194 | | - publisherFirstPartyIDEnabled = optBool("publisherFirstPartyIDEnabled") { |
195 | | - requestConfiguration.setPublisherFirstPartyIDEnabled(publisherFirstPartyIDEnabled) |
196 | | - } |
197 | | - |
198 | | - resolve() |
199 | | - } |
200 | | -} |
201 | | - |
202 | | -class AMBCoreAd: NSObject { |
203 | | - static var ads = [String: AMBCoreAd]() |
204 | | - |
205 | | - let id: String |
206 | | - let adUnitId: String |
207 | | - let adRequest: Request |
208 | | - |
209 | | - init(id: String, adUnitId: String, adRequest: Request) { |
210 | | - self.id = id |
211 | | - self.adUnitId = adUnitId |
212 | | - self.adRequest = adRequest |
213 | | - |
214 | | - super.init() |
215 | | - |
216 | | - AMBCoreAd.ads[id] = self |
217 | | - } |
218 | | - |
219 | | - convenience init?(_ ctx: AMBCoreContext) { |
220 | | - guard let id = ctx.optId(), |
221 | | - let adUnitId = ctx.optAdUnitID() |
222 | | - else { |
223 | | - return nil |
224 | | - } |
225 | | - self.init(id: id, adUnitId: adUnitId, adRequest: ctx.optRequest()) |
226 | | - } |
227 | | - |
228 | | - deinit { |
229 | | - let key = self.id |
230 | | - DispatchQueue.main.async { |
231 | | - AMBCoreAd.ads.removeValue(forKey: key) |
232 | | - } |
233 | | - } |
234 | | -} |
235 | | - |
236 | | -class AMBBannerPlaceholder: UIView {} |
0 commit comments