mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
fixes in pagination
This commit is contained in:
parent
9091fa5bd8
commit
c6f417f8c8
@ -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
|
||||||
|
|
||||||
|
@ -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(
|
||||||
|
@ -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()
|
||||||
|
@ -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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user