All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.dev.inmo.micro_utils.pagination.WalkPagination.kt Maven / Gradle / Ivy

There is a newer version: 0.22.2
Show newest version
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()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy