Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paginating the movie DB in swift

im having trouble setting up pagination in swift with the MovieDB API normally you would have a limit and an offet then that would relay to your model array .count -1 when working with CollectionViews Im working with a diffable datasource and cannot see the solution has anyone manage to implement this or something similar?

current API service looks like this

  class APIService {

      static let shared = APIService()

    //always pass in your first API so the one which holds title, release date ect
    func fetchMovies(completionHandler: @escaping ([Movie]?, Error?) -> ()) {

        guard let url = URL(string: APINOWPLAYING) else {
            print("not a valid url")
            return
        }
        let request = URLRequest(url: url)
        URLSession.shared.dataTask(with: request) { (data, response, error) in
            if let data = data {//when Decoding use the 2nd API model with the array 
                if let decodedResponse = try? JSONDecoder().decode(Movies.self, from: data) {

                    DispatchQueue.main.async {
                        completionHandler(decodedResponse.results, nil)
                        print("TOTAL RESULTS \(decodedResponse.page)")
                    }
                    return
                }
            }
            print("Fatch Failed \(error?.localizedDescription ?? "error unknown")")
        }.resume()

    }

view controller

  private func setupDiffableDataSource() {
        collectionView.dataSource = diffDataSource

        //MARK:- SetupHeader under Compositional Sections Extension
        setupHeader()

        APIService.shared.fetchMovies { (movies, err) in

            APIService.shared.fetchTopMovies { (movieGroup, err) in

                var snapshot = self.diffDataSource.snapshot()
                snapshot.appendSections([.topSection])
                snapshot.appendItems(movies ?? [], toSection: .topSection)

                snapshot.appendSections([.bottomSection])
                let objects = movieGroup?.results ?? []
                snapshot.appendItems(objects, toSection: .bottomSection)

                self.diffDataSource.apply(snapshot)
            }
        }
    }

does anyone know how to work with API for pagination?

this is what the MOVIEDB api call looks like

let APINOWPLAYING = "https://api.themoviedb.org/3/movie/now_playing?api_key=(APIKEY)&language=en-US&page=1&total_pages=56"

hoping someone can point me in the right direction

thanks

like image 316
Legend_ 33 Avatar asked Mar 03 '23 14:03

Legend_ 33


1 Answers

You can use func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) from UICollectionViewDelegate

You need to update your service so it can handle the page parameter

var isCanLoadMore = false
var currentPage = 1

private func fetchData(page: Int) {
    // your API request
    // remember to change isCanLoadMore = true after apply(snapshot)
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    
    if isCanLoadMore {
        if diffableDataSource.snapshot().numberOfSections - 1 == indexPath.section {
            let currentSection = diffableDataSource.snapshot().sectionIdentifiers[indexPath.section]
            if diffableDataSource.snapshot().numberOfItems(inSection: currentSection) - 1 == indexPath.row {
                isCanLoadMore = false
                currentPage += 1
                print("NEXT PAGE")
                fetchData(page: currentPage)
            }
        }
    }
}
like image 99
aiwiguna Avatar answered Mar 05 '23 16:03

aiwiguna