Fix ArticleList recomposition

This commit is contained in:
Ash 2022-04-20 17:44:46 +08:00
parent f873af28c8
commit 581c8243e3
3 changed files with 13 additions and 10 deletions

View File

@ -15,21 +15,22 @@ fun LazyListScope.ArticleList(
pagingItems: LazyPagingItems<FlowItemView>,
onClick: (ArticleWithFeed) -> Unit = {},
) {
for (itemIndex in 0 until pagingItems.itemCount) {
when (val item = pagingItems[itemIndex]) {
for (index in 0 until pagingItems.itemCount) {
when (val item = pagingItems.peek(index)) {
is FlowItemView.Article -> {
item {
item(key = item.articleWithFeed.article.id) {
ArticleItem(
articleWithFeed = item.articleWithFeed,
articleWithFeed = (pagingItems[index] as FlowItemView.Article).articleWithFeed,
) {
onClick(it)
}
}
}
is FlowItemView.Date -> {
if (itemIndex != 0) item { Spacer(modifier = Modifier.height(40.dp)) }
val separator = pagingItems[index] as FlowItemView.Date
if (separator.showSpacer) item { Spacer(modifier = Modifier.height(40.dp)) }
stickyHeader {
StickyHeader(item.date)
StickyHeader(separator.date)
}
}
else -> {}

View File

@ -6,6 +6,7 @@ import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.ArrowBack
import androidx.compose.material.icons.rounded.DoneAll
@ -63,6 +64,7 @@ fun FlowPage(
var onSearch by remember { mutableStateOf(false) }
val viewState = flowViewModel.viewState.collectAsStateValue()
val pagingItems = viewState.pagingData.collectAsLazyPagingItems()
val listState = if (pagingItems.itemCount > 0) viewState.listState else rememberLazyListState()
val owner = LocalLifecycleOwner.current
var isSyncing by remember { mutableStateOf(false) }
@ -176,7 +178,7 @@ fun FlowPage(
) {
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = viewState.listState,
state = listState,
) {
item {
DisplayTextHeader(filterState, isSyncing)

View File

@ -74,7 +74,7 @@ class FlowViewModel @Inject constructor(
val afterDate =
stringsRepository.formatAsString(after?.articleWithFeed?.article?.date)
if (beforeDate != afterDate) {
afterDate?.let { FlowItemView.Date(it) }
afterDate?.let { FlowItemView.Date(it, beforeDate != null) }
} else {
null
}
@ -102,7 +102,7 @@ class FlowViewModel @Inject constructor(
val afterDate =
stringsRepository.formatAsString(after?.articleWithFeed?.article?.date)
if (beforeDate != afterDate) {
afterDate?.let { FlowItemView.Date(it) }
afterDate?.let { FlowItemView.Date(it, beforeDate != null) }
} else {
null
}
@ -212,5 +212,5 @@ enum class MarkAsReadBefore {
sealed class FlowItemView {
class Article(val articleWithFeed: ArticleWithFeed) : FlowItemView()
class Date(val date: String) : FlowItemView()
class Date(val date: String, val showSpacer: Boolean) : FlowItemView()
}