@@ -15,10 +15,9 @@ class InReadDirectTableViewController: TeadsViewController {
15
15
let contentCell = " TeadsContentCell "
16
16
let teadsAdCellIndentifier = " TeadsAdCell "
17
17
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 > ( )
22
21
23
22
var placement : TeadsInReadAdPlacement ?
24
23
@@ -30,6 +29,24 @@ class InReadDirectTableViewController: TeadsViewController {
30
29
31
30
private var elements = [ TeadsElement] ( )
32
31
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
+
33
50
override func viewDidLoad( ) {
34
51
super. viewDidLoad ( )
35
52
@@ -43,10 +60,6 @@ class InReadDirectTableViewController: TeadsViewController {
43
60
44
61
// keep a strong reference to placement instance
45
62
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
-
50
63
tableView. register ( AdOpportunityTrackerTableViewCell . self, forCellReuseIdentifier: AdOpportunityTrackerTableViewCell . identifier)
51
64
}
52
65
@@ -57,17 +70,22 @@ class InReadDirectTableViewController: TeadsViewController {
57
70
elements. removeAll { $0 == . ad( inReadAd) }
58
71
tableView. reloadData ( )
59
72
}
60
-
61
- func updateAdCellHeight( ) {
62
- tableView. reloadRows ( at: [ IndexPath ( row: adRowNumber, section: 0 ) ] , with: . automatic)
63
- }
64
73
}
65
74
66
75
extension InReadDirectTableViewController : UITableViewDelegate , UITableViewDataSource {
67
76
func tableView( _: UITableView , numberOfRowsInSection _: Int ) -> Int {
68
77
return elements. count
69
78
}
70
79
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
+
71
89
func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
72
90
if indexPath. row == 0 {
73
91
return tableView. dequeueReusableCell ( withIdentifier: contentCell, for: indexPath)
@@ -100,9 +118,11 @@ extension InReadDirectTableViewController: UITableViewDelegate, UITableViewDataS
100
118
101
119
extension InReadDirectTableViewController : TeadsInReadAdPlacementDelegate {
102
120
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)
104
124
ad. delegate = self
105
- let indexPaths = [ IndexPath ( row: adRowNumber , section: 0 ) ]
125
+ let indexPaths = [ IndexPath ( row: adRowIndex , section: 0 ) ]
106
126
tableView. insertRows ( at: indexPaths, with: . automatic)
107
127
}
108
128
@@ -117,8 +137,10 @@ extension InReadDirectTableViewController: TeadsInReadAdPlacementDelegate {
117
137
}
118
138
119
139
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 ) ]
122
144
tableView. insertRows ( at: indexPaths, with: . automatic)
123
145
}
124
146
}
0 commit comments