mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-13 18:27:00 +05:30
refactor(android): leverage assisted inject for LobstersPagingSource
This commit is contained in:
parent
59b35a9255
commit
2afbc73bd4
2 changed files with 17 additions and 7 deletions
|
@ -2,13 +2,19 @@ package dev.msfjarvis.claw.android.paging
|
||||||
|
|
||||||
import androidx.paging.PagingSource
|
import androidx.paging.PagingSource
|
||||||
import androidx.paging.PagingState
|
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 dev.msfjarvis.claw.model.LobstersPost
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
class LobstersPagingSource(
|
class LobstersPagingSource
|
||||||
private val getMorePosts: suspend (Int) -> List<LobstersPost>,
|
@AssistedInject
|
||||||
private val ioDispatcher: CoroutineDispatcher,
|
constructor(
|
||||||
|
@Assisted private val getMorePosts: suspend (Int) -> List<LobstersPost>,
|
||||||
|
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
) : PagingSource<Int, LobstersPost>() {
|
) : PagingSource<Int, LobstersPost>() {
|
||||||
|
|
||||||
override suspend fun load(params: LoadParams<Int>): LoadResult<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 {
|
override fun getRefreshKey(state: PagingState<Int, LobstersPost>): Int {
|
||||||
return state.pages.size + 1
|
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 api: LobstersApi,
|
||||||
private val savedPostsRepository: SavedPostsRepository,
|
private val savedPostsRepository: SavedPostsRepository,
|
||||||
private val postDetailsRepository: PostDetailsRepository,
|
private val postDetailsRepository: PostDetailsRepository,
|
||||||
|
private val pagingSourceFactory: LobstersPagingSource.Factory,
|
||||||
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
|
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
private var hottestPostsPagingSource: LobstersPagingSource? = null
|
private var hottestPostsPagingSource: LobstersPagingSource? = null
|
||||||
private var newestPostsPagingSource: LobstersPagingSource? = null
|
private var newestPostsPagingSource: LobstersPagingSource? = null
|
||||||
private val hottestPostsPager =
|
private val hottestPostsPager =
|
||||||
Pager(PagingConfig(20)) {
|
Pager(PagingConfig(20)) {
|
||||||
LobstersPagingSource(api::getHottestPosts, ioDispatcher).also {
|
pagingSourceFactory.create(api::getHottestPosts).also { hottestPostsPagingSource = it }
|
||||||
hottestPostsPagingSource = it
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
private val newestPostsPager =
|
private val newestPostsPager =
|
||||||
Pager(PagingConfig(20)) {
|
Pager(PagingConfig(20)) {
|
||||||
LobstersPagingSource(api::getNewestPosts, ioDispatcher).also { newestPostsPagingSource = it }
|
pagingSourceFactory.create(api::getHottestPosts).also { newestPostsPagingSource = it }
|
||||||
}
|
}
|
||||||
|
|
||||||
val hottestPosts
|
val hottestPosts
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue