app: start qualifying infra for hottest posts

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2021-03-29 11:43:16 +05:30
parent b9c253733e
commit e3c5097035
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
4 changed files with 12 additions and 11 deletions

View file

@ -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,

View file

@ -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)
}

View file

@ -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) {

View file

@ -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) {