Skip to content

Commit 9c21d14

Browse files
fix variable naming, unneeded conditions and unneeded requesting of ads
1 parent c259f0d commit 9c21d14

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

TeadsSampleApp/Controllers/InRead/Direct/TableView/InReadDirectTableViewController.swift

+15-24
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class InReadDirectTableViewController: TeadsViewController {
1616
let teadsAdCellIndentifier = "TeadsAdCell"
1717
let fakeArticleCell = "fakeArticleCell"
1818
var adPosition: [(UUID, Int)] = []
19-
static let startPosition = 3
19+
static let incrementPosition = 3
2020
var adRequestedIndices = Set<Int>()
2121

2222
var placement: TeadsInReadAdPlacement?
@@ -31,12 +31,12 @@ class InReadDirectTableViewController: TeadsViewController {
3131

3232
func trackerViewRowNumber(requestIdentifier: UUID?) -> Int {
3333
guard let requestIdentifier else {
34-
return InReadDirectTableViewController.startPosition
34+
return InReadDirectTableViewController.incrementPosition
3535
}
3636
guard let position = adPosition.first(where: { uuid, _ in
3737
uuid == requestIdentifier
3838
}) else {
39-
let newPosition = (adPosition.last?.1 ?? 0) + InReadDirectTableViewController.startPosition
39+
let newPosition = (adPosition.last?.1 ?? 0) + InReadDirectTableViewController.incrementPosition
4040
adPosition.append((requestIdentifier, newPosition))
4141
return newPosition
4242
}
@@ -78,12 +78,11 @@ extension InReadDirectTableViewController: UITableViewDelegate, UITableViewDataS
7878
}
7979

8080
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+
})
8786
}
8887
}
8988

@@ -121,14 +120,10 @@ extension InReadDirectTableViewController: TeadsInReadAdPlacementDelegate {
121120
func didReceiveAd(ad: TeadsInReadAd, adRatio _: TeadsAdRatio) {
122121
let adRowIndex = adRowNumber(requestIdentifier: ad.requestIdentifier)
123122

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)
132127
}
133128

134129
func didFailToReceiveAd(reason: AdFailReason) {
@@ -143,14 +138,10 @@ extension InReadDirectTableViewController: TeadsInReadAdPlacementDelegate {
143138

144139
func adOpportunityTrackerView(trackerView: TeadsAdOpportunityTrackerView) {
145140
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)
148142

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)
154145
}
155146
}
156147

0 commit comments

Comments
 (0)