mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-18 00:57:02 +05:30
Don't inject paging source
Co-authored-by: Aditya Wasan <adityawasan55@gmail.com> Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
4585757ac2
commit
83af1e1c53
2 changed files with 4 additions and 5 deletions
|
@ -4,9 +4,8 @@ import androidx.paging.PagingSource
|
|||
import androidx.paging.PagingState
|
||||
import dev.msfjarvis.lobsters.data.repo.LobstersRepository
|
||||
import dev.msfjarvis.lobsters.model.LobstersPost
|
||||
import javax.inject.Inject
|
||||
|
||||
class LobstersPagingSource @Inject constructor(
|
||||
class LobstersPagingSource constructor(
|
||||
private val lobstersRepository: LobstersRepository,
|
||||
) : PagingSource<Int, LobstersPost>() {
|
||||
|
||||
|
|
|
@ -19,13 +19,13 @@ import kotlinx.coroutines.launch
|
|||
@HiltViewModel
|
||||
class LobstersViewModel @Inject constructor(
|
||||
private val lobstersRepository: LobstersRepository,
|
||||
private val pagingSource: LobstersPagingSource,
|
||||
) : ViewModel() {
|
||||
private val _savedPosts = MutableStateFlow<List<SavedPost>>(emptyList())
|
||||
val savedPosts = _savedPosts.asStateFlow()
|
||||
val posts = Pager(PagingConfig(25)) {
|
||||
pagingSource
|
||||
LobstersPagingSource(lobstersRepository).also { pagingSource = it }
|
||||
}.flow.cachedIn(viewModelScope)
|
||||
private var pagingSource: LobstersPagingSource? = null
|
||||
|
||||
init {
|
||||
lobstersRepository.isCacheReady.onEach { ready ->
|
||||
|
@ -36,7 +36,7 @@ class LobstersViewModel @Inject constructor(
|
|||
}
|
||||
|
||||
fun reloadPosts() {
|
||||
pagingSource.invalidate()
|
||||
pagingSource?.invalidate()
|
||||
}
|
||||
|
||||
fun toggleSave(post: SavedPost) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue