Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: add initialOffset for OffsetQueryPagingSource #4802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ internal class OffsetQueryPagingSource<RowType : Any>(
private val countQuery: Query<Int>,
private val transacter: TransacterBase,
private val context: CoroutineContext,
private val initialOffset: Int,
) : QueryPagingSource<Int, RowType>() {

override val jumpingSupported get() = true

override suspend fun load(
params: PagingSourceLoadParams<Int>,
): PagingSourceLoadResult<Int, RowType> = withContext(context) {
val key = params.key ?: 0
val key = params.key ?: initialOffset
val limit = when (params) {
is PagingSourceLoadParamsPrepend<*> -> minOf(key, params.loadSize)
else -> params.loadSize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlCursor
import kotlin.coroutines.CoroutineContext
import kotlin.jvm.JvmName
import kotlin.jvm.JvmOverloads
import kotlin.properties.Delegates

internal abstract class QueryPagingSource<Key : Any, RowType : Any> :
Expand All @@ -48,6 +49,7 @@ internal abstract class QueryPagingSource<Key : Any, RowType : Any> :
* Create a [PagingSource] that pages through results according to queries generated by
* [queryProvider]. Queries returned by [queryProvider] should expect to do SQL offset/limit
* based paging. For that reason, [countQuery] is required to calculate pages and page offsets.
* [initialOffset] initial offset to start paging from.
*
* An example query returned by [queryProvider] could look like:
*
Expand All @@ -61,21 +63,24 @@ internal abstract class QueryPagingSource<Key : Any, RowType : Any> :
*/
@Suppress("FunctionName")
@JvmName("QueryPagingSourceInt")
@JvmOverloads
fun <RowType : Any> QueryPagingSource(
countQuery: Query<Int>,
transacter: TransacterBase,
context: CoroutineContext,
queryProvider: (limit: Int, offset: Int) -> Query<RowType>,
initialOffset: Int = 0,
): PagingSource<Int, RowType> = OffsetQueryPagingSource(
queryProvider,
countQuery,
transacter,
context,
initialOffset,
)

/**
* Variant of [QueryPagingSource] that accepts a [Long] instead of an [Int] for [countQuery]
* and [queryProvider].
* , [queryProvider] and [initialOffset].
*
* If the result of [countQuery] exceeds [Int.MAX_VALUE], then the count will be truncated
* to the least significant 32 bits of this [Long] value.
Expand All @@ -84,16 +89,19 @@ fun <RowType : Any> QueryPagingSource(
*/
@Suppress("FunctionName")
@JvmName("QueryPagingSourceLong")
@JvmOverloads
fun <RowType : Any> QueryPagingSource(
countQuery: Query<Long>,
transacter: TransacterBase,
context: CoroutineContext,
queryProvider: (limit: Long, offset: Long) -> Query<RowType>,
initialOffset: Long = 0,
): PagingSource<Int, RowType> = OffsetQueryPagingSource(
{ limit, offset -> queryProvider(limit.toLong(), offset.toLong()) },
countQuery.toInt(),
transacter,
context,
initialOffset.toInt(),
)

private fun Query<Long>.toInt(): Query<Int> =
Expand Down