From eea645c865718225a2496731dc7044d1b881163f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 25 Jul 2022 13:31:15 +0600 Subject: [PATCH] PaginationResult#objectsCount --- CHANGELOG.md | 3 +++ .../pagination/PaginationResult.kt | 9 ++++--- .../repos/mappers/KeyValueMappers.kt | 24 ++++++------------- .../repos/mappers/OneToManyKeyValueMappers.kt | 24 ++++++------------- .../repos/keyvalue/KeyValueStore.kt | 24 ++++++------------- 5 files changed, 30 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a80f1d8721e..4ad586b1834 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.11.14 +* `Pagination`: + * `PaginationResult` got new field `objectsCount` which by default is a times between `pagesNumber` and `size` + ## 0.11.13 * `Versions`: diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/PaginationResult.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/PaginationResult.kt index fbc092069d4..a24053294bd 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/PaginationResult.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/PaginationResult.kt @@ -7,7 +7,8 @@ data class PaginationResult( override val page: Int, val pagesNumber: Int, val results: List, - override val size: Int + override val size: Int, + val objectsCount: Long = pagesNumber * size.toLong() ) : Pagination fun emptyPaginationResult() = PaginationResult(0, 0, emptyList(), 0) @@ -38,7 +39,8 @@ fun List.createPaginationResult( pagination.size ), this, - pagination.size + pagination.size, + commonObjectsNumber ) fun List.createPaginationResult( @@ -51,7 +53,8 @@ fun List.createPaginationResult( size ), this, - size + size, + commonObjectsNumber ) fun Pair>.createPaginationResult( diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt index 4df2b614f6e..dfabde93e95 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt @@ -1,7 +1,6 @@ package dev.inmo.micro_utils.repos.mappers -import dev.inmo.micro_utils.pagination.Pagination -import dev.inmo.micro_utils.pagination.PaginationResult +import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.* import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map @@ -23,11 +22,8 @@ open class MapperReadKeyValueRepo( pagination, reversed ).let { - PaginationResult( - it.page, - it.pagesNumber, - it.results.map { it.toInnerValue() }, - it.size + it.changeResultsUnchecked( + it.results.map { it.toInnerValue() } ) } @@ -38,11 +34,8 @@ open class MapperReadKeyValueRepo( pagination, reversed ).let { - PaginationResult( - it.page, - it.pagesNumber, - it.results.map { it.toInnerKey() }, - it.size + it.changeResultsUnchecked( + it.results.map { it.toInnerKey() } ) } @@ -55,11 +48,8 @@ open class MapperReadKeyValueRepo( pagination, reversed ).let { - PaginationResult( - it.page, - it.pagesNumber, - it.results.map { it.toInnerKey() }, - it.size + it.changeResultsUnchecked( + it.results.map { it.toInnerKey() } ) } diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/OneToManyKeyValueMappers.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/OneToManyKeyValueMappers.kt index f74b81d9149..809f7ae340c 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/OneToManyKeyValueMappers.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/OneToManyKeyValueMappers.kt @@ -1,7 +1,6 @@ package dev.inmo.micro_utils.repos.mappers -import dev.inmo.micro_utils.pagination.Pagination -import dev.inmo.micro_utils.pagination.PaginationResult +import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.* import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map @@ -21,11 +20,8 @@ open class MapperReadKeyValuesRepo( pagination, reversed ).let { - PaginationResult( - it.page, - it.pagesNumber, - it.results.map { it.toInnerValue() }, - it.size + it.changeResultsUnchecked( + it.results.map { it.toInnerValue() } ) } @@ -36,11 +32,8 @@ open class MapperReadKeyValuesRepo( pagination, reversed ).let { - PaginationResult( - it.page, - it.pagesNumber, - it.results.map { it.toInnerKey() }, - it.size + it.changeResultsUnchecked( + it.results.map { it.toInnerKey() } ) } @@ -53,11 +46,8 @@ open class MapperReadKeyValuesRepo( pagination, reversed ).let { - PaginationResult( - it.page, - it.pagesNumber, - it.results.map { it.toInnerKey() }, - it.size + it.changeResultsUnchecked( + it.results.map { it.toInnerKey() } ) } diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt index dbddcbf52cb..ccbc93fe0b7 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt @@ -3,8 +3,7 @@ package dev.inmo.micro_utils.repos.keyvalue import android.content.Context import android.content.SharedPreferences import androidx.core.content.edit -import dev.inmo.micro_utils.pagination.Pagination -import dev.inmo.micro_utils.pagination.PaginationResult +import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.pagination.utils.reverse import dev.inmo.micro_utils.repos.KeyValueRepo @@ -72,14 +71,11 @@ class KeyValueStore internal constructor ( return sharedPreferences.all.values.paginate( resultPagination ).let { - PaginationResult( - it.page, - it.pagesNumber, + it.changeResultsUnchecked( it.results.map { @Suppress("UNCHECKED_CAST") it as T - }.let { if (reversed) it.reversed() else it }, - it.size + }.let { if (reversed) it.reversed() else it } ) } } @@ -89,11 +85,8 @@ class KeyValueStore internal constructor ( return sharedPreferences.all.keys.paginate( resultPagination ).let { - PaginationResult( - it.page, - it.pagesNumber, - it.results.let { if (reversed) it.reversed() else it }, - it.size + it.changeResultsUnchecked( + it.results.let { if (reversed) it.reversed() else it } ) } } @@ -103,11 +96,8 @@ class KeyValueStore internal constructor ( return sharedPreferences.all.mapNotNull { (k, value) -> if (value == v) k else null }.paginate( resultPagination ).let { - PaginationResult( - it.page, - it.pagesNumber, - it.results.let { if (reversed) it.reversed() else it }, - it.size + it.changeResultsUnchecked( + it.results.let { if (reversed) it.reversed() else it } ) } }