Created
February 24, 2017 06:25
-
-
Save erkekin/8fe9f1628904e66cec91832ef3e4b669 to your computer and use it in GitHub Desktop.
A pagination struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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