app: add view comments navigation action
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
@@ -15,14 +15,17 @@ import androidx.compose.ui.Modifier
|
|||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.platform.testTag
|
import androidx.compose.ui.platform.testTag
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
|
import androidx.navigation.NavType
|
||||||
import androidx.navigation.compose.KEY_ROUTE
|
import androidx.navigation.compose.KEY_ROUTE
|
||||||
import androidx.navigation.compose.NavHost
|
import androidx.navigation.compose.NavHost
|
||||||
import androidx.navigation.compose.composable
|
import androidx.navigation.compose.composable
|
||||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||||
|
import androidx.navigation.compose.navArgument
|
||||||
import androidx.navigation.compose.navigate
|
import androidx.navigation.compose.navigate
|
||||||
import androidx.navigation.compose.rememberNavController
|
import androidx.navigation.compose.rememberNavController
|
||||||
import androidx.paging.LoadState
|
import androidx.paging.LoadState
|
||||||
import androidx.paging.compose.collectAsLazyPagingItems
|
import androidx.paging.compose.collectAsLazyPagingItems
|
||||||
|
import dev.msfjarvis.lobsters.ui.comments.CommentsPage
|
||||||
import dev.msfjarvis.lobsters.ui.navigation.Destination
|
import dev.msfjarvis.lobsters.ui.navigation.Destination
|
||||||
import dev.msfjarvis.lobsters.ui.posts.NetworkPosts
|
import dev.msfjarvis.lobsters.ui.posts.NetworkPosts
|
||||||
import dev.msfjarvis.lobsters.ui.posts.SavedPosts
|
import dev.msfjarvis.lobsters.ui.posts.SavedPosts
|
||||||
@@ -49,9 +52,7 @@ fun LobstersApp() {
|
|||||||
navBackStackEntry?.arguments?.getString(KEY_ROUTE) ?: Destination.startDestination.route
|
navBackStackEntry?.arguments?.getString(KEY_ROUTE) ?: Destination.startDestination.route
|
||||||
val currentDestination = Destination.getDestinationFromRoute(currentRoute)
|
val currentDestination = Destination.getDestinationFromRoute(currentRoute)
|
||||||
val navigateToDestination: (destination: Destination) -> Unit = { destination ->
|
val navigateToDestination: (destination: Destination) -> Unit = { destination ->
|
||||||
navController.navigate(destination.route) {
|
navController.navigate(destination.route) { launchSingleTop = true }
|
||||||
launchSingleTop = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
val jumpToIndex: suspend (Int, Destination) -> Unit = { index, screen ->
|
val jumpToIndex: suspend (Int, Destination) -> Unit = { index, screen ->
|
||||||
when (screen) {
|
when (screen) {
|
||||||
@@ -93,6 +94,7 @@ fun LobstersApp() {
|
|||||||
isPostSaved = viewModel::isPostSaved,
|
isPostSaved = viewModel::isPostSaved,
|
||||||
saveAction = viewModel::toggleSave,
|
saveAction = viewModel::toggleSave,
|
||||||
refreshAction = viewModel::reloadHottestPosts,
|
refreshAction = viewModel::reloadHottestPosts,
|
||||||
|
viewComments = { navController.navigate("comments/$it") },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
composable(Destination.Newest.route) {
|
composable(Destination.Newest.route) {
|
||||||
@@ -103,6 +105,7 @@ fun LobstersApp() {
|
|||||||
isPostSaved = viewModel::isPostSaved,
|
isPostSaved = viewModel::isPostSaved,
|
||||||
saveAction = viewModel::toggleSave,
|
saveAction = viewModel::toggleSave,
|
||||||
refreshAction = viewModel::reloadNewestPosts,
|
refreshAction = viewModel::reloadNewestPosts,
|
||||||
|
viewComments = { navController.navigate("comments/$it") },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
composable(Destination.Saved.route) {
|
composable(Destination.Saved.route) {
|
||||||
@@ -111,6 +114,16 @@ fun LobstersApp() {
|
|||||||
saveAction = viewModel::toggleSave,
|
saveAction = viewModel::toggleSave,
|
||||||
modifier = Modifier.padding(bottom = innerPadding.calculateBottomPadding()),
|
modifier = Modifier.padding(bottom = innerPadding.calculateBottomPadding()),
|
||||||
sortReversed = viewModel.getSortOrder(),
|
sortReversed = viewModel.getSortOrder(),
|
||||||
|
viewComments = { navController.navigate("comments/$it") },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
composable(
|
||||||
|
Destination.Comments.route,
|
||||||
|
listOf(navArgument("postId") { type = NavType.StringType }),
|
||||||
|
) { backStackEntry ->
|
||||||
|
CommentsPage(
|
||||||
|
postId = requireNotNull(backStackEntry.arguments?.getString("postId")),
|
||||||
|
getDetails = viewModel::getPostDetails,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,7 +138,7 @@ fun LobstersBottomNav(
|
|||||||
) {
|
) {
|
||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
BottomNavigation(modifier = Modifier.testTag("LobstersBottomNav")) {
|
BottomNavigation(modifier = Modifier.testTag("LobstersBottomNav")) {
|
||||||
Destination.values().forEach { screen ->
|
Destination.values().filter { it.bottombar }.forEach { screen ->
|
||||||
BottomNavigationItem(
|
BottomNavigationItem(
|
||||||
icon = {
|
icon = {
|
||||||
IconResource(
|
IconResource(
|
||||||
|
|||||||
@@ -9,10 +9,12 @@ enum class Destination(
|
|||||||
val route: String,
|
val route: String,
|
||||||
val labelRes: Strings,
|
val labelRes: Strings,
|
||||||
@DrawableRes val badgeRes: Int,
|
@DrawableRes val badgeRes: Int,
|
||||||
|
val bottombar: Boolean = true,
|
||||||
) {
|
) {
|
||||||
Hottest("hottest", Strings.HottestPosts, R.drawable.ic_whatshot_24px),
|
Hottest("hottest", Strings.HottestPosts, R.drawable.ic_whatshot_24px),
|
||||||
Newest("newest", Strings.NewestPosts, R.drawable.ic_schedule_black_24dp),
|
Newest("newest", Strings.NewestPosts, R.drawable.ic_schedule_black_24dp),
|
||||||
Saved("saved", Strings.SavedPosts, R.drawable.ic_favorite_24px),
|
Saved("saved", Strings.SavedPosts, R.drawable.ic_favorite_24px),
|
||||||
|
Comments("comments/{postId}", Strings.OpenComments, -1, false),
|
||||||
;
|
;
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ fun LobstersItem(
|
|||||||
post: SavedPost,
|
post: SavedPost,
|
||||||
isSaved: Boolean,
|
isSaved: Boolean,
|
||||||
viewPost: () -> Unit,
|
viewPost: () -> Unit,
|
||||||
viewComments: () -> Unit,
|
viewComments: (String) -> Unit,
|
||||||
toggleSave: () -> Unit,
|
toggleSave: () -> Unit,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
@@ -87,7 +87,7 @@ fun LobstersItem(
|
|||||||
modifier = Modifier.width(8.dp),
|
modifier = Modifier.width(8.dp),
|
||||||
)
|
)
|
||||||
CommentsButton(
|
CommentsButton(
|
||||||
onClick = viewComments,
|
onClick = { viewComments(post.shortId) },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
SubmitterName(
|
SubmitterName(
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ fun NetworkPosts(
|
|||||||
isPostSaved: (String) -> Boolean,
|
isPostSaved: (String) -> Boolean,
|
||||||
saveAction: (SavedPost) -> Unit,
|
saveAction: (SavedPost) -> Unit,
|
||||||
refreshAction: () -> Unit,
|
refreshAction: () -> Unit,
|
||||||
|
viewComments: (String) -> Unit,
|
||||||
) {
|
) {
|
||||||
val urlLauncher = LocalUrlLauncher.current
|
val urlLauncher = LocalUrlLauncher.current
|
||||||
var isRefreshing by mutableStateOf(false)
|
var isRefreshing by mutableStateOf(false)
|
||||||
@@ -55,7 +56,7 @@ fun NetworkPosts(
|
|||||||
post = item,
|
post = item,
|
||||||
isSaved = isSaved,
|
isSaved = isSaved,
|
||||||
viewPost = { urlLauncher.launch(item.url.ifEmpty { item.commentsUrl }) },
|
viewPost = { urlLauncher.launch(item.url.ifEmpty { item.commentsUrl }) },
|
||||||
viewComments = { urlLauncher.launch(item.commentsUrl) },
|
viewComments = viewComments,
|
||||||
toggleSave = {
|
toggleSave = {
|
||||||
isSaved = isSaved.not()
|
isSaved = isSaved.not()
|
||||||
saveAction.invoke(item)
|
saveAction.invoke(item)
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ fun SavedPosts(
|
|||||||
sortReversed: Flow<Boolean>,
|
sortReversed: Flow<Boolean>,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
saveAction: (SavedPost) -> Unit,
|
saveAction: (SavedPost) -> Unit,
|
||||||
|
viewComments: (String) -> Unit,
|
||||||
) {
|
) {
|
||||||
val listState = rememberLazyListState()
|
val listState = rememberLazyListState()
|
||||||
val urlLauncher = LocalUrlLauncher.current
|
val urlLauncher = LocalUrlLauncher.current
|
||||||
@@ -71,7 +72,7 @@ fun SavedPosts(
|
|||||||
post = item,
|
post = item,
|
||||||
isSaved = true,
|
isSaved = true,
|
||||||
viewPost = { urlLauncher.launch(item.url.ifEmpty { item.commentsUrl }) },
|
viewPost = { urlLauncher.launch(item.url.ifEmpty { item.commentsUrl }) },
|
||||||
viewComments = { urlLauncher.launch(item.commentsUrl) },
|
viewComments = viewComments,
|
||||||
toggleSave = { saveAction.invoke(item) },
|
toggleSave = { saveAction.invoke(item) },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user