yandexads-flutter

Форк
0
/
YandexAdsNativeComponent.swift 
167 строк · 5.3 Кб
1
//
2
//  YandexAdsNativeComponent.swift
3
//  flutter_yandex_ads
4
//
5
//  Created by Artem Kovardin on 06.12.2022.
6
//
7

8
import Foundation
9
import YandexMobileAds
10
import Flutter
11

12

13
struct NativeData {
14
    var view: YMANativeBannerView? = nil
15
    var delegate: YandexAdsNativeLoadedDelegate? = nil
16
    var onAdLoaded: ((Result<Void, Error>) -> Void)? = nil
17
    var onAdFailed: ((Result<NativeError, Error>) -> Void)? = nil
18
    var onAdClicked: ((Result<Void, Error>) -> Void)? = nil
19
    var onAdShown: ((Result<Void, Error>) -> Void)? = nil
20
    var onLeftApplication: ((Result<Void, Error>) -> Void)? = nil
21
    var onReturned: ((Result<Void, Error>) -> Void)? = nil
22
    var onImpression: ((Result<NativeImpression, Error>) -> Void)? = nil
23
}
24

25
class YandexAdsNativeComponent: NSObject, YandexAdsNative {
26
    var banners: [String: NativeData] = [:]
27
    let loader = YMANativeAdLoader()
28
    
29
    func make(id: String) throws {
30
        banners[id] = NativeData(
31
            view: YMANativeBannerView(),
32
            delegate: YandexAdsNativeLoadedDelegate(
33
                id: id,
34
                nativeDelegate: YandexAdsNativeDelegate(
35
                    id: id,
36
                    component: self
37
                ),
38
                component: self
39
            )
40
        )
41
    }
42
    
43
    func load(id: String, width: Int64, height: Int64) throws {
44
        if (banners[id]?.delegate != nil) {
45
            loader.delegate = banners[id]?.delegate
46
        }
47
        
48
        let configuration = YMAMutableNativeAdRequestConfiguration(adUnitID: id)
49
        
50
        if (width > 0 || height > 0 ) {
51
            configuration.parameters = [
52
                "preferable-height": "\(width)",
53
                "preferable-width": "\(height)",
54
            ]
55
        }
56
        configuration.shouldLoadImagesAutomatically = true
57
        
58
        loader.loadAd(with: configuration)
59
    }
60
    
61
    func onAdLoaded(id: String, completion: @escaping (Result<Void, Error>) -> Void) {
62
        banners[id]?.onAdLoaded = completion
63
    }
64
    
65
    func onAdFailedToLoad(id: String, completion: @escaping (Result<NativeError, Error>) -> Void) {
66
        banners[id]?.onAdFailed = completion
67
    }
68
    
69
    func onAdClicked(id: String, completion: @escaping (Result<Void, Error>) -> Void) {
70
        banners[id]?.onAdClicked = completion
71
    }
72
    
73
    func onLeftApplication(id: String, completion: @escaping (Result<Void, Error>) -> Void) {
74
        banners[id]?.onLeftApplication = completion
75
    }
76
    
77
    func onReturnedToApplication(id: String, completion: @escaping (Result<Void, Error>) -> Void) {
78
        banners[id]?.onReturned = completion
79
    }
80
    
81
    func onImpression(id: String, completion: @escaping (Result<NativeImpression, Error>) -> Void) {
82
        banners[id]?.onImpression = completion
83
    }
84
}
85

86
extension YandexAdsNativeComponent: YMANativeAdLoaderDelegate {
87
    func nativeAdLoader(_ loader: YMANativeAdLoader, didLoad ad: YMANativeAd) {
88
        print(ad)
89
    }
90
    
91
    func nativeAdLoader(_ loader: YMANativeAdLoader, didFailLoadingWithError error: Error) {
92
        print(error)
93
    }
94
}
95

96
class YandexAdsNativeLoadedDelegate: NSObject, YMANativeAdLoaderDelegate {
97

98
    var id: String
99
    var nativeDelegate: YMANativeAdDelegate
100
    var component: YandexAdsNativeComponent
101
    
102
    init(id: String, nativeDelegate: YMANativeAdDelegate, component: YandexAdsNativeComponent) {
103
        self.id = id
104
        self.nativeDelegate = nativeDelegate
105
        self.component = component
106
    }
107
    
108
    func nativeAdLoader(_ loader: YMANativeAdLoader, didLoad ad: YMANativeAd) {
109
        ad.delegate = nativeDelegate
110
        
111
        component.banners[id]?.view?.ad = ad
112
        
113
        if let callback =  component.banners[id]?.onAdLoaded {
114
            callback(Result.success(()))
115
        }
116
    }
117
    
118
    func nativeAdLoader(_ loader: YMANativeAdLoader, didFailLoadingWithError error: Error) {
119
        let response = NativeError(
120
            code: Int64(error._code),
121
            description: error.localizedDescription)
122
        
123
        if let callback =  component.banners[id]?.onAdFailed {
124
            callback(Result.success(response))
125
        }
126
    }
127
}
128

129
class YandexAdsNativeDelegate: NSObject, YMANativeAdDelegate {
130
    var id: String
131
    var component: YandexAdsNativeComponent
132
    
133
    init(id: String, component: YandexAdsNativeComponent) {
134
        self.id = id
135
        self.component = component
136
    }
137
    
138
    func nativeAd(_ ad: YMANativeAd, didDismissScreen viewController: UIViewController?) {
139
        print("Native ad did dismiss")
140
    }
141
    
142
    func nativeAd(_ ad: YMANativeAd, willPresentScreen viewController: UIViewController?) {
143
        if let callback =  component.banners[id]?.onReturned {
144
            callback(Result.success(()))
145
        }
146
    }
147
    
148
    func nativeAdWillLeaveApplication(_ ad: YMANativeAd) {
149
        if let callback =  component.banners[id]?.onLeftApplication {
150
            callback(Result.success(()))
151
        }
152
    }
153
    
154
    func nativeAdDidClick(_ ad: YMANativeAd) {
155
        if let callback =  component.banners[id]?.onAdClicked {
156
            callback(Result.success(()))
157
        }
158
    }
159
    
160
    func nativeAd(_ ad: YMANativeAd, didTrackImpressionWith impressionData: YMAImpressionData?) {
161
        let response = NativeImpression(data: impressionData?.rawData ?? "")
162
        
163
        if let callback =  component.banners[id]?.onImpression {
164
            callback(Result.success(response))
165
        }
166
    }
167
}
168

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.