Skip to content

Commit

Permalink
Don't throw exception on empty data in AndroidX Paging3 extension (#2219
Browse files Browse the repository at this point in the history
)
  • Loading branch information
veyndan committed Mar 29, 2021
1 parent ed0dc0e commit de0ad1a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class OffsetQueryPagingSource<RowType : Any>(
val key = params.key ?: 0L
transacter.transactionWithResult {
val count = countQuery.executeAsOne()
if (key >= count) throw IndexOutOfBoundsException()
if (count != 0L && key >= count) throw IndexOutOfBoundsException()

val loadSize = if (key < 0) params.loadSize + key else params.loadSize

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ class OffsetQueryPagingSourceTest {
transacter = object : TransacterImpl(driver) {}
}

@Test fun `empty page gives correct prevKey and nextKey`() {
driver.execute(null, "DELETE FROM testTable", 0)
val source = OffsetQueryPagingSource(
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher()
)

val results = runBlocking { source.load(Refresh(null, 2, false)) }

assertNull((results as LoadResult.Page).prevKey)
assertNull(results.nextKey)
}

@Test fun `aligned first page gives correct prevKey and nextKey`() {
val source = OffsetQueryPagingSource(
this::query,
Expand Down

0 comments on commit de0ad1a

Please sign in to comment.