Skip to content

Instantly share code, notes, and snippets.

@erkekin
Created February 24, 2017 06:25
Show Gist options
  • Save erkekin/8fe9f1628904e66cec91832ef3e4b669 to your computer and use it in GitHub Desktop.
Save erkekin/8fe9f1628904e66cec91832ef3e4b669 to your computer and use it in GitHub Desktop.
A pagination struct
protocol Pageable{
var loading:Bool{get}
var paginator:Paginator{get set}
func fetchWith(paginator: Paginator)
}
struct Paginator {
var page = 0
var offset = 0
let count = 10
mutating func increment(){
page += 1
offset = count * page
}
mutating func reset(){
page = 0
offset = 0
}
var metadata:[String:Any]{
return ["offset":offset, "count":count]
}
var urlComponent :URLComponents {
var components = Settings.hosturl
components.queryItems = []
let queryItemOffset = URLQueryItem(name: "offset", value: "\(offset)")
let queryItemCount = URLQueryItem(name: "count", value: "\(count)")
components.queryItems?.append(contentsOf:[queryItemOffset, queryItemCount])
return components
}
}
extension Paginator:CustomStringConvertible{
public var description: String {
return "\(page) - \(offset) - \(count)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment