diff --git a/CHANGELOG.md b/CHANGELOG.md index 238a31cd99a..dfc58b0982d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ * `Pagination` * `Common`: * Extension `Pagination#reverse` has been added + * Factory `PaginationByIndexes` + * Shortcut `calculatePagesNumber` with reversed parameters ## 0.2.0 diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt index 53d8958dc42..2ea52f141ae 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt @@ -43,6 +43,11 @@ val Pagination.lastIndex: Int fun calculatePagesNumber(datasetSize: Long, pageSize: Int): Int { return ceil(datasetSize.toDouble() / pageSize).toInt() } +/** + * Calculates pages count for given [datasetSize]. As a fact, it is shortcut for [calculatePagesNumber] + * @return calculated page number which can be correctly used in [PaginationResult] as [PaginationResult.page] value + */ +fun calculatePagesNumber(pageSize: Int, datasetSize: Long): Int = calculatePagesNumber(datasetSize, pageSize) /** * Calculates pages count for given [datasetSize] */ 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 900bf745794..f3ca4e1e8db 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 @@ -27,7 +27,21 @@ data class SimplePagination( override val size: Int ) : Pagination +/** + * Factory for [SimplePagination] + */ fun Pagination( page: Int, size: Int ) = SimplePagination(page, size) + +/** + * @param firstIndex Inclusive first index of pagination + * @param lastIndex INCLUSIVE last index of pagination (last index of object covered by result [SimplePagination]) + */ +fun PaginationByIndexes( + firstIndex: Int, + lastIndex: Int +) = maxOf(0, (lastIndex - firstIndex + 1)).let { size -> + Pagination(calculatePage(firstIndex, size), size) +} 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 ba921212880..6835c2d0bfc 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 @@ -11,16 +11,19 @@ import dev.inmo.micro_utils.pagination.* * @return Reversed version of this [Pagination] */ fun Pagination.reverse(objectsCount: Long): SimplePagination { - val resultSize = minOf(size, objectsCount.toInt()) - return when { - firstIndex > objectsCount -> Pagination(calculatePage(resultSize, resultSize), resultSize) - size > objectsCount -> FirstPagePagination(resultSize) - else -> { - val firstIndex = (objectsCount - firstIndex - resultSize).toInt() - Pagination( - firstIndex, - resultSize - ) + val firstIndex = (objectsCount - (this.lastIndex + 1)).let { + when { + it < 0 -> it + it > objectsCount -> objectsCount + else -> it } - } + }.toInt() + val lastIndex = (objectsCount - (this.firstIndex + 1)).let { + when { + it < 0 -> it + it > objectsCount -> objectsCount + else -> it + } + }.toInt() + return PaginationByIndexes(firstIndex, lastIndex) } 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 new file mode 100644 index 00000000000..4ba6b148b3a --- /dev/null +++ b/pagination/common/src/commonTest/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationReversing.kt @@ -0,0 +1,24 @@ +package dev.inmo.micro_utils.pagination.utils + +import dev.inmo.micro_utils.pagination.* +import kotlin.test.Test +import kotlin.test.assertEquals + +class PaginationReversingTests { + @Test + fun testThatCommonCaseWorksOk() { + val pageSize = 2 + val collectionSize = 8 + 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 + } + } + } +}