hope last fix

This commit is contained in:
InsanusMokrassar 2020-10-22 18:37:56 +06:00
parent c6f417f8c8
commit 864d0ffcc6
2 changed files with 15 additions and 16 deletions

View File

@ -10,22 +10,22 @@ import dev.inmo.micro_utils.pagination.*
* *
* @return Reversed version of this [Pagination] * @return Reversed version of this [Pagination]
*/ */
fun Pagination.reverse(objectsCount: Long): SimplePagination { fun Pagination.reverse(datasetSize: Long): SimplePagination {
val firstIndex = (objectsCount - (this.lastIndex + 1)).let { val pagesNumber = calculatePagesNumber(size, datasetSize)
when { val newPage = pagesNumber - page - 1
it < 0 -> 0 return when {
it >= objectsCount -> return emptyPagination page < 0 || page >= pagesNumber -> emptyPagination
else -> it else -> Pagination(
newPage,
size
).let {
if (it.lastIndex > datasetSize) {
it.copy(size = (datasetSize - firstIndex - 1).toInt())
} else {
it
}
} }
}.toInt() }
val lastIndex = (objectsCount - (this.firstIndex + 1)).let {
when {
it < 0 -> return emptyPagination
it >= objectsCount -> objectsCount - 1
else -> it
}
}.toInt()
return PaginationByIndexes(firstIndex, lastIndex)
} }
/** /**

View File

@ -9,7 +9,6 @@ class PaginationReversingTests {
fun testThatCommonCaseWorksOk() { fun testThatCommonCaseWorksOk() {
val pageSize = 3 val pageSize = 3
val collectionSize = 9 val collectionSize = 9
val pages = calculatePage(collectionSize, pageSize)
assertEquals(Pagination(-1, pageSize).reverse(collectionSize), Pagination(0, 0)) assertEquals(Pagination(-1, pageSize).reverse(collectionSize), Pagination(0, 0))