@@ -15,10 +15,9 @@ class InReadDirectTableViewController: TeadsViewController {
1515 let contentCell = " TeadsContentCell "
1616 let teadsAdCellIndentifier = " TeadsAdCell "
1717 let fakeArticleCell = " fakeArticleCell "
18- let trackerViewRowNumber = 3 // tracker view needs to be placed above the slot view
19- var adRowNumber : Int {
20- return trackerViewRowNumber + 1
21- }
18+ var adPosition : [ ( UUID , Int ) ] = [ ]
19+ static let incrementPosition = 3
20+ var adRequestedIndices = Set < Int > ( )
2221
2322 var placement : TeadsInReadAdPlacement ?
2423
@@ -30,6 +29,24 @@ class InReadDirectTableViewController: TeadsViewController {
3029
3130 private var elements = [ TeadsElement] ( )
3231
32+ func trackerViewRowNumber( requestIdentifier: UUID ? ) -> Int {
33+ guard let requestIdentifier else {
34+ return InReadDirectTableViewController . incrementPosition
35+ }
36+ guard let position = adPosition. first ( where: { uuid, _ in
37+ uuid == requestIdentifier
38+ } ) else {
39+ let newPosition = ( adPosition. last? . 1 ?? 0 ) + InReadDirectTableViewController. incrementPosition
40+ adPosition. append ( ( requestIdentifier, newPosition) )
41+ return newPosition
42+ }
43+ return position. 1
44+ }
45+
46+ func adRowNumber( requestIdentifier: UUID ? ) -> Int {
47+ return trackerViewRowNumber ( requestIdentifier: requestIdentifier) + 1
48+ }
49+
3350 override func viewDidLoad( ) {
3451 super. viewDidLoad ( )
3552
@@ -43,10 +60,6 @@ class InReadDirectTableViewController: TeadsViewController {
4360
4461 // keep a strong reference to placement instance
4562 placement = Teads . createInReadPlacement ( pid: Int ( pid) ?? 0 , settings: placementSettings, delegate: self )
46- placement? . requestAd ( requestSettings: TeadsAdRequestSettings { settings in
47- settings. pageUrl ( " https://www.teads.com " )
48- } )
49-
5063 tableView. register ( AdOpportunityTrackerTableViewCell . self, forCellReuseIdentifier: AdOpportunityTrackerTableViewCell . identifier)
5164 }
5265
@@ -57,17 +70,22 @@ class InReadDirectTableViewController: TeadsViewController {
5770 elements. removeAll { $0 == . ad( inReadAd) }
5871 tableView. reloadData ( )
5972 }
60-
61- func updateAdCellHeight( ) {
62- tableView. reloadRows ( at: [ IndexPath ( row: adRowNumber, section: 0 ) ] , with: . automatic)
63- }
6473}
6574
6675extension InReadDirectTableViewController : UITableViewDelegate , UITableViewDataSource {
6776 func tableView( _: UITableView , numberOfRowsInSection _: Int ) -> Int {
6877 return elements. count
6978 }
7079
80+ func tableView( _: UITableView , willDisplay _: UITableViewCell , forRowAt indexPath: IndexPath ) {
81+ if indexPath. row % InReadDirectTableViewController. incrementPosition == 0 , elements [ indexPath. row] == . article, !adRequestedIndices. contains ( indexPath. row) {
82+ adRequestedIndices. insert ( indexPath. row)
83+ placement? . requestAd ( requestSettings: TeadsAdRequestSettings { settings in
84+ settings. pageUrl ( " https://www.teads.com " )
85+ } )
86+ }
87+ }
88+
7189 func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
7290 if indexPath. row == 0 {
7391 return tableView. dequeueReusableCell ( withIdentifier: contentCell, for: indexPath)
@@ -100,9 +118,11 @@ extension InReadDirectTableViewController: UITableViewDelegate, UITableViewDataS
100118
101119extension InReadDirectTableViewController : TeadsInReadAdPlacementDelegate {
102120 func didReceiveAd( ad: TeadsInReadAd , adRatio _: TeadsAdRatio ) {
103- elements. insert ( . ad( ad) , at: adRowNumber)
121+ let adRowIndex = adRowNumber ( requestIdentifier: ad. requestIdentifier)
122+
123+ elements. insert ( . ad( ad) , at: adRowIndex)
104124 ad. delegate = self
105- let indexPaths = [ IndexPath ( row: adRowNumber , section: 0 ) ]
125+ let indexPaths = [ IndexPath ( row: adRowIndex , section: 0 ) ]
106126 tableView. insertRows ( at: indexPaths, with: . automatic)
107127 }
108128
@@ -117,8 +137,10 @@ extension InReadDirectTableViewController: TeadsInReadAdPlacementDelegate {
117137 }
118138
119139 func adOpportunityTrackerView( trackerView: TeadsAdOpportunityTrackerView ) {
120- elements. insert ( . trackerView( trackerView) , at: trackerViewRowNumber)
121- let indexPaths = [ IndexPath ( row: trackerViewRowNumber, section: 0 ) ]
140+ let trackerRowIndex = trackerViewRowNumber ( requestIdentifier: trackerView. requestIdentifier)
141+ elements. insert ( . trackerView( trackerView) , at: trackerRowIndex)
142+
143+ let indexPaths = [ IndexPath ( row: trackerRowIndex, section: 0 ) ]
122144 tableView. insertRows ( at: indexPaths, with: . automatic)
123145 }
124146}
0 commit comments