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

View file

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