-
Notifications
You must be signed in to change notification settings - Fork 2
/
PaginationReversing.kt
40 lines (35 loc) · 1017 Bytes
/
PaginationReversing.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package dev.inmo.micro_utils.pagination.utils
import dev.inmo.micro_utils.pagination.*
/**
* Example:
*
* * `|__f__l_______________________|` will be transformed to `|_______________________f__l__|`
* * `|__f__l_|` will be transformed to `|_f__l__|`
*
* @return Reversed version of this [Pagination]
*/
fun Pagination.reverse(datasetSize: Long): SimplePagination {
val pagesNumber = calculatePagesNumber(size, datasetSize)
val newPage = pagesNumber - page - 1
return when {
page < 0 || page >= pagesNumber -> emptyPagination
else -> Pagination(
newPage,
size
)
}
}
/**
* Shortcut for [reverse]
*/
fun Pagination.reverse(objectsCount: Int) = reverse(objectsCount.toLong())
fun Pagination.optionallyReverse(objectsCount: Int, reverse: Boolean) = if (reverse) {
reverse(objectsCount)
} else {
this
}
fun Pagination.optionallyReverse(objectsCount: Long, reverse: Boolean) = if (reverse) {
reverse(objectsCount)
} else {
this
}