Persist hottest list scroll state across navigation events

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-10-30 14:16:09 +05:30
parent 108a28384c
commit 41521cca95
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
2 changed files with 5 additions and 2 deletions

View file

@ -3,6 +3,7 @@ package dev.msfjarvis.lobsters
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Text import androidx.compose.foundation.Text
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.BottomNavigation import androidx.compose.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.Scaffold import androidx.compose.material.Scaffold
@ -85,10 +86,12 @@ fun LobstersApp() {
} }
}, },
) { ) {
val hottestPostsListState = rememberLazyListState()
NavHost(navController, startDestination = Destination.Hottest.route) { NavHost(navController, startDestination = Destination.Hottest.route) {
composable(Destination.Hottest.route) { composable(Destination.Hottest.route) {
HottestPosts( HottestPosts(
posts = hottestPosts, posts = hottestPosts,
listState = hottestPostsListState,
saveAction = viewModel::savePost, saveAction = viewModel::savePost,
overscrollAction = viewModel::getMorePosts, overscrollAction = viewModel::getMorePosts,
) )

View file

@ -2,7 +2,7 @@ package dev.msfjarvis.lobsters.ui
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumnForIndexed import androidx.compose.foundation.lazy.LazyColumnForIndexed
import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -12,11 +12,11 @@ import dev.msfjarvis.lobsters.urllauncher.UrlLauncherAmbient
@Composable @Composable
fun HottestPosts( fun HottestPosts(
posts: List<LobstersPost>, posts: List<LobstersPost>,
listState: LazyListState,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
saveAction: (LobstersPost) -> Unit, saveAction: (LobstersPost) -> Unit,
overscrollAction: () -> Unit, overscrollAction: () -> Unit,
) { ) {
val listState = rememberLazyListState()
val urlLauncher = UrlLauncherAmbient.current val urlLauncher = UrlLauncherAmbient.current
if (posts.isEmpty()) { if (posts.isEmpty()) {