Skip to content

Commit

Permalink
Use article count instead of filter to reset position
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp committed Dec 2, 2024
1 parent 3f31fff commit 1e57ee8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.paging.PagingData
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import com.capyreader.app.R
import com.capyreader.app.common.Media
Expand Down Expand Up @@ -115,8 +116,8 @@ fun ArticleLayout(
val coroutineScope = rememberCoroutineScope()
val scaffoldNavigator = rememberListDetailPaneScaffoldNavigator()
var isRefreshing by remember { mutableStateOf(false) }
val listState = rememberArticleListState(filter)
val pagingArticles = articles.collectAsLazyPagingItems(Dispatchers.IO)
val listState = rememberArticleListState(pagingArticles)
val snackbarHost = remember { SnackbarHostState() }
val addFeedSuccessMessage = stringResource(R.string.add_feed_success)
val currentFeed = findCurrentFeed(filter, allFeeds)
Expand Down Expand Up @@ -468,8 +469,8 @@ fun findCurrentFeed(filter: ArticleFilter, feeds: List<Feed>): Feed? {


@Composable
fun rememberArticleListState(filter: ArticleFilter): LazyListState {
return rememberSaveable(filter, saver = LazyListState.Saver) {
fun rememberArticleListState(articles: LazyPagingItems<Article>): LazyListState {
return rememberSaveable(articles.itemCount, saver = LazyListState.Saver) {
LazyListState()
}
}
Expand Down
43 changes: 23 additions & 20 deletions app/src/main/java/com/capyreader/app/ui/articles/ArticleList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
Expand Down Expand Up @@ -44,26 +45,28 @@ fun ArticleList(
}
}

LazyScrollbar(state = listState) {
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
) {
items(count = articles.itemCount) { index ->
val item = articles[index]

Box {
if (item == null) {
PlaceholderArticleRow(articleOptions.imagePreview)
} else {
ArticleRow(
article = item,
selected = selectedArticleKey == item.id,
onSelect = { selectArticle(it) },
onMarkAllRead = onMarkAllRead,
currentTime = currentTime,
options = articleOptions
)
key(articles.itemCount) {
LazyScrollbar(state = listState) {
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
) {
items(count = articles.itemCount) { index ->
val item = articles[index]

Box {
if (item == null) {
PlaceholderArticleRow(articleOptions.imagePreview)
} else {
ArticleRow(
article = item,
selected = selectedArticleKey == item.id,
onSelect = { selectArticle(it) },
onMarkAllRead = onMarkAllRead,
currentTime = currentTime,
options = articleOptions
)
}
}
}
}
Expand Down

0 comments on commit 1e57ee8

Please sign in to comment.