commonMain.dev.inmo.micro_utils.pagination.PaginationResult.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
import kotlinx.serialization.*
import kotlin.math.ceil
/**
* @param page Current page number
* @param size Current page size. It can be greater than size of [results]
* @param results Result objects
* @param objectsNumber Count of all objects across all pages
*/
@Serializable
data class PaginationResult(
override val page: Int,
override val size: Int,
val results: List,
val objectsNumber: Long
) : Pagination {
/**
* Amount of pages for current pagination
*/
@EncodeDefault
@SerialName("pagesNumber")
val pagesNumberLong: Long = ceil(objectsNumber / size.toFloat()).toLong()
/**
* Amount of pages for current pagination
*/
@Transient
val pagesNumber: Int = pagesNumberLong.toInt()
constructor(
page: Int,
results: List,
pagesNumber: Int,
size: Int
) : this(
page,
size,
results,
(pagesNumber * size).toLong()
)
}
val PaginationResult<*>.lastPageLong
get() = pagesNumberLong - 1
val PaginationResult<*>.lastPage
get() = lastPageLong.toInt()
val PaginationResult<*>.isLastPage
get() = page.toLong() == lastPageLong
fun emptyPaginationResult(
basePagination: Pagination,
objectsNumber: Number
) = PaginationResult(
basePagination.page,
basePagination.size,
emptyList(),
objectsNumber.toLong()
)
fun emptyPaginationResult(
basePagination: Pagination,
) = emptyPaginationResult(basePagination, 0)
fun emptyPaginationResult() = emptyPaginationResult(FirstPagePagination(0))
/**
* @return New [PaginationResult] with [data] without checking of data sizes equality
*/
inline fun PaginationResult.changeResultsUnchecked(
block: PaginationResult.() -> List
): PaginationResult = PaginationResult(page, size, block(), objectsNumber)
/**
* @return New [PaginationResult] with [data] without checking of data sizes equality
*/
fun PaginationResult.changeResultsUnchecked(
data: List
): PaginationResult = changeResultsUnchecked { data }
/**
* @return New [PaginationResult] with [data] with checking of data sizes equality
*/
inline fun PaginationResult.changeResults(
block: PaginationResult.() -> List
): PaginationResult {
val data = block()
require(data.size == results.size)
return changeResultsUnchecked(data)
}
/**
* @return New [PaginationResult] with [data] with checking of data sizes equality
*/
fun PaginationResult.changeResults(
data: List
): PaginationResult = changeResults { data }
fun List.createPaginationResult(
pagination: Pagination,
commonObjectsNumber: Long
) = PaginationResult(
pagination.page,
pagination.size,
this,
commonObjectsNumber
)
fun List.createPaginationResult(
firstIndex: Int,
commonObjectsNumber: Long
) = PaginationResult(
calculatePage(firstIndex, size),
size,
this,
commonObjectsNumber
)
fun Pair>.createPaginationResult(
pagination: Pagination
) = second.createPaginationResult(pagination, first)