update repos to use new reverse method

This commit is contained in:
InsanusMokrassar 2020-10-22 15:47:28 +06:00
parent 4972cc9daa
commit 418af7874d
3 changed files with 33 additions and 32 deletions

View File

@ -27,3 +27,8 @@ fun Pagination.reverse(objectsCount: Long): SimplePagination {
}.toInt()
return PaginationByIndexes(firstIndex, lastIndex)
}
/**
* Shortcut for [reverse]
*/
fun Pagination.reverse(objectsCount: Int) = reverse(objectsCount.toLong())

View File

@ -2,6 +2,8 @@ package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.micro_utils.pagination.utils.reverse
import kotlinx.coroutines.flow.Flow
class ReadMapKeyValueRepo<Key, Value>(
@ -13,31 +15,27 @@ class ReadMapKeyValueRepo<Key, Value>(
pagination: Pagination,
reversed: Boolean
): PaginationResult<Value> {
val firstIndex: Int = if (reversed) {
val size = map.size
(size - pagination.lastIndex).let { if (it < 0) 0 else it }
} else {
pagination.firstIndex
val values = map.values
val actualPagination = if (reversed) pagination.reverse(values.size) else pagination
return values.paginate(actualPagination).let {
if (reversed) {
it.copy(results = it.results.reversed())
} else {
it
}
}
return map.keys.drop(firstIndex).take(pagination.size).mapNotNull { map[it] }.createPaginationResult(
firstIndex,
count()
)
}
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
val firstIndex: Int = if (reversed) {
val size = map.size
(size - pagination.lastIndex).let { if (it < 0) 0 else it }
} else {
pagination.firstIndex
val keys = map.keys
val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
return keys.paginate(actualPagination).let {
if (reversed) {
it.copy(results = it.results.reversed())
} else {
it
}
}
return map.keys.drop(firstIndex).take(pagination.size).createPaginationResult(
firstIndex,
count()
)
}
override suspend fun contains(key: Key): Boolean = map.containsKey(key)

View File

@ -3,6 +3,7 @@ package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.micro_utils.pagination.utils.reverse
import kotlinx.coroutines.flow.Flow
class MapReadOneToManyKeyValueRepo<Key, Value>(
@ -13,8 +14,7 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
return list.paginate(
if (reversed) {
val firstIndex = (map.size - pagination.lastIndex).let { if (it < 0) 0 else it }
SimplePagination(firstIndex, pagination.size)
pagination.reverse(list.size)
} else {
pagination
}
@ -22,17 +22,15 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
}
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
val firstIndex: Int = if (reversed) {
val size = map.size
(size - pagination.lastIndex).let { if (it < 0) 0 else it }
} else {
pagination.firstIndex
val keys = map.keys
val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
return keys.paginate(actualPagination).let {
if (reversed) {
it.copy(results = it.results.reversed())
} else {
it
}
}
return map.keys.drop(firstIndex).take(pagination.size).createPaginationResult(
firstIndex,
count()
)
}
override suspend fun contains(k: Key): Boolean = map.containsKey(k)