Skip to content

Commit 1009dc4

Browse files
Added loading indicator
1 parent 42cb914 commit 1009dc4

2 files changed

Lines changed: 40 additions & 19 deletions

File tree

app/src/main/java/org/schabi/newpipe/compose/comment/CommentSection.kt

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ package org.schabi.newpipe.compose.comment
22

33
import android.content.res.Configuration
44
import androidx.compose.foundation.layout.Column
5+
import androidx.compose.foundation.layout.padding
56
import androidx.compose.foundation.lazy.LazyColumn
67
import androidx.compose.foundation.lazy.rememberLazyListState
78
import androidx.compose.material3.HorizontalDivider
89
import androidx.compose.material3.MaterialTheme
910
import androidx.compose.material3.Surface
1011
import androidx.compose.material3.Text
1112
import androidx.compose.runtime.Composable
12-
import androidx.compose.runtime.derivedStateOf
13-
import androidx.compose.runtime.getValue
14-
import androidx.compose.runtime.remember
1513
import androidx.compose.ui.Alignment
14+
import androidx.compose.ui.Modifier
1615
import androidx.compose.ui.res.stringResource
1716
import androidx.compose.ui.tooling.preview.Preview
1817
import androidx.compose.ui.tooling.preview.PreviewParameter
@@ -26,8 +25,8 @@ import androidx.paging.compose.collectAsLazyPagingItems
2625
import kotlinx.coroutines.flow.Flow
2726
import kotlinx.coroutines.flow.flowOf
2827
import my.nanihadesuka.compose.LazyColumnScrollbar
29-
import my.nanihadesuka.compose.ScrollbarSettings
3028
import org.schabi.newpipe.R
29+
import org.schabi.newpipe.compose.status.LoadingIndicator
3130
import org.schabi.newpipe.compose.theme.AppTheme
3231
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
3332
import org.schabi.newpipe.extractor.stream.Description
@@ -38,26 +37,30 @@ fun CommentSection(
3837
parentComment: CommentsInfoItem? = null,
3938
commentsFlow: Flow<PagingData<CommentsInfoItem>>
4039
) {
41-
val comments = commentsFlow.collectAsLazyPagingItems()
42-
val itemCount by remember { derivedStateOf { comments.itemCount } }
43-
4440
Surface(color = MaterialTheme.colorScheme.background) {
41+
val comments = commentsFlow.collectAsLazyPagingItems()
4542
val refresh = comments.loadState.refresh
46-
if (itemCount == 0 && refresh !is LoadState.Loading) {
47-
NoCommentsMessage((refresh as? LoadState.Error)?.error)
48-
} else {
49-
val listState = rememberLazyListState()
43+
val listState = rememberLazyListState()
5044

51-
LazyColumnScrollbar(state = listState, settings = ScrollbarSettings.Default) {
52-
LazyColumn(state = listState) {
53-
if (parentComment != null) {
54-
item {
55-
CommentRepliesHeader(comment = parentComment)
56-
HorizontalDivider(thickness = 1.dp)
57-
}
45+
LazyColumnScrollbar(state = listState) {
46+
LazyColumn(state = listState) {
47+
if (parentComment != null) {
48+
item {
49+
CommentRepliesHeader(comment = parentComment)
50+
HorizontalDivider(thickness = 1.dp)
5851
}
52+
}
5953

60-
items(itemCount) {
54+
if (comments.itemCount == 0) {
55+
item {
56+
if (refresh is LoadState.Loading) {
57+
LoadingIndicator(modifier = Modifier.padding(top = 8.dp))
58+
} else {
59+
NoCommentsMessage((refresh as? LoadState.Error)?.error)
60+
}
61+
}
62+
} else {
63+
items(comments.itemCount) {
6164
Comment(comment = comments[it]!!)
6265
}
6366
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.schabi.newpipe.compose.status
2+
3+
import androidx.compose.foundation.layout.fillMaxSize
4+
import androidx.compose.foundation.layout.wrapContentSize
5+
import androidx.compose.material3.CircularProgressIndicator
6+
import androidx.compose.material3.MaterialTheme
7+
import androidx.compose.runtime.Composable
8+
import androidx.compose.ui.Alignment
9+
import androidx.compose.ui.Modifier
10+
11+
@Composable
12+
fun LoadingIndicator(modifier: Modifier = Modifier) {
13+
CircularProgressIndicator(
14+
modifier = modifier.fillMaxSize().wrapContentSize(Alignment.Center),
15+
color = MaterialTheme.colorScheme.primary,
16+
trackColor = MaterialTheme.colorScheme.surfaceVariant,
17+
)
18+
}

0 commit comments

Comments
 (0)