From 758a92410b364a7e8218bfe9dc3ae74aa553e39a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 26 May 2024 23:51:46 +0600 Subject: [PATCH] more fixes to god of fixes in paginations --- .../pagination/PaginationResult.kt | 10 ++++++--- .../micro_utils/pagination/utils/Paginate.kt | 4 ++-- .../pagination/utils/PaginationPaging.kt | 22 ++++++++++++++++++- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/PaginationResult.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/PaginationResult.kt index 39720d86c80..217fff6006d 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/PaginationResult.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/PaginationResult.kt @@ -50,15 +50,19 @@ val PaginationResult<*>.lastPage val PaginationResult<*>.isLastPage get() = page.toLong() == lastPageLong -fun emptyPaginationResult() = PaginationResult(0, 0, emptyList(), 0L) fun emptyPaginationResult( - basePagination: Pagination + basePagination: Pagination, + objectsNumber: Number ) = PaginationResult( basePagination.page, basePagination.size, emptyList(), - 0L + objectsNumber.toLong() ) +fun emptyPaginationResult( + basePagination: Pagination, +) = emptyPaginationResult(basePagination, 0) +fun emptyPaginationResult() = emptyPaginationResult(FirstPagePagination(0)) /** * @return New [PaginationResult] with [data] without checking of data sizes equality diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/Paginate.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/Paginate.kt index 660635a4c8a..3c11e0667b2 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/Paginate.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/Paginate.kt @@ -21,8 +21,8 @@ fun Iterable.paginate(with: Pagination): PaginationResult { } fun List.paginate(with: Pagination): PaginationResult { - if (with.firstIndex !in indices || with.lastIndex !in indices) { - return emptyPaginationResult(with) + if (with.firstIndex >= size || with.lastIndex < 0) { + return emptyPaginationResult(with, size.toLong()) } return asSequence().drop(with.firstIndex).take(with.size).toList().createPaginationResult( with, diff --git a/pagination/common/src/commonTest/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationPaging.kt b/pagination/common/src/commonTest/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationPaging.kt index 9dd539110ec..0767b755e81 100644 --- a/pagination/common/src/commonTest/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationPaging.kt +++ b/pagination/common/src/commonTest/kotlin/dev/inmo/micro_utils/pagination/utils/PaginationPaging.kt @@ -38,7 +38,27 @@ class PaginationPaging { doForAllWithNextPaging(startPagination) { val resultPagination = list.paginate(it) - assertEquals(resultPagination, emptyPaginationResult(it)) + assertEquals(resultPagination, emptyPaginationResult(it, list.size)) + + assertFalse(paginationHappend) + + paginationHappend = true + + resultPagination + } + + assertTrue(paginationHappend) + } + @Test + fun testRightOutPaginateOnList() { + val list = (0 until 7).toList() + val startPagination = SimplePagination(page = 4, size = 2) + + var paginationHappend = false + doForAllWithNextPaging(startPagination) { + val resultPagination = list.paginate(it) + + assertEquals(resultPagination, emptyPaginationResult(it, list.size)) assertFalse(paginationHappend)