refactor(android): leverage assisted inject for `LobstersPagingSource`
This commit is contained in:
parent
59b35a9255
commit
2afbc73bd4
|
@ -2,13 +2,19 @@ package dev.msfjarvis.claw.android.paging
|
|||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.paging.PagingState
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedFactory
|
||||
import dagger.assisted.AssistedInject
|
||||
import dev.msfjarvis.claw.android.injection.IODispatcher
|
||||
import dev.msfjarvis.claw.model.LobstersPost
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class LobstersPagingSource(
|
||||
private val getMorePosts: suspend (Int) -> List<LobstersPost>,
|
||||
private val ioDispatcher: CoroutineDispatcher,
|
||||
class LobstersPagingSource
|
||||
@AssistedInject
|
||||
constructor(
|
||||
@Assisted private val getMorePosts: suspend (Int) -> List<LobstersPost>,
|
||||
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : PagingSource<Int, LobstersPost>() {
|
||||
|
||||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, LobstersPost> {
|
||||
|
@ -29,4 +35,9 @@ class LobstersPagingSource(
|
|||
override fun getRefreshKey(state: PagingState<Int, LobstersPost>): Int {
|
||||
return state.pages.size + 1
|
||||
}
|
||||
|
||||
@AssistedFactory
|
||||
interface Factory {
|
||||
fun create(getMorePosts: suspend (Int) -> List<LobstersPost>): LobstersPagingSource
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,19 +25,18 @@ constructor(
|
|||
private val api: LobstersApi,
|
||||
private val savedPostsRepository: SavedPostsRepository,
|
||||
private val postDetailsRepository: PostDetailsRepository,
|
||||
private val pagingSourceFactory: LobstersPagingSource.Factory,
|
||||
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||
) : ViewModel() {
|
||||
private var hottestPostsPagingSource: LobstersPagingSource? = null
|
||||
private var newestPostsPagingSource: LobstersPagingSource? = null
|
||||
private val hottestPostsPager =
|
||||
Pager(PagingConfig(20)) {
|
||||
LobstersPagingSource(api::getHottestPosts, ioDispatcher).also {
|
||||
hottestPostsPagingSource = it
|
||||
}
|
||||
pagingSourceFactory.create(api::getHottestPosts).also { hottestPostsPagingSource = it }
|
||||
}
|
||||
private val newestPostsPager =
|
||||
Pager(PagingConfig(20)) {
|
||||
LobstersPagingSource(api::getNewestPosts, ioDispatcher).also { newestPostsPagingSource = it }
|
||||
pagingSourceFactory.create(api::getHottestPosts).also { newestPostsPagingSource = it }
|
||||
}
|
||||
|
||||
val hottestPosts
|
||||
|
|
Loading…
Reference in New Issue