PagingDataController
PagingDataController is a library for implementing loading data by page (pagination) easily. It provides a short setup via few lines of code. You will not need to worry about page management, when to load the next page's data, and so on.
Requirements
- iOS 8.0+
- Xcode 8.3+
- Swift 3.1+
Installation
CocoaPods
pod 'PagingDataController'
Carthage
github "congncif/PagingDataController"
How it works?
Provider is a component which handles loading data by page. A list of objects or error will be returned once it is finished in processing.
DataSource keeps all of data which loaded by provider and arranges them in order of page.
A controller which implements PagingDataController
includes a provider and a dataSource itself. While dataSource
is injected automatically, provider
needs to be declared explicit.
Usage
- Create a new project.
- In
ViewController.swift
, add atableView
orcollectionView
to display list of items. - Create
Provider
:
struct GithubUsersProvider: PagingProviderProtocol {
// custom pageSize here
var pageSize: Int = 36
func loadData(parameters: AnyObject?, page: Int, completion: @escaping ([[String: AnyObject]], Error?) -> Void) {
let apiPath = "https://api.github.com/search/users?q=apple&page=\(page + 1)&per_page=\(pageSize)"
Alamofire.request(apiPath, method: .get).responseJSON { response in
var error: Error? = response.result.error
var result: [[String: AnyObject]] = []
defer {
completion(result, error)
}
guard let data = (response.result.value as? [String: AnyObject]) else {
return
}
result = data["items"] as! [[String: AnyObject]]
}
}
}
- In
ViewController.swift
, Implement the methods ofUITableViewDataSource
orUICollectionViewDataSource
to render data, make it conforms protocolPagingControllerProtocol
// 🚀 Just 4 steps to setup a Paging View Controller
class GithubUsersViewController: UIViewController, PagingControllerProtocol, PagingViewControllable {
@IBOutlet var tableView: UITableView!
// 1️⃣ [Step 1]: Provider definition.
lazy var provider = GithubUsersProvider()
// 2️⃣ [Step 2]: setup paging in single line of code.
/******************************************
Copy this method into your view controller
******************************************/
override func viewDidFinishInitialLayout() {
setupPagingController()
}
// 3️⃣ [Step 3]: Pass parameters for fetching data by page, default is nil.
func parametersForPage(_ page: Int) -> AnyObject? {
return nil
}
// 4️⃣ [Step 4]: Handle error when fetching failed.
func errorWarningForPage(_ page: Int, error: Error) {
showErrorAlert(error)
}
}
- Build & Run to enjoy
Author
NGUYEN CHI CONG, congnc.if@gmail.com
License
PagingDataController is available under the MIT license. See the LICENSE file for more info.