mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
commit
8b61c984eb
@ -1,5 +1,13 @@
|
||||
# Changelog
|
||||
|
||||
## 0.4.8
|
||||
|
||||
* `Versions`:
|
||||
* `Coroutines`: `1.4.1` -> `1.4.2`
|
||||
* `UUID`: `0.2.2` -> `0.2.3`
|
||||
* `Pagination`
|
||||
* Add `PaginatedIterable` and `PaginatedIterator`
|
||||
|
||||
## 0.4.7
|
||||
|
||||
* `Ktor`
|
||||
|
@ -5,10 +5,9 @@ kotlin.incremental=true
|
||||
kotlin.incremental.js=true
|
||||
android.useAndroidX=true
|
||||
android.enableJetifier=true
|
||||
org.gradle.jvmargs=-Xmx2048m
|
||||
|
||||
kotlin_version=1.4.20
|
||||
kotlin_coroutines_version=1.4.1
|
||||
kotlin_coroutines_version=1.4.2
|
||||
kotlin_serialisation_core_version=1.0.1
|
||||
kotlin_exposed_version=0.28.1
|
||||
|
||||
@ -18,7 +17,7 @@ klockVersion=2.0.0
|
||||
|
||||
github_release_plugin_version=2.2.12
|
||||
|
||||
uuidVersion=0.2.2
|
||||
uuidVersion=0.2.3
|
||||
|
||||
# ANDROID
|
||||
|
||||
@ -36,10 +35,10 @@ espresso_core=3.3.0
|
||||
|
||||
# Dokka
|
||||
|
||||
dokka_version=1.4.0
|
||||
dokka_version=1.4.10.2
|
||||
|
||||
# Project data
|
||||
|
||||
group=dev.inmo
|
||||
version=0.4.7
|
||||
android_code_version=11
|
||||
version=0.4.8
|
||||
android_code_version=12
|
||||
|
@ -0,0 +1,41 @@
|
||||
package dev.inmo.micro_utils.pagination.utils
|
||||
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
|
||||
class PaginatedIterator<T>(
|
||||
pageSize: Int,
|
||||
private val countGetter: () -> Long,
|
||||
private val paginationResultGetter: Pagination.() -> PaginationResult<T>
|
||||
) : Iterator<T> {
|
||||
private var pagination = FirstPagePagination(pageSize)
|
||||
private val currentStack = mutableListOf<T>()
|
||||
override fun hasNext(): Boolean = currentStack.isNotEmpty() || (countGetter() < pagination.lastIndexExclusive)
|
||||
|
||||
override fun next(): T {
|
||||
if (currentStack.isEmpty()) {
|
||||
val resultPagination = paginationResultGetter.invoke(pagination)
|
||||
currentStack.addAll(resultPagination.results)
|
||||
require(currentStack.isNotEmpty()) { "There is no elements left" }
|
||||
pagination = resultPagination.nextPage()
|
||||
}
|
||||
return currentStack.removeFirst()
|
||||
}
|
||||
}
|
||||
|
||||
class PaginatedIterable<T>(
|
||||
private val pageSize: Int,
|
||||
private val countGetter: () -> Long,
|
||||
private val paginationResultGetter: Pagination.() -> PaginationResult<T>
|
||||
) : Iterable<T> {
|
||||
override fun iterator(): Iterator<T> = PaginatedIterator(pageSize, countGetter, paginationResultGetter)
|
||||
}
|
||||
|
||||
/**
|
||||
* Will make iterable using incoming [countGetter] and [paginationResultGetter]
|
||||
*/
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun <T> makeIterable(
|
||||
noinline countGetter: () -> Long,
|
||||
pageSize: Int = defaultMediumPageSize,
|
||||
noinline paginationResultGetter: Pagination.() -> PaginationResult<T>
|
||||
): Iterable<T> = PaginatedIterable(pageSize, countGetter, paginationResultGetter)
|
Loading…
Reference in New Issue
Block a user