Compare commits

..

No commits in common. "6331f13e9a0436c594c7458a138b4222c449d85d" and "eea645c865718225a2496731dc7044d1b881163f" have entirely different histories.

3 changed files with 18 additions and 50 deletions

View File

@ -3,7 +3,7 @@
## 0.11.14
* `Pagination`:
* `PaginationResult` got new field `objectsNumber` which by default is a times between `pagesNumber` and `size`
* `PaginationResult` got new field `objectsCount` which by default is a times between `pagesNumber` and `size`
## 0.11.13

View File

@ -1,60 +1,24 @@
package dev.inmo.micro_utils.pagination
import kotlinx.serialization.*
import kotlin.math.ceil
import kotlinx.serialization.Serializable
/**
* @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<T>(
override val page: Int,
override val size: Int,
val pagesNumber: Int,
val results: List<T>,
val objectsNumber: Long
) : Pagination {
/**
* Amount of pages for current pagination
*/
@EncodeDefault
val pagesNumber: Int = ceil(objectsNumber / size.toFloat()).toInt()
override val size: Int,
val objectsCount: Long = pagesNumber * size.toLong()
) : Pagination
constructor(
page: Int,
results: List<T>,
pagesNumber: Int,
size: Int
) : this(
page,
size,
results,
(pagesNumber * size).toLong()
)
@Deprecated("Replace with The other order of incoming parameters or objectsCount parameter")
constructor(
page: Int,
pagesNumber: Int,
results: List<T>,
size: Int
) : this(
page,
results,
pagesNumber,
size
)
}
fun <T> emptyPaginationResult() = PaginationResult<T>(0, 0, emptyList(), 0L)
fun <T> emptyPaginationResult() = PaginationResult<T>(0, 0, emptyList(), 0)
/**
* @return New [PaginationResult] with [data] without checking of data sizes equality
*/
fun <I, O> PaginationResult<I>.changeResultsUnchecked(
data: List<O>
): PaginationResult<O> = PaginationResult(page, size, data, objectsNumber)
): PaginationResult<O> = PaginationResult(page, pagesNumber, data, size)
/**
* @return New [PaginationResult] with [data] <b>with</b> checking of data sizes equality
*/
@ -70,8 +34,12 @@ fun <T> List<T>.createPaginationResult(
commonObjectsNumber: Long
) = PaginationResult(
pagination.page,
pagination.size,
calculatePagesNumber(
commonObjectsNumber,
pagination.size
),
this,
pagination.size,
commonObjectsNumber
)
@ -80,8 +48,12 @@ fun <T> List<T>.createPaginationResult(
commonObjectsNumber: Long
) = PaginationResult(
calculatePage(firstIndex, size),
size,
calculatePagesNumber(
commonObjectsNumber,
size
),
this,
size,
commonObjectsNumber
)

View File

@ -26,10 +26,6 @@ inline fun Pagination.nextPage() =
size
)
/**
* @param page Current page number
* @param size Current page size
*/
@Serializable
data class SimplePagination(
override val page: Int,