@@ -17,16 +17,17 @@ class InReadDirectCollectionViewController: TeadsViewController {
1717 let teadsAdCellIndentifier = " TeadsAdCell "
1818 let fakeArticleCell = " fakeArticleCell "
1919 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
2620 var placement : TeadsInReadAdPlacement ?
21+
22+ private var elements = [ TeadsInReadAd? ] ( )
2723
2824 override func viewDidLoad( ) {
2925 super. viewDidLoad ( )
26+
27+ ( 0 ..< 8 ) . forEach { _ in
28+ elements. append ( nil )
29+ }
30+
3031 let placementSettings = TeadsAdPlacementSettings { ( settings) in
3132 settings. enableDebug ( )
3233 }
@@ -35,86 +36,64 @@ class InReadDirectCollectionViewController: TeadsViewController {
3536 placement? . requestAd ( requestSettings: TeadsAdRequestSettings { settings in
3637 settings. pageUrl ( " https://www.teads.tv " )
3738 } )
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 )
5439 }
5540
5641 @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 ) )
5945 }
46+ collectionView. reloadItems ( at: indexPaths)
6047 collectionView. collectionViewLayout. invalidateLayout ( )
6148 }
6249
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 ( )
7153 }
7254
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+ }
7660 }
7761
7862}
7963
8064extension InReadDirectCollectionViewController : UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout {
8165 func collectionView( _ collectionView: UICollectionView , numberOfItemsInSection section: Int ) -> Int {
82- return 4
66+ return elements . count
8367 }
8468
8569 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] {
9473 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 )
9977 return cellAd
100- default :
78+ } else {
10179 let cell = collectionView. dequeueReusableCell ( withReuseIdentifier: fakeArticleCell, for: indexPath)
10280 return cell
10381 }
10482 }
10583
10684 func collectionView( _ collectionView: UICollectionView , layout collectionViewLayout: UICollectionViewLayout , sizeForItemAt indexPath: IndexPath ) -> CGSize {
107- switch indexPath. item {
108- case 0 :
85+ if indexPath. item == 0 {
10986 let cell = collectionView. cellForItem ( at: indexPath)
11087 guard let bounds = cell? . bounds else {
11188 return CGSize . zero
11289 }
11390 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 )
11897 }
11998 }
12099}
@@ -134,36 +113,34 @@ extension InReadDirectCollectionViewController: TeadsAdDelegate {
134113 }
135114
136115 func didCatchError( ad: TeadsAd , error: Error ) {
137- closeSlot ( )
116+ closeSlot ( ad : ad )
138117 }
139118
140119 func didCloseAd( ad: TeadsAd ) {
141- closeSlot ( )
120+ closeSlot ( ad : ad )
142121 }
143122
144123}
145124
146125extension InReadDirectCollectionViewController : TeadsInReadAdPlacementDelegate {
147126
148127 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 ( )
150132 ad. delegate = self
151- let creativeRatio = adRatio
152- self . adRatio = creativeRatio
153- resizeTeadsAd ( adRatio: creativeRatio)
154133 }
155134
156135 func didFailToReceiveAd( reason: AdFailReason ) {
157- closeSlot ( )
136+ print ( " didFailToReceiveAd: \( reason . description ) " )
158137 }
159138
160139 func didUpdateRatio( ad: TeadsInReadAd , adRatio: TeadsAdRatio ) {
161- updateAdCellHeight ( )
162- self . adRatio = adRatio
140+ updateAdSize ( ad: ad)
163141 }
164142
165143 func adOpportunityTrackerView( trackerView: TeadsAdOpportunityTrackerView ) {
166- teadsAdView ? . addSubview ( trackerView )
144+ //not relevant in tableView integration
167145 }
168-
169146}
0 commit comments