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,
|
setWebUri: (String?) -> Unit,
|
||||||
) {
|
) {
|
||||||
val systemUiController = rememberSystemUiController()
|
val systemUiController = rememberSystemUiController()
|
||||||
val networkListState = rememberLazyListState()
|
val hottestListState = rememberLazyListState()
|
||||||
|
val newestListState = rememberLazyListState()
|
||||||
val savedListState = rememberLazyListState()
|
val savedListState = rememberLazyListState()
|
||||||
val navController = rememberNavController()
|
val navController = rememberNavController()
|
||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
val postActions = rememberPostActions(urlLauncher, navController, viewModel)
|
val postActions = rememberPostActions(urlLauncher, navController, viewModel)
|
||||||
val currentDestination by currentNavigationDestination(navController)
|
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())
|
val savedPosts by viewModel.savedPosts.collectAsState(emptyList())
|
||||||
|
|
||||||
LobstersTheme(
|
LobstersTheme(
|
||||||
|
@ -85,7 +87,12 @@ fun LobstersApp(
|
||||||
label = "Hottest",
|
label = "Hottest",
|
||||||
route = Destinations.Hottest.getRoute(),
|
route = Destinations.Hottest.getRoute(),
|
||||||
icon = ClawIcons.Flame,
|
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(
|
NavigationItem(
|
||||||
label = "Saved",
|
label = "Saved",
|
||||||
route = Destinations.Saved.getRoute(),
|
route = Destinations.Saved.getRoute(),
|
||||||
|
@ -147,10 +154,23 @@ fun LobstersApp(
|
||||||
) {
|
) {
|
||||||
setWebUri("https://lobste.rs/")
|
setWebUri("https://lobste.rs/")
|
||||||
NetworkPosts(
|
NetworkPosts(
|
||||||
items = networkPosts,
|
items = hottestPosts,
|
||||||
listState = networkListState,
|
listState = hottestListState,
|
||||||
isPostSaved = viewModel::isPostSaved,
|
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,
|
postActions = postActions,
|
||||||
modifier = Modifier.padding(bottom = paddingValues.calculateBottomPadding()),
|
modifier = Modifier.padding(bottom = paddingValues.calculateBottomPadding()),
|
||||||
)
|
)
|
||||||
|
|
|
@ -5,6 +5,10 @@ sealed class Destinations(internal val route: String) {
|
||||||
fun getRoute() = route
|
fun getRoute() = route
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object Newest : Destinations("newest") {
|
||||||
|
fun getRoute() = route
|
||||||
|
}
|
||||||
|
|
||||||
object Saved : Destinations("saved") {
|
object Saved : Destinations("saved") {
|
||||||
fun getRoute() = route
|
fun getRoute() = route
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,14 +24,22 @@ constructor(
|
||||||
private val api: LobstersApi,
|
private val api: LobstersApi,
|
||||||
private val repository: SavedPostsRepository,
|
private val repository: SavedPostsRepository,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
private var lastPagingSource: LobstersPagingSource? = null
|
private var hottestPostsPagingSource: LobstersPagingSource? = null
|
||||||
private val pager =
|
private var newestPostsPagingSource: LobstersPagingSource? = null
|
||||||
|
private val hottestPostsPager =
|
||||||
Pager(PagingConfig(20)) {
|
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
|
val newestPosts
|
||||||
get() = pager.flow
|
get() = newestPostsPager.flow
|
||||||
|
|
||||||
val savedPosts
|
val savedPosts
|
||||||
get() = repository.savedPosts
|
get() = repository.savedPosts
|
||||||
|
@ -57,7 +65,11 @@ constructor(
|
||||||
suspend fun getPostComments(postId: String) =
|
suspend fun getPostComments(postId: String) =
|
||||||
withContext(Dispatchers.IO) { api.getPostDetails(postId) }
|
withContext(Dispatchers.IO) { api.getPostDetails(postId) }
|
||||||
|
|
||||||
fun reloadPosts() {
|
fun refreshHottestPosts() {
|
||||||
lastPagingSource?.invalidate()
|
hottestPostsPagingSource?.invalidate()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun refreshNewestPosts() {
|
||||||
|
newestPostsPagingSource?.invalidate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue