Skip to content

Commit 89addf7

Browse files
authored
fit documentation to sample app (#158)
1 parent c9726d0 commit 89addf7

12 files changed

+120
-173
lines changed

TeadsSampleApp/Cells/AdmobNativeAdTableViewCell.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,5 @@ import UIKit
99
import GoogleMobileAds
1010

1111
class AdmobNativeAdTableViewCell: UITableViewCell {
12-
1312
@IBOutlet weak var nativeAdView: GADNativeAdView!
14-
1513
}

TeadsSampleApp/Controllers/InRead/Admob/ScrollView/InReadAdmobScrollViewController.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class InReadAdmobScrollViewController: TeadsViewController {
3333
bannerView.delegate = self
3434

3535
// 3. Load a new ad (this will call AdMob and Teads afterward)
36-
let request = GADRequest()
3736
let adSettings = TeadsAdapterSettings { (settings) in
3837
settings.enableDebug()
3938
settings.disableLocation()
@@ -46,10 +45,9 @@ class InReadAdmobScrollViewController: TeadsViewController {
4645
//settings.pageUrl("http://page.com/article1")
4746
}
4847

49-
let extras = try? adSettings.toDictionary()
50-
let customEventExtras = GADCustomEventExtras()
51-
customEventExtras.setExtras(extras, forLabel: "Teads")
52-
48+
let customEventExtras = GADMAdapterTeads.customEventExtra(with: adSettings)
49+
50+
let request = GADRequest()
5351
request.register(customEventExtras)
5452

5553
bannerView.load(request)

TeadsSampleApp/Controllers/InRead/Admob/TableView/InReadAdmobTableViewController.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import UIKit
1010
import GoogleMobileAds
1111
import TeadsSDK
12+
import TeadsAdMobAdapter
1213

1314
class InReadAdmobTableViewController: TeadsViewController {
1415

@@ -39,7 +40,6 @@ class InReadAdmobTableViewController: TeadsViewController {
3940
admobAdView?.delegate = self
4041

4142
// 3. Load a new ad (this will call AdMob and Teads afterward)
42-
let request = GADRequest()
4343
let adSettings = TeadsAdapterSettings { (settings) in
4444
settings.enableDebug()
4545
settings.disableLocation()
@@ -54,10 +54,9 @@ class InReadAdmobTableViewController: TeadsViewController {
5454
//settings.pageUrl("http://page.com/article1")
5555
}
5656

57-
let extras = try? adSettings.toDictionary()
58-
let customEventExtras = GADCustomEventExtras()
59-
customEventExtras.setExtras(extras, forLabel: "Teads")
60-
57+
let customEventExtras = GADMAdapterTeads.customEventExtra(with: adSettings)
58+
59+
let request = GADRequest()
6160
request.register(customEventExtras)
6261

6362
admobAdView?.load(request)

TeadsSampleApp/Controllers/InRead/Admob/WebView/InReadAdmobWebViewController.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import UIKit
1010
import WebKit
1111
import GoogleMobileAds
1212
import TeadsSDK
13+
import TeadsAdMobAdapter
1314

1415
class InReadAdmobWebViewController: TeadsViewController {
1516

@@ -39,7 +40,6 @@ class InReadAdmobWebViewController: TeadsViewController {
3940
/// init helper
4041
webViewHelper = TeadsWebViewHelper(webView: webView, selector: "#teads-placement-slot", delegate: self)
4142

42-
let request = GADRequest()
4343
let adSettings = TeadsAdapterSettings { (settings) in
4444
settings.enableDebug()
4545
settings.disableLocation()
@@ -53,10 +53,9 @@ class InReadAdmobWebViewController: TeadsViewController {
5353
//settings.pageUrl("http://page.com/article1")
5454
}
5555

56-
let extras = try? adSettings.toDictionary()
57-
let customEventExtras = GADCustomEventExtras()
58-
customEventExtras.setExtras(extras, forLabel: "Teads")
59-
56+
let customEventExtras = GADMAdapterTeads.customEventExtra(with: adSettings)
57+
58+
let request = GADRequest()
6059
request.register(customEventExtras)
6160

6261
bannerView.load(request)

TeadsSampleApp/Controllers/InRead/Direct/CollectionView/InReadDirectCollectionViewController.swift

Lines changed: 43 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ class InReadDirectCollectionViewController: TeadsViewController {
1717
let teadsAdCellIndentifier = "TeadsAdCell"
1818
let fakeArticleCell = "fakeArticleCell"
1919
let adItemNumber = 2
20-
var adHeight: CGFloat?
21-
var adRatio: TeadsAdRatio?
22-
var teadsAdIsLoaded = false
23-
var teadsAdView: TeadsInReadAdView?
24-
var collectionViewAdCellWidth: CGFloat!
25-
var fakeArticleCellHeight: CGFloat = 300
2620
var placement: TeadsInReadAdPlacement?
21+
22+
private var elements = [TeadsInReadAd?]()
2723

2824
override func viewDidLoad() {
2925
super.viewDidLoad()
26+
27+
(0..<8).forEach { _ in
28+
elements.append(nil)
29+
}
30+
3031
let placementSettings = TeadsAdPlacementSettings { (settings) in
3132
settings.enableDebug()
3233
}
@@ -35,86 +36,64 @@ class InReadDirectCollectionViewController: TeadsViewController {
3536
placement?.requestAd(requestSettings: TeadsAdRequestSettings { settings in
3637
settings.pageUrl("https://www.teads.tv")
3738
})
38-
39-
teadsAdView = TeadsInReadAdView()
40-
41-
// We use an observer to know when a rotation happened, to resize the ad
42-
// You can use whatever way you want to do so
43-
NotificationCenter.default.addObserver(self, selector: #selector(rotationDetected), name: UIDevice.orientationDidChangeNotification, object: nil)
44-
45-
}
46-
47-
override func viewDidLayoutSubviews() {
48-
super.viewDidLayoutSubviews()
49-
collectionViewAdCellWidth = collectionView.frame.width - 20
50-
}
51-
52-
deinit {
53-
NotificationCenter.default.removeObserver(self)
5439
}
5540

5641
@objc func rotationDetected() {
57-
if let adRatio = self.adRatio {
58-
resizeTeadsAd(adRatio: adRatio)
42+
var indexPaths = [IndexPath]()
43+
for index in 0..<elements.count {
44+
indexPaths.append(IndexPath(row: index, section: 0))
5945
}
46+
collectionView.reloadItems(at: indexPaths)
6047
collectionView.collectionViewLayout.invalidateLayout()
6148
}
6249

63-
func resizeTeadsAd(adRatio: TeadsAdRatio) {
64-
adHeight = adRatio.calculateHeight(for: collectionViewAdCellWidth)
65-
updateAdCellHeight()
66-
}
67-
68-
func closeSlot() {
69-
adHeight = 0
70-
updateAdCellHeight()
50+
func closeSlot(ad: TeadsAd) {
51+
elements.removeAll { $0 == ad }
52+
collectionView.reloadData()
7153
}
7254

73-
func updateAdCellHeight() {
74-
collectionView.reloadItems(at: [IndexPath(row: adItemNumber, section: 0)])
75-
collectionView.collectionViewLayout.invalidateLayout()
55+
func updateAdSize(ad: TeadsInReadAd) {
56+
if let row = elements.firstIndex(of: ad) {
57+
collectionView.reloadItems(at: [IndexPath(row: row, section: 0)])
58+
collectionView.collectionViewLayout.invalidateLayout()
59+
}
7660
}
7761

7862
}
7963

8064
extension InReadDirectCollectionViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
8165
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
82-
return 4
66+
return elements.count
8367
}
8468

8569
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
86-
switch indexPath.item {
87-
case 0:
88-
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: contentCell, for: indexPath)
89-
cell.contentView.translatesAutoresizingMaskIntoConstraints = false
90-
cell.contentView.widthAnchor.constraint(equalToConstant: collectionView.bounds.width).isActive = true
91-
return cell
92-
case adItemNumber:
93-
//need to create a cell and just add a teadsAd to it, so we have only one teads ad
70+
if indexPath.item == 0 {
71+
return collectionView.dequeueReusableCell(withReuseIdentifier: contentCell, for: indexPath)
72+
} else if let ad = elements[indexPath.row] {
9473
let cellAd = collectionView.dequeueReusableCell(withReuseIdentifier: teadsAdCellIndentifier, for: indexPath)
95-
if let teadsAdView = teadsAdView {
96-
cellAd.addSubview(teadsAdView)
97-
teadsAdView.frame = CGRect(x: 0, y: 0, width: collectionViewAdCellWidth, height: adHeight ?? 250)
98-
}
74+
let teadsAdView = TeadsInReadAdView(bind: ad)
75+
cellAd.contentView.addSubview(teadsAdView)
76+
teadsAdView.setupConstraintsToFitSuperView(horizontalMargin: 10)
9977
return cellAd
100-
default:
78+
} else {
10179
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: fakeArticleCell, for: indexPath)
10280
return cell
10381
}
10482
}
10583

10684
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
107-
switch indexPath.item {
108-
case 0:
85+
if indexPath.item == 0 {
10986
let cell = collectionView.cellForItem(at: indexPath)
11087
guard let bounds = cell?.bounds else {
11188
return CGSize.zero
11289
}
11390
return CGSize(width: collectionView.bounds.width, height: bounds.height)
114-
case adItemNumber:
115-
return CGSize(width: collectionViewAdCellWidth, height: adHeight ?? 0)
116-
default:
117-
return CGSize(width: collectionView.bounds.width, height: fakeArticleCellHeight)
91+
} else if let ad = elements[indexPath.row] {
92+
let width = collectionView.frame.width - 20
93+
let height = ad.adRatio.calculateHeight(for: width)
94+
return .init(width: width, height: height)
95+
} else {
96+
return CGSize(width: collectionView.bounds.width, height: 300)
11897
}
11998
}
12099
}
@@ -134,36 +113,34 @@ extension InReadDirectCollectionViewController: TeadsAdDelegate {
134113
}
135114

136115
func didCatchError(ad: TeadsAd, error: Error) {
137-
closeSlot()
116+
closeSlot(ad: ad)
138117
}
139118

140119
func didCloseAd(ad: TeadsAd) {
141-
closeSlot()
120+
closeSlot(ad: ad)
142121
}
143122

144123
}
145124

146125
extension InReadDirectCollectionViewController: TeadsInReadAdPlacementDelegate {
147126

148127
func didReceiveAd(ad: TeadsInReadAd, adRatio: TeadsAdRatio) {
149-
teadsAdView?.bind(ad)
128+
elements.insert(ad, at: adItemNumber)
129+
let indexPaths = [IndexPath(row: adItemNumber, section: 0)]
130+
collectionView.insertItems(at: indexPaths)
131+
collectionView.collectionViewLayout.invalidateLayout()
150132
ad.delegate = self
151-
let creativeRatio = adRatio
152-
self.adRatio = creativeRatio
153-
resizeTeadsAd(adRatio: creativeRatio)
154133
}
155134

156135
func didFailToReceiveAd(reason: AdFailReason) {
157-
closeSlot()
136+
print("didFailToReceiveAd: \(reason.description)")
158137
}
159138

160139
func didUpdateRatio(ad: TeadsInReadAd, adRatio: TeadsAdRatio) {
161-
updateAdCellHeight()
162-
self.adRatio = adRatio
140+
updateAdSize(ad: ad)
163141
}
164142

165143
func adOpportunityTrackerView(trackerView: TeadsAdOpportunityTrackerView) {
166-
teadsAdView?.addSubview(trackerView)
144+
//not relevant in tableView integration
167145
}
168-
169146
}

0 commit comments

Comments
 (0)