mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 10:37:05 +05:30
android: add entry for newest posts
This commit is contained in:
parent
132898e3ca
commit
08e97807dd
3 changed files with 49 additions and 13 deletions
|
@ -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()),
|
||||
)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue