Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languageswift
import UIKit
import R89SDK

class CollectionViewInfiniteScrollViewControllerViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
 
    var collectionView: UICollectionView!
    ...
    override func viewDidLoad() {
        super.viewDidLoad()
        // Configure the collection view layout
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
        // Configure the collection view
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(DynamicHeightCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        // Add constraints to fill the view
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(collectionView)
        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
        ])
    }
    ...
}

...

Code Block
languageswift
import UIKit
import R89SDK

class CollectionViewInfiniteScrollViewControllerViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
 
    var collectionView: UICollectionView!
    ...
    override func viewDidLoad() {
        super.viewDidLoad()
        // Configure the collection view layout
        ...
        let infiniteScrollConfigId = ConfigBuilder.companion.INFINITE_SCROLL_TEST_R89_CONFIG_ID
        RefineryAdFactory.shared.createInfiniteScroll(
          configurationID: infiniteScrollConfigId,
          scrollView: self.collectionView,
          scrollItemAdWrapperTag: "infiniteScroll_ad_wrapper_tag",
          lifecycleCallbacks: nil)
    }
    ...
}

...

Note

Careful on the implementation ofgetPositionInScrollMethod()

This needs to be implemented according to your implementation of the infinite scroll.
If you have paginated content this position needs to account for that

EX: 20 items per page so first 20 items go from 0 to 19 and the first item of the second page is not 0 is 20

formula beeing -> pageItemIndex + (pageSize*currentPage)

so first item position of first page is 0 + (20*0)

item 0 plus 20 multiplied by the current page index, so page 1 has index 0

and first item position of the second page is 0 + (20*1)

item 0 plus 20 multiplied by the current page index, so page 2 has index 1

second item first page would be 1 + (20*0) = 1

second item second page would be 1 + (20*1) = 21

and so on

Lifecycle Events

There are two options available to track the advertisement events for the infinite scroll. You can subscribe to these events with the same method but passing a new object as a parameter. Details about this object can be found in the Reference.

Code Block
import UIKit
import R89SDK

// 1. Extend from the Banner InfiniteScrollEventListener
class InfiniteScrollLifecycleListener: InfiniteScrollEventListener {
    ...
    override func onAdItemLoaded(itemIdInData: Int32) {
        // Ad has been loaded for the position (itemIdInData)
    }
    ...
}

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
 
    var collectionView: UICollectionView!
    ...
    override func viewDidLoad() {
        super.viewDidLoad()
        // Configure the collection view layout
        ...
        let infiniteScrollConfigId = ConfigBuilder.companion.INFINITE_SCROLL_TEST_R89_CONFIG_ID
        RefineryAdFactory.shared.createInfiniteScroll(
          configurationID: infiniteScrollConfigId,
          scrollView: self.collectionView,
          scrollItemAdWrapperTag: "infiniteScroll_ad_wrapper_tag",
          // 
          lifecycleCallbacks: InfiniteScrollLifecycleListener())
    }
    ...
}