diff --git a/CHANGELOG.md b/CHANGELOG.md index 95b5f8b908c..5b016dd9dab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.8.3 +* `Pagination`: + * `Pagination` now extends `ClosedRange` + ## 0.8.2 * `Versions`: 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 69bb2138c64..5afd2b1461e 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 @@ -9,7 +9,7 @@ import kotlin.math.floor * If you want to request something, you should use [SimplePagination]. If you need to return some result including * pagination - [PaginationResult] */ -interface Pagination { +interface Pagination : ClosedRange { /** * Started with 0. * Number of page inside of pagination. Offset can be calculated as [page] * [size] @@ -20,6 +20,11 @@ interface Pagination { * Size of current page. Offset can be calculated as [page] * [size] */ val size: Int + + override val start: Int + get() = page * size + override val endInclusive: Int + get() = lastIndex } /** @@ -32,7 +37,7 @@ inline val Pagination.isFirstPage * First number in index of objects. It can be used as offset for databases or other data sources */ val Pagination.firstIndex: Int - get() = page * size + get() = start /** * Last number in index of objects. In fact, one [Pagination] object represent data in next range: @@ -41,7 +46,7 @@ val Pagination.firstIndex: Int * you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19. Here [Pagination.lastIndexExclusive] == 20 */ val Pagination.lastIndexExclusive: Int - get() = firstIndex + size + get() = endInclusive + 1 /** * Last number in index of objects. In fact, one [Pagination] object represent data in next range: @@ -50,7 +55,7 @@ val Pagination.lastIndexExclusive: Int * you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19. */ val Pagination.lastIndex: Int - get() = lastIndexExclusive - 1 + get() = endInclusive /** * Calculates pages count for given [datasetSize]