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
Allow construction of OffsetQueryPagingSource with Long #3409
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import androidx.paging.PagingConfig | |
import androidx.paging.PagingSource | ||
import app.cash.sqldelight.Query | ||
import app.cash.sqldelight.Transacter | ||
import app.cash.sqldelight.db.SqlCursor | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.properties.Delegates | ||
|
@@ -70,6 +71,35 @@ fun <RowType : Any> QueryPagingSource( | |
context, | ||
) | ||
|
||
/** | ||
* Variant of [QueryPagingSource] that accepts a [Long] instead of an [Int] for [countQuery] | ||
* and [queryProvider]. | ||
* | ||
* 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. | ||
* | ||
* @see toInt | ||
*/ | ||
@Suppress("FunctionName") | ||
fun <RowType : Any> QueryPagingSourceLong( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer having this be the same name as the other function ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we just switch them so the Int one has a fancy name since SQLDelight users will pretty much only use this one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, just renamed it to @AlecStrong I just suffixed the type to each one for consistency in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, perfect |
||
countQuery: Query<Long>, | ||
transacter: Transacter, | ||
context: CoroutineContext = Dispatchers.IO, | ||
queryProvider: (limit: Long, offset: Long) -> Query<RowType>, | ||
): PagingSource<Int, RowType> = OffsetQueryPagingSource( | ||
{ limit, offset -> queryProvider(limit.toLong(), offset.toLong()) }, | ||
countQuery.toInt(), | ||
transacter, | ||
context, | ||
) | ||
|
||
private fun Query<Long>.toInt(): Query<Int> = | ||
object : Query<Int>({ cursor -> mapper(cursor).toInt() }) { | ||
override fun <R> execute(mapper: (SqlCursor) -> R) = this@toInt.execute(mapper) | ||
override fun addListener(listener: Listener) = this@toInt.addListener(listener) | ||
override fun removeListener(listener: Listener) = this@toInt.removeListener(listener) | ||
} | ||
|
||
/** | ||
* Create a [PagingSource] that pages through results according to queries generated by | ||
* [queryProvider]. Queries returned by [queryProvider] should expected to do keyset paging. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wanted a test to ensure that the constructor works as expected. This test is a copy of
load_initialEmptyLoad
, just usingLong
instead ofInt
.