app: add newest posts screen

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2021-03-29 11:57:52 +05:30
parent fcd27863a1
commit 79b03a4907
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
7 changed files with 43 additions and 6 deletions

View file

@ -34,9 +34,11 @@ fun LobstersApp() {
val viewModel: LobstersViewModel = viewModel()
val navController = rememberNavController()
val hottestPosts = viewModel.hottestPosts.collectAsLazyPagingItems()
val newestPosts = viewModel.newestPosts.collectAsLazyPagingItems()
val savedPosts by viewModel.savedPosts.collectAsState()
val hottestPostsListState = rememberLazyListState()
val newestPostsListState = rememberLazyListState()
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute =
@ -48,9 +50,20 @@ fun LobstersApp() {
popUpTo(navController.graph.startDestination) { inclusive = false }
}
}
val jumpToIndex: suspend (Int) -> Unit = {
if (hottestPosts.loadState.refresh != LoadState.Loading) {
hottestPostsListState.animateScrollToItem(it)
val jumpToIndex: suspend (Int, Destination) -> Unit = { index, screen ->
when (screen) {
Destination.Hottest -> {
if (hottestPosts.loadState.refresh != LoadState.Loading) {
hottestPostsListState.animateScrollToItem(index)
}
}
Destination.Newest -> {
if (newestPosts.loadState.refresh != LoadState.Loading) {
newestPostsListState.animateScrollToItem(index)
}
}
else -> {
}
}
}
@ -80,6 +93,16 @@ fun LobstersApp() {
refreshAction = viewModel::reloadHottestPosts,
)
}
composable(Destination.Newest.route) {
NetworkPosts(
posts = newestPosts,
listState = newestPostsListState,
modifier = Modifier.padding(bottom = innerPadding.calculateBottomPadding()),
isPostSaved = viewModel::isPostSaved,
saveAction = viewModel::toggleSave,
refreshAction = viewModel::reloadNewestPosts,
)
}
composable(Destination.Saved.route) {
SavedPosts(
posts = savedPosts,
@ -96,7 +119,7 @@ fun LobstersApp() {
fun LobstersBottomNav(
currentDestination: Destination,
navigateToDestination: (destination: Destination) -> Unit,
jumpToIndex: suspend (index: Int) -> Unit,
jumpToIndex: suspend (index: Int, screen: Destination) -> Unit,
) {
val coroutineScope = rememberCoroutineScope()
BottomNavigation(modifier = Modifier.testTag("LobstersBottomNav")) {
@ -115,8 +138,8 @@ fun LobstersBottomNav(
onClick = {
if (screen != currentDestination) {
navigateToDestination(screen)
} else if (screen.route == Destination.Hottest.route) {
coroutineScope.launch { jumpToIndex(0) }
} else if (screen.route != Destination.Saved.route) {
coroutineScope.launch { jumpToIndex(0, screen) }
}
}
)

View file

@ -13,6 +13,7 @@ enum class Destination(
@DrawableRes val badgeRes: Int,
) {
Hottest("hottest", Strings.HottestPosts, R.drawable.ic_whatshot_24px),
Newest("newest", Strings.NewestPosts, R.drawable.ic_schedule_black_24dp),
Saved("saved", Strings.SavedPosts, R.drawable.ic_favorite_24px),
;

View file

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8zM11.78,7h-0.06c-0.4,0 -0.72,0.32 -0.72,0.72v4.72c0,0.35 0.18,0.68 0.49,0.86l4.15,2.49c0.34,0.2 0.78,0.1 0.98,-0.24 0.21,-0.34 0.1,-0.79 -0.25,-0.99l-3.87,-2.3L12.5,7.72c0,-0.4 -0.32,-0.72 -0.72,-0.72z" />
</vector>

View file

@ -17,6 +17,7 @@ private fun stringEnumMapper(stringEnum: Strings): Int {
Strings.RemoveFromSavedPosts -> R.string.remove_from_saved_posts
Strings.SavedPosts -> R.string.saved_posts
Strings.SubmittedBy -> R.string.submitted_by
Strings.NewestPosts -> R.string.newest_posts
}
}

View file

@ -10,4 +10,5 @@
<string name="refresh_posts_content_description">Refresh posts</string>
<string name="open_comments">Open comments</string>
<string name="change_sorting_order">Change sort order</string>
<string name="newest_posts">Newest</string>
</resources>

View file

@ -12,5 +12,6 @@ enum class Strings {
RemoveFromSavedPosts,
SavedPosts,
SubmittedBy,
NewestPosts,
;
}

View file

@ -15,6 +15,7 @@ private fun stringEnumMapper(stringEnum: Strings): String {
Strings.RemoveFromSavedPosts -> "Remove from saved posts"
Strings.SavedPosts -> "Saved"
Strings.SubmittedBy -> "submitted by %1s"
Strings.NewestPosts -> "Newest"
}
}