yandexads-flutter
167 строк · 5.3 Кб
1//
2// YandexAdsNativeComponent.swift
3// flutter_yandex_ads
4//
5// Created by Artem Kovardin on 06.12.2022.
6//
7
8import Foundation
9import YandexMobileAds
10import Flutter
11
12
13struct NativeData {
14var view: YMANativeBannerView? = nil
15var delegate: YandexAdsNativeLoadedDelegate? = nil
16var onAdLoaded: ((Result<Void, Error>) -> Void)? = nil
17var onAdFailed: ((Result<NativeError, Error>) -> Void)? = nil
18var onAdClicked: ((Result<Void, Error>) -> Void)? = nil
19var onAdShown: ((Result<Void, Error>) -> Void)? = nil
20var onLeftApplication: ((Result<Void, Error>) -> Void)? = nil
21var onReturned: ((Result<Void, Error>) -> Void)? = nil
22var onImpression: ((Result<NativeImpression, Error>) -> Void)? = nil
23}
24
25class YandexAdsNativeComponent: NSObject, YandexAdsNative {
26var banners: [String: NativeData] = [:]
27let loader = YMANativeAdLoader()
28
29func make(id: String) throws {
30banners[id] = NativeData(
31view: YMANativeBannerView(),
32delegate: YandexAdsNativeLoadedDelegate(
33id: id,
34nativeDelegate: YandexAdsNativeDelegate(
35id: id,
36component: self
37),
38component: self
39)
40)
41}
42
43func load(id: String, width: Int64, height: Int64) throws {
44if (banners[id]?.delegate != nil) {
45loader.delegate = banners[id]?.delegate
46}
47
48let configuration = YMAMutableNativeAdRequestConfiguration(adUnitID: id)
49
50if (width > 0 || height > 0 ) {
51configuration.parameters = [
52"preferable-height": "\(width)",
53"preferable-width": "\(height)",
54]
55}
56configuration.shouldLoadImagesAutomatically = true
57
58loader.loadAd(with: configuration)
59}
60
61func onAdLoaded(id: String, completion: @escaping (Result<Void, Error>) -> Void) {
62banners[id]?.onAdLoaded = completion
63}
64
65func onAdFailedToLoad(id: String, completion: @escaping (Result<NativeError, Error>) -> Void) {
66banners[id]?.onAdFailed = completion
67}
68
69func onAdClicked(id: String, completion: @escaping (Result<Void, Error>) -> Void) {
70banners[id]?.onAdClicked = completion
71}
72
73func onLeftApplication(id: String, completion: @escaping (Result<Void, Error>) -> Void) {
74banners[id]?.onLeftApplication = completion
75}
76
77func onReturnedToApplication(id: String, completion: @escaping (Result<Void, Error>) -> Void) {
78banners[id]?.onReturned = completion
79}
80
81func onImpression(id: String, completion: @escaping (Result<NativeImpression, Error>) -> Void) {
82banners[id]?.onImpression = completion
83}
84}
85
86extension YandexAdsNativeComponent: YMANativeAdLoaderDelegate {
87func nativeAdLoader(_ loader: YMANativeAdLoader, didLoad ad: YMANativeAd) {
88print(ad)
89}
90
91func nativeAdLoader(_ loader: YMANativeAdLoader, didFailLoadingWithError error: Error) {
92print(error)
93}
94}
95
96class YandexAdsNativeLoadedDelegate: NSObject, YMANativeAdLoaderDelegate {
97
98var id: String
99var nativeDelegate: YMANativeAdDelegate
100var component: YandexAdsNativeComponent
101
102init(id: String, nativeDelegate: YMANativeAdDelegate, component: YandexAdsNativeComponent) {
103self.id = id
104self.nativeDelegate = nativeDelegate
105self.component = component
106}
107
108func nativeAdLoader(_ loader: YMANativeAdLoader, didLoad ad: YMANativeAd) {
109ad.delegate = nativeDelegate
110
111component.banners[id]?.view?.ad = ad
112
113if let callback = component.banners[id]?.onAdLoaded {
114callback(Result.success(()))
115}
116}
117
118func nativeAdLoader(_ loader: YMANativeAdLoader, didFailLoadingWithError error: Error) {
119let response = NativeError(
120code: Int64(error._code),
121description: error.localizedDescription)
122
123if let callback = component.banners[id]?.onAdFailed {
124callback(Result.success(response))
125}
126}
127}
128
129class YandexAdsNativeDelegate: NSObject, YMANativeAdDelegate {
130var id: String
131var component: YandexAdsNativeComponent
132
133init(id: String, component: YandexAdsNativeComponent) {
134self.id = id
135self.component = component
136}
137
138func nativeAd(_ ad: YMANativeAd, didDismissScreen viewController: UIViewController?) {
139print("Native ad did dismiss")
140}
141
142func nativeAd(_ ad: YMANativeAd, willPresentScreen viewController: UIViewController?) {
143if let callback = component.banners[id]?.onReturned {
144callback(Result.success(()))
145}
146}
147
148func nativeAdWillLeaveApplication(_ ad: YMANativeAd) {
149if let callback = component.banners[id]?.onLeftApplication {
150callback(Result.success(()))
151}
152}
153
154func nativeAdDidClick(_ ad: YMANativeAd) {
155if let callback = component.banners[id]?.onAdClicked {
156callback(Result.success(()))
157}
158}
159
160func nativeAd(_ ad: YMANativeAd, didTrackImpressionWith impressionData: YMAImpressionData?) {
161let response = NativeImpression(data: impressionData?.rawData ?? "")
162
163if let callback = component.banners[id]?.onImpression {
164callback(Result.success(response))
165}
166}
167}
168