@@ -16,7 +16,7 @@ class InReadDirectTableViewController: TeadsViewController {
16
16
let teadsAdCellIndentifier = " TeadsAdCell "
17
17
let fakeArticleCell = " fakeArticleCell "
18
18
var adPosition : [ ( UUID , Int ) ] = [ ]
19
- static let startPosition = 3
19
+ static let incrementPosition = 3
20
20
var adRequestedIndices = Set < Int > ( )
21
21
22
22
var placement : TeadsInReadAdPlacement ?
@@ -31,12 +31,12 @@ class InReadDirectTableViewController: TeadsViewController {
31
31
32
32
func trackerViewRowNumber( requestIdentifier: UUID ? ) -> Int {
33
33
guard let requestIdentifier else {
34
- return InReadDirectTableViewController . startPosition
34
+ return InReadDirectTableViewController . incrementPosition
35
35
}
36
36
guard let position = adPosition. first ( where: { uuid, _ in
37
37
uuid == requestIdentifier
38
38
} ) else {
39
- let newPosition = ( adPosition. last? . 1 ?? 0 ) + InReadDirectTableViewController. startPosition
39
+ let newPosition = ( adPosition. last? . 1 ?? 0 ) + InReadDirectTableViewController. incrementPosition
40
40
adPosition. append ( ( requestIdentifier, newPosition) )
41
41
return newPosition
42
42
}
@@ -78,12 +78,11 @@ extension InReadDirectTableViewController: UITableViewDelegate, UITableViewDataS
78
78
}
79
79
80
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
- }
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
+ } )
87
86
}
88
87
}
89
88
@@ -121,14 +120,10 @@ extension InReadDirectTableViewController: TeadsInReadAdPlacementDelegate {
121
120
func didReceiveAd( ad: TeadsInReadAd , adRatio _: TeadsAdRatio ) {
122
121
let adRowIndex = adRowNumber ( requestIdentifier: ad. requestIdentifier)
123
122
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
- }
123
+ elements. insert ( . ad( ad) , at: adRowIndex)
124
+ ad. delegate = self
125
+ let indexPaths = [ IndexPath ( row: adRowIndex, section: 0 ) ]
126
+ tableView. insertRows ( at: indexPaths, with: . automatic)
132
127
}
133
128
134
129
func didFailToReceiveAd( reason: AdFailReason ) {
@@ -143,14 +138,10 @@ extension InReadDirectTableViewController: TeadsInReadAdPlacementDelegate {
143
138
144
139
func adOpportunityTrackerView( trackerView: TeadsAdOpportunityTrackerView ) {
145
140
let trackerRowIndex = trackerViewRowNumber ( requestIdentifier: trackerView. requestIdentifier)
146
- if trackerRowIndex <= elements. count {
147
- elements. insert ( . trackerView( trackerView) , at: trackerRowIndex)
141
+ elements. insert ( . trackerView( trackerView) , at: trackerRowIndex)
148
142
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
- }
143
+ let indexPaths = [ IndexPath ( row: trackerRowIndex, section: 0 ) ]
144
+ tableView. insertRows ( at: indexPaths, with: . automatic)
154
145
}
155
146
}
156
147
0 commit comments