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>, pagingItems: LazyPagingItems<FlowItemView>,
onClick: (ArticleWithFeed) -> Unit = {}, onClick: (ArticleWithFeed) -> Unit = {},
) { ) {
for (itemIndex in 0 until pagingItems.itemCount) { for (index in 0 until pagingItems.itemCount) {
when (val item = pagingItems[itemIndex]) { when (val item = pagingItems.peek(index)) {
is FlowItemView.Article -> { is FlowItemView.Article -> {
item { item(key = item.articleWithFeed.article.id) {
ArticleItem( ArticleItem(
articleWithFeed = item.articleWithFeed, articleWithFeed = (pagingItems[index] as FlowItemView.Article).articleWithFeed,
) { ) {
onClick(it) onClick(it)
} }
} }
} }
is FlowItemView.Date -> { 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 {
StickyHeader(item.date) StickyHeader(separator.date)
} }
} }
else -> {} else -> {}

View File

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

View File

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