Skip to content

An easy-to-use API for implementing infinite scrolling in your applications

License

Notifications You must be signed in to change notification settings

grighakobian/swift-pagination

Repository files navigation

Pagination

CI

A flexible and easy-to-use pagination framework inspired by Texture batch fetching API.

Overview

Pagination provides an easy-to-use API for implementing infinite scrolling in your applications. It allows seamless integration of pagination functionality in any scrollable view, whether it's a UITableView, UICollectionView, or any other scrollable container.

With Pagination, you can effortlessly manage pagination in your app by automatically detecting when a user has scrolled close to the end of the current content and triggering the fetching of the next page. The framework supports both vertical and horizontal scrolling and is designed to work seamlessly with various UI components.

Features

  • Easily integrates with UIScrollView, UITableView, and UICollectionView.
  • Supports both vertical and horizontal scroll directions.
  • Provides customizable prefetching distance to control when the next batch of data is fetched.
  • Objective-C Support: Fully compatible with Objective-C projects, making it easier to integrate into existing codebases.

Getting Started

Implementing infinite scrolling is straightforward, especially with vertical scrolling. Set up the delegate to handle requests for new page prefetching

collectionView.pagination.delegate = self

Implement the delegate method to fetch data for the new page

func pagination(_ pagination: Pagination, prefetchNextPageWith context: PaginationContext) {
    // Fetch the next page of data from your source
    fetchData(forPage: nextPage) { result in
        switch result {
        case .success(let data):
            // Append the new data and update UI.
            pagination.isEnabled = nextPage < data.totalPages
            context.finish(true)
        case .failure:
            // Failed to fetch data
            context.finish(false)
        }
    }
}

Important

It is essential to call context.finish(_:) once the data loading is complete to accurately update the pagination state.

To disable pagination, set the isEnabled property to false. This will stop pagination from monitoring the scrollable view

collectionView.pagination.isEnabled = false

For horizontal scrolling, configure pagination to handle horizontal scroll

collectionView.pagination.direction = .horizontal

To adjust the prefetching distance, set the leadingScreensForPrefetching property to your desired value. The default is 2 leading screens. Setting it to 0 will stop pagination from notifying you about new data prefetching

collectionView.pagination.leadingScreensForPrefetching = 3

Objective-C Integration

Note

Pagination is fully compatible with Objective-C projects. Simply import the module and use the provided APIs.

self.tableView.pagination.isEnabled = YES;
self.tableView.pagination.direction = PaginationDirectionVertical;
self.tableView.pagination.leadingScreensForPrefetching = 3;
self.tableView.pagination.delegate = self;

Examples

Check out the Example directory to see how to use Pagination in real-world scenarios.

Installation

You can add pagination to an Xcode project by adding it as a package dependency.

https://github.com/grighakobian/swift-pagination

If you want to use Pagination in a SwiftPM project, it's as simple as adding it to a dependencies clause in your Package.swift:

dependencies: [
  .package(url: "https://github.com/grighakobian/swift-pagination", from: "1.0.0")
]

License

Paginator is available under the MIT license. See the LICENSE file for more info.