Swift package module used to handle server-side pagination.
https://github.com/LooseMarek/Pagination
Both loadNextPage()
and setTotalPages(totalPages)
should be called before performing canLoadNextPage()
check. See: Usage for implementation.
You should call loadNextPage()
before fetch.
You can set setTotalPages(totalPages)
in the callback of the initial fetch call.
If the API don't provide totalPages
in the response but returns total results count and results per fetch, you will need to manually calculate this from those values e.g. totalPages = totalResults / resultsPerFetch
.
import Pagination
let pagination: PaginationProtocol = Pagination()
func fetch() {
pagination.loadNextPage()
yourApiService.fetch(for: pagination.page) { [weak self] (response, error) in
self?.pagination.finishedLoadingNextPage()
if let error = error {
// Handle error
} else if let response = response {
self?.pagination.setTotalPages(response.totalPages)
// Update list and UI
}
}
}
Which will:
loadNextPage()
Note: Need to be called before fetch. First call will set it to page number 1page
from the Pagination module in API fetch methodfinishedLoadingNextPage()
in a callback of the fetchsetTotalPages(response.totalPages)
if we have responsetableView cellForRowAt
do {
if (try pagination.canLoadNextPage(at: index, listCount: yourList.count)) {
fetch()
}
} catch {
// Handle error
}
Which will:
loadNextPage()
and setTotalPages()
weren't set before that check, it will throw an errorBy default Pagination module will allow for the next fetch, if the list will scroll to above 70% of it's content. You can change it to a different percentage by using
setPercentageScrollToLoadNextPage(_ percentageScroll: Double)
percentageScroll
value should be greater than 30% (0.3) and less than 100% (1.0). If you will try to set the value bellow or above allowed percentage it will set to 30% and 100% respectively.
Module is run with GitHub Actions everytime the new commit is pushed to the remote branch master
. The testing includes:
You can also do that locally by running below commands in terminal:
Build:
xcodebuild -scheme 'Pagination' -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 11,OS=15.0' build
Test:
xcodebuild -scheme 'Pagination' -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 11,OS=15.0' test
Note: The commands above are running with iPhone 11 but you can use any iOS devices you like.
Marek Loose
link |
Stars: 0 |
Last commit: 28 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics