android: wire DatabasePosts into the navigation flow

This commit is contained in:
Harsh Shandilya 2022-02-06 15:19:03 +05:30
parent 071a06ff56
commit cc6ca5e6b1
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
3 changed files with 28 additions and 11 deletions

View file

@ -5,6 +5,10 @@ sealed class Destinations(internal val route: String) {
fun getRoute() = route
}
object Saved : Destinations("saved") {
fun getRoute() = route
}
object Comments : Destinations("comments/%s") {
fun getRoute(postId: String) = route.format(postId)
}

View file

@ -6,6 +6,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@ -47,7 +48,8 @@ fun LobstersApp(
setWebUri: (String) -> Unit,
) {
val systemUiController = rememberSystemUiController()
val listState = rememberLazyListState()
val networkListState = rememberLazyListState()
val savedListState = rememberLazyListState()
val navController = rememberNavController()
// The destination needs to be tracked here rather than used directly since
// `NavController#currentDestination` is not a Composable state.
@ -103,14 +105,15 @@ fun LobstersApp(
systemUiController.setStatusBarColor(color = statusBarColor)
systemUiController.setNavigationBarColor(color = Color.Transparent)
}
val items = viewModel.pagerFlow.collectAsLazyPagingItems()
val networkPosts = viewModel.pagerFlow.collectAsLazyPagingItems()
val savedPosts by viewModel.savedPosts.collectAsState(emptyList())
Scaffold(
topBar = { ClawAppBar(modifier = Modifier.statusBarsPadding()) },
floatingActionButton = {
ClawFab(
isFabVisible = isFabVisible && currentDestination == Destinations.Hottest.getRoute(),
listState = listState,
listState = networkListState,
modifier = Modifier.navigationBarsPadding(),
)
},
@ -119,12 +122,21 @@ fun LobstersApp(
composable(Destinations.Hottest.getRoute()) {
setWebUri("https://lobste.rs/")
HottestPosts(
items,
listState,
viewModel::isPostSaved,
viewModel::reloadPosts,
postActions,
Modifier.nestedScroll(nestedScrollConnection),
items = networkPosts,
listState = networkListState,
isPostSaved = viewModel::isPostSaved,
reloadPosts = viewModel::reloadPosts,
postActions = postActions,
modifier = Modifier.nestedScroll(nestedScrollConnection),
)
}
composable(Destinations.Saved.getRoute()) {
DatabasePosts(
items = savedPosts,
listState = savedListState,
isSaved = viewModel::isPostSaved,
postActions = postActions,
modifier = Modifier.nestedScroll(nestedScrollConnection),
)
}
composable(Destinations.Comments.getRoute("{postId}")) { backStackEntry ->

View file

@ -11,7 +11,6 @@ import dev.msfjarvis.claw.database.local.SavedPost
import javax.inject.Inject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.mapLatest
@ -27,7 +26,6 @@ constructor(
private val repository: SavedPostsRepository,
) : ViewModel() {
private var lastPagingSource: LobstersPagingSource? = null
private val savedPosts = flow { repository.savedPosts.collect { emit(it) } }
private val pager =
Pager(PagingConfig(20)) {
LobstersPagingSource(api::getHottestPosts).also { lastPagingSource = it }
@ -36,6 +34,9 @@ constructor(
val pagerFlow
get() = pager.flow
val savedPosts
get() = repository.savedPosts
suspend fun isPostSaved(post: SavedPost) = savedPosts.mapLatest { posts -> post in posts }.first()
fun toggleSave(post: SavedPost) {