mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-17 22:37:03 +05:30
app: start qualifying infra for hottest posts
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
b9c253733e
commit
e3c5097035
4 changed files with 12 additions and 11 deletions
|
@ -5,7 +5,7 @@ import androidx.paging.PagingState
|
|||
import dev.msfjarvis.lobsters.data.repo.LobstersRepository
|
||||
import dev.msfjarvis.lobsters.model.LobstersPost
|
||||
|
||||
class LobstersPagingSource constructor(
|
||||
class HottestPostsPagingSource constructor(
|
||||
private val lobstersRepository: LobstersRepository,
|
||||
) : PagingSource<Int, LobstersPost>() {
|
||||
|
||||
|
@ -15,7 +15,7 @@ class LobstersPagingSource constructor(
|
|||
// Update cache before fetching a list.
|
||||
// This is done to make sure that we can update the isSaved status of incoming posts.
|
||||
lobstersRepository.updateCache()
|
||||
val posts = lobstersRepository.fetchPosts(page)
|
||||
val posts = lobstersRepository.fetchHottestPosts(page)
|
||||
|
||||
LoadResult.Page(
|
||||
data = posts,
|
|
@ -26,7 +26,7 @@ class LobstersRepository constructor(
|
|||
return savedPostsCache.values.toList()
|
||||
}
|
||||
|
||||
suspend fun fetchPosts(page: Int): List<LobstersPost> = withContext(Dispatchers.IO) {
|
||||
suspend fun fetchHottestPosts(page: Int): List<LobstersPost> = withContext(Dispatchers.IO) {
|
||||
return@withContext lobstersApi.getHottestPosts(page)
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,8 @@ import kotlinx.coroutines.launch
|
|||
fun LobstersApp() {
|
||||
val viewModel: LobstersViewModel = viewModel()
|
||||
val navController = rememberNavController()
|
||||
val hottestPosts = viewModel.posts.collectAsLazyPagingItems()
|
||||
val hottestPosts = viewModel.hottestPosts.collectAsLazyPagingItems()
|
||||
|
||||
val savedPosts by viewModel.savedPosts.collectAsState()
|
||||
val hottestPostsListState = rememberLazyListState()
|
||||
|
||||
|
@ -76,7 +77,7 @@ fun LobstersApp() {
|
|||
modifier = Modifier.padding(bottom = innerPadding.calculateBottomPadding()),
|
||||
isPostSaved = viewModel::isPostSaved,
|
||||
saveAction = viewModel::toggleSave,
|
||||
refreshAction = viewModel::reloadPosts,
|
||||
refreshAction = viewModel::reloadHottestPosts,
|
||||
)
|
||||
}
|
||||
composable(Destination.Saved.route) {
|
||||
|
|
|
@ -8,7 +8,7 @@ import androidx.paging.cachedIn
|
|||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import dev.msfjarvis.lobsters.data.local.SavedPost
|
||||
import dev.msfjarvis.lobsters.data.preferences.ClawPreferences
|
||||
import dev.msfjarvis.lobsters.data.remote.LobstersPagingSource
|
||||
import dev.msfjarvis.lobsters.data.remote.HottestPostsPagingSource
|
||||
import dev.msfjarvis.lobsters.data.repo.LobstersRepository
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
@ -25,10 +25,10 @@ class LobstersViewModel @Inject constructor(
|
|||
) : ViewModel() {
|
||||
private val _savedPosts = MutableStateFlow<List<SavedPost>>(emptyList())
|
||||
val savedPosts = _savedPosts.asStateFlow()
|
||||
val posts = Pager(PagingConfig(25)) {
|
||||
LobstersPagingSource(lobstersRepository).also { pagingSource = it }
|
||||
val hottestPosts = Pager(PagingConfig(25)) {
|
||||
HottestPostsPagingSource(lobstersRepository).also { hottestPostsPagingSource = it }
|
||||
}.flow.cachedIn(viewModelScope)
|
||||
private var pagingSource: LobstersPagingSource? = null
|
||||
private var hottestPostsPagingSource: HottestPostsPagingSource? = null
|
||||
|
||||
init {
|
||||
lobstersRepository.isCacheReady.onEach { ready ->
|
||||
|
@ -46,8 +46,8 @@ class LobstersViewModel @Inject constructor(
|
|||
clawPreferences.toggleSortingOrder()
|
||||
}
|
||||
|
||||
fun reloadPosts() {
|
||||
pagingSource?.invalidate()
|
||||
fun reloadHottestPosts() {
|
||||
hottestPostsPagingSource?.invalidate()
|
||||
}
|
||||
|
||||
fun toggleSave(post: SavedPost) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue