small refactoring

This commit is contained in:
InsanusMokrassar 2019-11-04 20:35:24 +06:00
parent 7a5fd177c8
commit f2d66b2fe9
3 changed files with 25 additions and 18 deletions

View File

@ -39,6 +39,11 @@ val Pagination.lastIndex: Int
/**
* Calculates pages count for given [datasetSize]
*/
fun Pagination.pagesCount(datasetSize: Int): Int {
return ceil(datasetSize.toDouble() / size).toInt()
fun calculatePagesNumber(datasetSize: Int, pageSize: Int): Int {
return ceil(datasetSize.toDouble() / pageSize).toInt()
}
/**
* Calculates pages count for given [datasetSize]
*/
inline fun Pagination.calculatePagesNumber(datasetSize: Int): Int = calculatePagesNumber(datasetSize, size)

View File

@ -1,14 +1,21 @@
package com.insanusmokrassar.postssystem.core.utils.pagination
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
@Serializable
data class PaginationResult<T>(
override val page: Int,
val pagesNumber: Int,
val results: List<T>
) : Pagination {
@Transient
val results: List<T>,
override val size: Int = results.size
}
) : Pagination
fun <T> Pagination.createResult(
commonObjectsNumber: Int,
results: List<T>
) = PaginationResult(
page,
calculatePagesNumber(commonObjectsNumber),
results,
size
)

View File

@ -2,10 +2,7 @@ package com.insanusmokrassar.postssystem.core.api
import com.insanusmokrassar.postssystem.core.content.*
import com.insanusmokrassar.postssystem.core.content.api.ContentAPI
import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination
import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult
import com.insanusmokrassar.postssystem.core.utils.pagination.firstIndex
import com.insanusmokrassar.postssystem.core.utils.pagination.pagesCount
import com.insanusmokrassar.postssystem.core.utils.pagination.*
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
@ -20,6 +17,8 @@ private fun generateId(): ContentId = UUID.randomUUID().toString()
class InMemoryContentAPI(
initialContent: List<RegisteredContent> = emptyList()
): ContentAPI {
private val contentCreatedBroadcastChannel = BroadcastChannel<RegisteredContent>(Channel.BUFFERED)
private val contentDeletedBroadcastChannel = BroadcastChannel<RegisteredContent>(Channel.BUFFERED)
override val contentCreatedFlow: Flow<RegisteredContent>
get() = contentCreatedBroadcastChannel.asFlow()
@ -31,9 +30,6 @@ class InMemoryContentAPI(
.associateBy(RegisteredContent::id)
.toMutableMap()
private val contentCreatedBroadcastChannel = BroadcastChannel<RegisteredContent>(Channel.BUFFERED)
private val contentDeletedBroadcastChannel = BroadcastChannel<RegisteredContent>(Channel.BUFFERED)
override suspend fun createContent(content: Content): RegisteredContent? {
return content.createRegisteredEntity(generateId())?.also { registeredContent ->
contents[registeredContent.id] = registeredContent
@ -44,10 +40,9 @@ class InMemoryContentAPI(
override suspend fun getContentById(id: ContentId): RegisteredContent? = contents[id]
override suspend fun getContentByPagination(pagination: Pagination): PaginationResult<out RegisteredContent> {
return PaginationResult(
page = pagination.page,
pagesNumber = pagination.pagesCount(contents.size),
results = contents.values.drop(pagination.firstIndex).take(pagination.size)
return pagination.createResult(
commonObjectsNumber = contents.size,
results = contents.values.asSequence().drop(pagination.firstIndex).take(pagination.size).toList()
)
}