Skip to content

Commit 74176e3

Browse files
committed
fix: use item key for mark as read on scroll
1 parent 70c0f0e commit 74176e3

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

app/src/main/java/me/ash/reader/ui/page/home/flow/ArticleList.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ private fun key(item: ArticleFlowItem): String {
119119

120120
private fun contentType(item: ArticleFlowItem): Int {
121121
return when (item) {
122-
is ArticleFlowItem.Article -> ARTICLE
123-
is ArticleFlowItem.Date -> DATE
122+
is ArticleFlowItem.Article -> CONTENT_TYPE_ARTICLE
123+
is ArticleFlowItem.Date -> CONTENT_TYPE_DATE_HEADER
124124
}
125125
}
126126

127-
private const val ARTICLE = 1
128-
private const val DATE = 2
127+
const val CONTENT_TYPE_ARTICLE = 1
128+
const val CONTENT_TYPE_DATE_HEADER = 2

app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ import androidx.compose.ui.text.font.FontWeight
5959
import androidx.compose.ui.text.style.TextOverflow
6060
import androidx.compose.ui.unit.dp
6161
import androidx.compose.ui.unit.sp
62-
import androidx.compose.ui.util.fastForEachIndexed
6362
import androidx.compose.ui.zIndex
6463
import androidx.paging.compose.LazyPagingItems
6564
import androidx.paging.compose.collectAsLazyPagingItems
@@ -518,15 +517,25 @@ fun FlowPage(
518517
if (markAsReadOnScroll && filterState.filter.isUnread()) {
519518
LaunchedEffect(listState.isScrollInProgress) {
520519
if (!listState.isScrollInProgress) {
521-
val firstItemIndex = listState.firstVisibleItemIndex
520+
val firstItemKey =
521+
listState.layoutInfo.visibleItemsInfo
522+
.firstOrNull { it.contentType == CONTENT_TYPE_ARTICLE }
523+
?.key
522524
val items = mutableListOf<ArticleWithFeed>()
523-
if (firstItemIndex < pagingItems.itemCount) {
524-
for (index in 0 until firstItemIndex) {
525-
pagingItems.peek(index).let {
526-
if (it is ArticleFlowItem.Article)
527-
items.add(it.articleWithFeed)
525+
var found = false
526+
val itemCount = pagingItems.itemCount
527+
for (index in 0 until itemCount) {
528+
pagingItems.peek(index).let {
529+
if (it is ArticleFlowItem.Article) {
530+
items.add(it.articleWithFeed)
531+
if (it.articleWithFeed.article.id == firstItemKey) {
532+
found = true
533+
break
534+
}
528535
}
529536
}
537+
}
538+
if (items.isNotEmpty() && found) {
530539
viewModel.diffMapHolder.updateDiff(
531540
articleWithFeed = items.toTypedArray(),
532541
isUnread = false,

0 commit comments

Comments
 (0)