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