From c6f417f8c8153a09def8049091a7b5320cd05d1e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 22 Oct 2020 17:57:27 +0600 Subject: [PATCH] fixes in pagination --- CHANGELOG.md | 1 + .../pagination/SimplePagination.kt | 2 ++ .../pagination/utils/PaginationReversing.kt | 8 +++---- .../pagination/utils/PaginationReversing.kt | 23 ++++++++++--------- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfc58b0982d..cb396f6008a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Extension `Pagination#reverse` has been added * Factory `PaginationByIndexes` * Shortcut `calculatePagesNumber` with reversed parameters + * Value `emptyPagination` for empty `SimplePagination` cases ## 0.2.0 diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/SimplePagination.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/SimplePagination.kt index f3ca4e1e8db..eb4bb6aabd8 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/SimplePagination.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/SimplePagination.kt @@ -14,6 +14,8 @@ inline fun FirstPagePagination(size: Int = defaultMediumPageSize) = size = size ) +val emptyPagination = Pagination(0, 0) + @Suppress("NOTHING_TO_INLINE") inline fun Pagination.nextPage() = SimplePagination( diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt index 678b7976bc6..e91f0e5584e 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt @@ -13,15 +13,15 @@ import dev.inmo.micro_utils.pagination.* fun Pagination.reverse(objectsCount: Long): SimplePagination { val firstIndex = (objectsCount - (this.lastIndex + 1)).let { when { - it < 0 -> it - it > objectsCount -> objectsCount + it < 0 -> 0 + it >= objectsCount -> return emptyPagination else -> it } }.toInt() val lastIndex = (objectsCount - (this.firstIndex + 1)).let { when { - it < 0 -> it - it > objectsCount -> objectsCount + it < 0 -> return emptyPagination + it >= objectsCount -> objectsCount - 1 else -> it } }.toInt() diff --git a/pagination/common/src/commonTest/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt b/pagination/common/src/commonTest/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt index 4ba6b148b3a..a66330c2cad 100644 --- a/pagination/common/src/commonTest/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt +++ b/pagination/common/src/commonTest/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt @@ -7,18 +7,19 @@ import kotlin.test.assertEquals class PaginationReversingTests { @Test fun testThatCommonCaseWorksOk() { - val pageSize = 2 - val collectionSize = 8 + val pageSize = 3 + val collectionSize = 9 val pages = calculatePage(collectionSize, pageSize) - doWithPagination(FirstPagePagination(pageSize)) { - val reversed = it.reverse(collectionSize.toLong()) - assertEquals(Pagination(calculatePage(collectionSize - it.firstIndex - it.size, it.size), it.size), reversed) - if (it.page < pages) { - it.nextPage() - } else { - null - } - } + assertEquals(Pagination(-1, pageSize).reverse(collectionSize), Pagination(0, 0)) + + val middleFirstIndex = collectionSize / 2 - pageSize / 2 + val middleLastIndex = middleFirstIndex + pageSize - 1 + assertEquals( + PaginationByIndexes(middleFirstIndex, middleLastIndex).reverse(collectionSize), + PaginationByIndexes(middleFirstIndex, middleLastIndex) + ) + + assertEquals(Pagination(calculatePagesNumber(collectionSize, pageSize), pageSize).reverse(collectionSize), Pagination(0, 0)) } }