android: add entry for newest posts

This commit is contained in:
Harsh Shandilya 2022-04-22 20:34:29 +05:30
parent 132898e3ca
commit 08e97807dd
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
3 changed files with 49 additions and 13 deletions

View file

@ -58,14 +58,16 @@ fun LobstersApp(
setWebUri: (String?) -> Unit,
) {
val systemUiController = rememberSystemUiController()
val networkListState = rememberLazyListState()
val hottestListState = rememberLazyListState()
val newestListState = rememberLazyListState()
val savedListState = rememberLazyListState()
val navController = rememberNavController()
val coroutineScope = rememberCoroutineScope()
val postActions = rememberPostActions(urlLauncher, navController, viewModel)
val currentDestination by currentNavigationDestination(navController)
val networkPosts = viewModel.pagerFlow.collectAsLazyPagingItems()
val hottestPosts = viewModel.hottestPosts.collectAsLazyPagingItems()
val newestPosts = viewModel.newestPosts.collectAsLazyPagingItems()
val savedPosts by viewModel.savedPosts.collectAsState(emptyList())
LobstersTheme(
@ -85,7 +87,12 @@ fun LobstersApp(
label = "Hottest",
route = Destinations.Hottest.getRoute(),
icon = ClawIcons.Flame,
) { coroutineScope.launch { networkListState.animateScrollToItem(index = 0) } },
) { coroutineScope.launch { hottestListState.animateScrollToItem(index = 0) } },
NavigationItem(
label = "Newest",
route = Destinations.Newest.getRoute(),
icon = ClawIcons.New,
) { coroutineScope.launch { newestListState.animateScrollToItem(index = 0) } },
NavigationItem(
label = "Saved",
route = Destinations.Saved.getRoute(),
@ -147,10 +154,23 @@ fun LobstersApp(
) {
setWebUri("https://lobste.rs/")
NetworkPosts(
items = networkPosts,
listState = networkListState,
items = hottestPosts,
listState = hottestListState,
isPostSaved = viewModel::isPostSaved,
reloadPosts = viewModel::reloadPosts,
reloadPosts = viewModel::refreshHottestPosts,
postActions = postActions,
modifier = Modifier.padding(bottom = paddingValues.calculateBottomPadding()),
)
}
composable(
route = Destinations.Newest.getRoute(),
) {
setWebUri("https://lobste.rs/")
NetworkPosts(
items = newestPosts,
listState = newestListState,
isPostSaved = viewModel::isPostSaved,
reloadPosts = viewModel::refreshNewestPosts,
postActions = postActions,
modifier = Modifier.padding(bottom = paddingValues.calculateBottomPadding()),
)

View file

@ -5,6 +5,10 @@ sealed class Destinations(internal val route: String) {
fun getRoute() = route
}
object Newest : Destinations("newest") {
fun getRoute() = route
}
object Saved : Destinations("saved") {
fun getRoute() = route
}

View file

@ -24,14 +24,22 @@ constructor(
private val api: LobstersApi,
private val repository: SavedPostsRepository,
) : ViewModel() {
private var lastPagingSource: LobstersPagingSource? = null
private val pager =
private var hottestPostsPagingSource: LobstersPagingSource? = null
private var newestPostsPagingSource: LobstersPagingSource? = null
private val hottestPostsPager =
Pager(PagingConfig(20)) {
LobstersPagingSource(api::getHottestPosts).also { lastPagingSource = it }
LobstersPagingSource(api::getHottestPosts).also { hottestPostsPagingSource = it }
}
private val newestPostsPager =
Pager(PagingConfig(20)) {
LobstersPagingSource(api::getNewestPosts).also { newestPostsPagingSource = it }
}
val hottestPosts
get() = hottestPostsPager.flow
val pagerFlow
get() = pager.flow
val newestPosts
get() = newestPostsPager.flow
val savedPosts
get() = repository.savedPosts
@ -57,7 +65,11 @@ constructor(
suspend fun getPostComments(postId: String) =
withContext(Dispatchers.IO) { api.getPostDetails(postId) }
fun reloadPosts() {
lastPagingSource?.invalidate()
fun refreshHottestPosts() {
hottestPostsPagingSource?.invalidate()
}
fun refreshNewestPosts() {
newestPostsPagingSource?.invalidate()
}
}