From aa45a4ab13ccc2ed3048cfb9cc46c33e37d96da2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 17 Nov 2021 14:07:11 +0600 Subject: [PATCH] now Pagination is an ClosedRange --- CHANGELOG.md | 3 +++ .../dev/inmo/micro_utils/pagination/Pagination.kt | 13 +++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) 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]