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
|
||||
* Factory `PaginationByIndexes`
|
||||
* Shortcut `calculatePagesNumber` with reversed parameters
|
||||
* Value `emptyPagination` for empty `SimplePagination` cases
|
||||
|
||||
## 0.2.0
|
||||
|
||||
|
@ -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(
|
||||
|
@ -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()
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user