@@ -17,16 +17,17 @@ class InReadDirectCollectionViewController: TeadsViewController {
17
17
let teadsAdCellIndentifier = " TeadsAdCell "
18
18
let fakeArticleCell = " fakeArticleCell "
19
19
let adItemNumber = 2
20
- var adHeight : CGFloat ?
21
- var adRatio : TeadsAdRatio ?
22
- var teadsAdIsLoaded = false
23
- var teadsAdView : TeadsInReadAdView ?
24
- var collectionViewAdCellWidth : CGFloat !
25
- var fakeArticleCellHeight : CGFloat = 300
26
20
var placement : TeadsInReadAdPlacement ?
21
+
22
+ private var elements = [ TeadsInReadAd? ] ( )
27
23
28
24
override func viewDidLoad( ) {
29
25
super. viewDidLoad ( )
26
+
27
+ ( 0 ..< 8 ) . forEach { _ in
28
+ elements. append ( nil )
29
+ }
30
+
30
31
let placementSettings = TeadsAdPlacementSettings { ( settings) in
31
32
settings. enableDebug ( )
32
33
}
@@ -35,86 +36,64 @@ class InReadDirectCollectionViewController: TeadsViewController {
35
36
placement? . requestAd ( requestSettings: TeadsAdRequestSettings { settings in
36
37
settings. pageUrl ( " https://www.teads.tv " )
37
38
} )
38
-
39
- teadsAdView = TeadsInReadAdView ( )
40
-
41
- // We use an observer to know when a rotation happened, to resize the ad
42
- // You can use whatever way you want to do so
43
- NotificationCenter . default. addObserver ( self , selector: #selector( rotationDetected) , name: UIDevice . orientationDidChangeNotification, object: nil )
44
-
45
- }
46
-
47
- override func viewDidLayoutSubviews( ) {
48
- super. viewDidLayoutSubviews ( )
49
- collectionViewAdCellWidth = collectionView. frame. width - 20
50
- }
51
-
52
- deinit {
53
- NotificationCenter . default. removeObserver ( self )
54
39
}
55
40
56
41
@objc func rotationDetected( ) {
57
- if let adRatio = self . adRatio {
58
- resizeTeadsAd ( adRatio: adRatio)
42
+ var indexPaths = [ IndexPath] ( )
43
+ for index in 0 ..< elements. count {
44
+ indexPaths. append ( IndexPath ( row: index, section: 0 ) )
59
45
}
46
+ collectionView. reloadItems ( at: indexPaths)
60
47
collectionView. collectionViewLayout. invalidateLayout ( )
61
48
}
62
49
63
- func resizeTeadsAd( adRatio: TeadsAdRatio ) {
64
- adHeight = adRatio. calculateHeight ( for: collectionViewAdCellWidth)
65
- updateAdCellHeight ( )
66
- }
67
-
68
- func closeSlot( ) {
69
- adHeight = 0
70
- updateAdCellHeight ( )
50
+ func closeSlot( ad: TeadsAd ) {
51
+ elements. removeAll { $0 == ad }
52
+ collectionView. reloadData ( )
71
53
}
72
54
73
- func updateAdCellHeight( ) {
74
- collectionView. reloadItems ( at: [ IndexPath ( row: adItemNumber, section: 0 ) ] )
75
- collectionView. collectionViewLayout. invalidateLayout ( )
55
+ func updateAdSize( ad: TeadsInReadAd ) {
56
+ if let row = elements. firstIndex ( of: ad) {
57
+ collectionView. reloadItems ( at: [ IndexPath ( row: row, section: 0 ) ] )
58
+ collectionView. collectionViewLayout. invalidateLayout ( )
59
+ }
76
60
}
77
61
78
62
}
79
63
80
64
extension InReadDirectCollectionViewController : UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout {
81
65
func collectionView( _ collectionView: UICollectionView , numberOfItemsInSection section: Int ) -> Int {
82
- return 4
66
+ return elements . count
83
67
}
84
68
85
69
func collectionView( _ collectionView: UICollectionView , cellForItemAt indexPath: IndexPath ) -> UICollectionViewCell {
86
- switch indexPath. item {
87
- case 0 :
88
- let cell = collectionView. dequeueReusableCell ( withReuseIdentifier: contentCell, for: indexPath)
89
- cell. contentView. translatesAutoresizingMaskIntoConstraints = false
90
- cell. contentView. widthAnchor. constraint ( equalToConstant: collectionView. bounds. width) . isActive = true
91
- return cell
92
- case adItemNumber:
93
- //need to create a cell and just add a teadsAd to it, so we have only one teads ad
70
+ if indexPath. item == 0 {
71
+ return collectionView. dequeueReusableCell ( withReuseIdentifier: contentCell, for: indexPath)
72
+ } else if let ad = elements [ indexPath. row] {
94
73
let cellAd = collectionView. dequeueReusableCell ( withReuseIdentifier: teadsAdCellIndentifier, for: indexPath)
95
- if let teadsAdView = teadsAdView {
96
- cellAd. addSubview ( teadsAdView)
97
- teadsAdView. frame = CGRect ( x: 0 , y: 0 , width: collectionViewAdCellWidth, height: adHeight ?? 250 )
98
- }
74
+ let teadsAdView = TeadsInReadAdView ( bind: ad)
75
+ cellAd. contentView. addSubview ( teadsAdView)
76
+ teadsAdView. setupConstraintsToFitSuperView ( horizontalMargin: 10 )
99
77
return cellAd
100
- default :
78
+ } else {
101
79
let cell = collectionView. dequeueReusableCell ( withReuseIdentifier: fakeArticleCell, for: indexPath)
102
80
return cell
103
81
}
104
82
}
105
83
106
84
func collectionView( _ collectionView: UICollectionView , layout collectionViewLayout: UICollectionViewLayout , sizeForItemAt indexPath: IndexPath ) -> CGSize {
107
- switch indexPath. item {
108
- case 0 :
85
+ if indexPath. item == 0 {
109
86
let cell = collectionView. cellForItem ( at: indexPath)
110
87
guard let bounds = cell? . bounds else {
111
88
return CGSize . zero
112
89
}
113
90
return CGSize ( width: collectionView. bounds. width, height: bounds. height)
114
- case adItemNumber:
115
- return CGSize ( width: collectionViewAdCellWidth, height: adHeight ?? 0 )
116
- default :
117
- return CGSize ( width: collectionView. bounds. width, height: fakeArticleCellHeight)
91
+ } else if let ad = elements [ indexPath. row] {
92
+ let width = collectionView. frame. width - 20
93
+ let height = ad. adRatio. calculateHeight ( for: width)
94
+ return . init( width: width, height: height)
95
+ } else {
96
+ return CGSize ( width: collectionView. bounds. width, height: 300 )
118
97
}
119
98
}
120
99
}
@@ -134,36 +113,34 @@ extension InReadDirectCollectionViewController: TeadsAdDelegate {
134
113
}
135
114
136
115
func didCatchError( ad: TeadsAd , error: Error ) {
137
- closeSlot ( )
116
+ closeSlot ( ad : ad )
138
117
}
139
118
140
119
func didCloseAd( ad: TeadsAd ) {
141
- closeSlot ( )
120
+ closeSlot ( ad : ad )
142
121
}
143
122
144
123
}
145
124
146
125
extension InReadDirectCollectionViewController : TeadsInReadAdPlacementDelegate {
147
126
148
127
func didReceiveAd( ad: TeadsInReadAd , adRatio: TeadsAdRatio ) {
149
- teadsAdView? . bind ( ad)
128
+ elements. insert ( ad, at: adItemNumber)
129
+ let indexPaths = [ IndexPath ( row: adItemNumber, section: 0 ) ]
130
+ collectionView. insertItems ( at: indexPaths)
131
+ collectionView. collectionViewLayout. invalidateLayout ( )
150
132
ad. delegate = self
151
- let creativeRatio = adRatio
152
- self . adRatio = creativeRatio
153
- resizeTeadsAd ( adRatio: creativeRatio)
154
133
}
155
134
156
135
func didFailToReceiveAd( reason: AdFailReason ) {
157
- closeSlot ( )
136
+ print ( " didFailToReceiveAd: \( reason . description ) " )
158
137
}
159
138
160
139
func didUpdateRatio( ad: TeadsInReadAd , adRatio: TeadsAdRatio ) {
161
- updateAdCellHeight ( )
162
- self . adRatio = adRatio
140
+ updateAdSize ( ad: ad)
163
141
}
164
142
165
143
func adOpportunityTrackerView( trackerView: TeadsAdOpportunityTrackerView ) {
166
- teadsAdView ? . addSubview ( trackerView )
144
+ //not relevant in tableView integration
167
145
}
168
-
169
146
}
0 commit comments