@@ -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 startPosition = 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 . startPosition
35
+ }
36
+ guard let position = adPosition. first ( where: { uuid, _ in
37
+ uuid == requestIdentifier
38
+ } ) else {
39
+ let newPosition = ( adPosition. last? . 1 ?? 0 ) + InReadDirectTableViewController. startPosition
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,23 @@ 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 % 3 == 0 {
82
+ if elements [ indexPath. row] == . article {
83
+ placement? . requestAd ( requestSettings: TeadsAdRequestSettings { settings in
84
+ settings. pageUrl ( " https://www.teads.com " )
85
+ } )
86
+ }
87
+ }
88
+ }
89
+
71
90
func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
72
91
if indexPath. row == 0 {
73
92
return tableView. dequeueReusableCell ( withIdentifier: contentCell, for: indexPath)
@@ -100,10 +119,16 @@ extension InReadDirectTableViewController: UITableViewDelegate, UITableViewDataS
100
119
101
120
extension InReadDirectTableViewController : TeadsInReadAdPlacementDelegate {
102
121
func didReceiveAd( ad: TeadsInReadAd , adRatio _: TeadsAdRatio ) {
103
- elements. insert ( . ad( ad) , at: adRowNumber)
104
- ad. delegate = self
105
- let indexPaths = [ IndexPath ( row: adRowNumber, section: 0 ) ]
106
- tableView. insertRows ( at: indexPaths, with: . automatic)
122
+ let adRowIndex = adRowNumber ( requestIdentifier: ad. requestIdentifier)
123
+
124
+ if adRowIndex <= elements. count {
125
+ elements. insert ( . ad( ad) , at: adRowIndex)
126
+ ad. delegate = self
127
+ let indexPaths = [ IndexPath ( row: adRowIndex, section: 0 ) ]
128
+ tableView. insertRows ( at: indexPaths, with: . automatic)
129
+ } else {
130
+ print ( " Invalid index for inserting ad: \( adRowIndex) , elements count: \( elements. count) " )
131
+ }
107
132
}
108
133
109
134
func didFailToReceiveAd( reason: AdFailReason ) {
@@ -117,9 +142,15 @@ extension InReadDirectTableViewController: TeadsInReadAdPlacementDelegate {
117
142
}
118
143
119
144
func adOpportunityTrackerView( trackerView: TeadsAdOpportunityTrackerView ) {
120
- elements. insert ( . trackerView( trackerView) , at: trackerViewRowNumber)
121
- let indexPaths = [ IndexPath ( row: trackerViewRowNumber, section: 0 ) ]
122
- tableView. insertRows ( at: indexPaths, with: . automatic)
145
+ let trackerRowIndex = trackerViewRowNumber ( requestIdentifier: trackerView. requestIdentifier)
146
+ if trackerRowIndex <= elements. count {
147
+ elements. insert ( . trackerView( trackerView) , at: trackerRowIndex)
148
+
149
+ let indexPaths = [ IndexPath ( row: trackerRowIndex, section: 0 ) ]
150
+ tableView. insertRows ( at: indexPaths, with: . automatic)
151
+ } else {
152
+ print ( " Invalid index for inserting trackerView: \( trackerRowIndex) , elements count: \( elements. count) " )
153
+ }
123
154
}
124
155
}
125
156
0 commit comments