From 703094c924b4cf4e212caa50ea7d3f09e21c90ba Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 26 Jul 2022 13:56:47 +0600 Subject: [PATCH] add several additional constructors for the pagination result --- .../pagination/PaginationResult.kt | 50 +++++++++++++------ 1 file changed, 34 insertions(+), 16 deletions(-) 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 a24053294bd..54cf4a4039e 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 @@ -1,24 +1,50 @@ package dev.inmo.micro_utils.pagination import kotlinx.serialization.Serializable +import kotlin.math.ceil @Serializable data class PaginationResult( override val page: Int, - val pagesNumber: Int, - val results: List, override val size: Int, - val objectsCount: Long = pagesNumber * size.toLong() -) : Pagination + val results: List, + val objectsCount: Long +) : Pagination { + val pagesNumber: Int = ceil(objectsCount / size.toFloat()).toInt() -fun emptyPaginationResult() = PaginationResult(0, 0, emptyList(), 0) + constructor( + page: Int, + results: List, + pagesNumber: Int, + size: Int + ) : this( + page, + size, + results, + (pagesNumber * size).toLong() + ) + @Deprecated("Replace with The other order of incoming parameters or objectsCount parameter") + constructor( + page: Int, + pagesNumber: Int, + results: List, + size: Int + ) : this( + page, + results, + pagesNumber, + size + ) +} + +fun emptyPaginationResult() = PaginationResult(0, 0, emptyList(), 0L) /** * @return New [PaginationResult] with [data] without checking of data sizes equality */ fun PaginationResult.changeResultsUnchecked( data: List -): PaginationResult = PaginationResult(page, pagesNumber, data, size) +): PaginationResult = PaginationResult(page, size, data, objectsCount) /** * @return New [PaginationResult] with [data] with checking of data sizes equality */ @@ -34,12 +60,8 @@ fun List.createPaginationResult( commonObjectsNumber: Long ) = PaginationResult( pagination.page, - calculatePagesNumber( - commonObjectsNumber, - pagination.size - ), - this, pagination.size, + this, commonObjectsNumber ) @@ -48,12 +70,8 @@ fun List.createPaginationResult( commonObjectsNumber: Long ) = PaginationResult( calculatePage(firstIndex, size), - calculatePagesNumber( - commonObjectsNumber, - size - ), - this, size, + this, commonObjectsNumber )