From ac1ca1412d7839e060df23faea0321fde624f39c Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 8 Jul 2024 19:27:57 +0530 Subject: [PATCH] Improve comment loading smoothness --- .../schabi/newpipe/compose/comment/CommentSection.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/compose/comment/CommentSection.kt b/app/src/main/java/org/schabi/newpipe/compose/comment/CommentSection.kt index a26bab8b7..1e32e14dd 100644 --- a/app/src/main/java/org/schabi/newpipe/compose/comment/CommentSection.kt +++ b/app/src/main/java/org/schabi/newpipe/compose/comment/CommentSection.kt @@ -10,6 +10,9 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.derivedStateOf +import androidx.compose.runtime.getValue +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource @@ -39,7 +42,7 @@ fun CommentSection( ) { Surface(color = MaterialTheme.colorScheme.background) { val comments = commentsFlow.collectAsLazyPagingItems() - val refresh = comments.loadState.refresh + val itemCount by remember { derivedStateOf { comments.itemCount } } val listState = rememberLazyListState() LazyColumnScrollbar(state = listState) { @@ -51,8 +54,9 @@ fun CommentSection( } } - if (comments.itemCount == 0) { + if (itemCount == 0) { item { + val refresh = comments.loadState.refresh if (refresh is LoadState.Loading) { LoadingIndicator(modifier = Modifier.padding(top = 8.dp)) } else { @@ -60,7 +64,7 @@ fun CommentSection( } } } else { - items(comments.itemCount) { + items(itemCount) { Comment(comment = comments[it]!!) } }