commonMain.dev.inmo.micro_utils.pagination.WalkPagination.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micro_utils.pagination.common-jvm Show documentation
Show all versions of micro_utils.pagination.common-jvm Show documentation
It is set of projects with micro tools for avoiding of routines coding
package dev.inmo.micro_utils.pagination
inline fun doWithPagination(
startPagination: Pagination = FirstPagePagination(),
requestMaker: (pagination: Pagination) -> Pagination?
) = requestMaker(startPagination).let {
var pagination = it
while (pagination != null) {
pagination = requestMaker(pagination)
}
}
inline fun > PR.nextPageIfTrue(condition: PR.() -> Boolean) = if (condition()) {
SimplePagination(
page + 1,
size
)
} else {
null
}
inline fun > PR.thisPageIfTrue(condition: PR.() -> Boolean): PR? = if (condition()) {
this
} else {
null
}
fun PaginationResult<*>.nextPageIfNotEmpty() = nextPageIfTrue { results.isNotEmpty() }
fun PaginationResult.thisPageIfNotEmpty(): PaginationResult? = thisPageIfTrue { results.isNotEmpty() }
fun PaginationResult.currentPageIfNotEmpty() = thisPageIfNotEmpty()
fun PaginationResult<*>.nextPageIfNotEmptyOrLastPage() = nextPageIfTrue { results.isNotEmpty() && !this.isLastPage }
fun PaginationResult.thisPageIfNotEmptyOrLastPage(): PaginationResult? = thisPageIfTrue { results.isNotEmpty() && !this.isLastPage }
fun PaginationResult.currentPageIfNotEmptyOrLastPage() = thisPageIfNotEmptyOrLastPage()
fun PaginationResult<*>.nextPageIfNotLastPage() = nextPageIfTrue { !this.isLastPage }
fun PaginationResult.thisPageIfNotLastPage(): PaginationResult? = thisPageIfTrue { !this.isLastPage }
fun PaginationResult.currentPageIfNotLastPage() = thisPageIfNotLastPage()