Created
August 17, 2020 07:59
-
-
Save alexnikol/e0e3b703276a7f62a638504b28deb26a to your computer and use it in GitHub Desktop.
Generic way to solve pagination duplications
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
class Pagination<Element: Decodable>: Decodable { | |
var list: [Element] | |
var currentPage: Int | |
var totalPages: Int | |
enum ResultCodingKeys: String, CodingKey { | |
case list, currentPage, totalPages | |
} | |
required init(from decoder: Decoder) throws { | |
let objectsContainer = try decoder.container(keyedBy: ResultCodingKeys.self) | |
currentPage = try objectsContainer.decode(Int.self, forKey: .currentPage) | |
totalPages = try objectsContainer.decode(Int.self, forKey: .totalPages) | |
list = try objectsContainer.decode([Element].self, forKey: .list) | |
} | |
} | |
//Usage part! | |
var paginationPet: Pagination<Pet>? | |
var paginationUser: Pagination<User>? | |
var paginationVet: Pagination<Vet>? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment