mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-13 18:27:00 +05:30
refactor(android): abstract away getMorePosts
type
This commit is contained in:
parent
f0cb6d638f
commit
eb058cfa3d
2 changed files with 9 additions and 3 deletions
|
@ -13,14 +13,14 @@ import kotlinx.coroutines.withContext
|
|||
class LobstersPagingSource
|
||||
@AssistedInject
|
||||
constructor(
|
||||
@Assisted private val getMorePosts: suspend (Int) -> List<LobstersPost>,
|
||||
@Assisted private val remoteFetcher: RemoteFetcher<LobstersPost>,
|
||||
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : PagingSource<Int, LobstersPost>() {
|
||||
|
||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, LobstersPost> {
|
||||
return try {
|
||||
val page = params.key ?: 1
|
||||
val posts = withContext(ioDispatcher) { getMorePosts(page) }
|
||||
val posts = withContext(ioDispatcher) { remoteFetcher.getItemsAtPage(page) }
|
||||
|
||||
LoadResult.Page(
|
||||
data = posts,
|
||||
|
@ -38,6 +38,6 @@ constructor(
|
|||
|
||||
@AssistedFactory
|
||||
interface Factory {
|
||||
fun create(getMorePosts: suspend (Int) -> List<LobstersPost>): LobstersPagingSource
|
||||
fun create(remoteFetcher: RemoteFetcher<LobstersPost>): LobstersPagingSource
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
package dev.msfjarvis.claw.android.paging
|
||||
|
||||
/** SAM interface to abstract over a remote API that fetches paginated content. */
|
||||
fun interface RemoteFetcher<T> {
|
||||
suspend fun getItemsAtPage(page: Int): List<T>
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue