fixes in pagination

This commit is contained in:
InsanusMokrassar 2020-10-22 17:57:27 +06:00
parent 9091fa5bd8
commit c6f417f8c8
4 changed files with 19 additions and 15 deletions

View File

@ -7,6 +7,7 @@
* Extension `Pagination#reverse` has been added * Extension `Pagination#reverse` has been added
* Factory `PaginationByIndexes` * Factory `PaginationByIndexes`
* Shortcut `calculatePagesNumber` with reversed parameters * Shortcut `calculatePagesNumber` with reversed parameters
* Value `emptyPagination` for empty `SimplePagination` cases
## 0.2.0 ## 0.2.0

View File

@ -14,6 +14,8 @@ inline fun FirstPagePagination(size: Int = defaultMediumPageSize) =
size = size size = size
) )
val emptyPagination = Pagination(0, 0)
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
inline fun Pagination.nextPage() = inline fun Pagination.nextPage() =
SimplePagination( SimplePagination(

View File

@ -13,15 +13,15 @@ import dev.inmo.micro_utils.pagination.*
fun Pagination.reverse(objectsCount: Long): SimplePagination { fun Pagination.reverse(objectsCount: Long): SimplePagination {
val firstIndex = (objectsCount - (this.lastIndex + 1)).let { val firstIndex = (objectsCount - (this.lastIndex + 1)).let {
when { when {
it < 0 -> it it < 0 -> 0
it > objectsCount -> objectsCount it >= objectsCount -> return emptyPagination
else -> it else -> it
} }
}.toInt() }.toInt()
val lastIndex = (objectsCount - (this.firstIndex + 1)).let { val lastIndex = (objectsCount - (this.firstIndex + 1)).let {
when { when {
it < 0 -> it it < 0 -> return emptyPagination
it > objectsCount -> objectsCount it >= objectsCount -> objectsCount - 1
else -> it else -> it
} }
}.toInt() }.toInt()

View File

@ -7,18 +7,19 @@ import kotlin.test.assertEquals
class PaginationReversingTests { class PaginationReversingTests {
@Test @Test
fun testThatCommonCaseWorksOk() { fun testThatCommonCaseWorksOk() {
val pageSize = 2 val pageSize = 3
val collectionSize = 8 val collectionSize = 9
val pages = calculatePage(collectionSize, pageSize) val pages = calculatePage(collectionSize, pageSize)
doWithPagination(FirstPagePagination(pageSize)) { assertEquals(Pagination(-1, pageSize).reverse(collectionSize), Pagination(0, 0))
val reversed = it.reverse(collectionSize.toLong())
assertEquals(Pagination(calculatePage(collectionSize - it.firstIndex - it.size, it.size), it.size), reversed) val middleFirstIndex = collectionSize / 2 - pageSize / 2
if (it.page < pages) { val middleLastIndex = middleFirstIndex + pageSize - 1
it.nextPage() assertEquals(
} else { PaginationByIndexes(middleFirstIndex, middleLastIndex).reverse(collectionSize),
null PaginationByIndexes(middleFirstIndex, middleLastIndex)
} )
}
assertEquals(Pagination(calculatePagesNumber(collectionSize, pageSize), pageSize).reverse(collectionSize), Pagination(0, 0))
} }
} }