@@ -45,13 +45,15 @@ import org.schabi.newpipe.viewmodels.util.Resource
4545@Composable
4646fun CommentSection (commentsViewModel : CommentsViewModel = viewModel()) {
4747 val state by commentsViewModel.uiState.collectAsStateWithLifecycle()
48- CommentSection (state, commentsViewModel.comments)
48+ val liveChatItems by commentsViewModel.liveChatItems.collectAsStateWithLifecycle()
49+ CommentSection (state, commentsViewModel.comments, liveChatItems)
4950}
5051
5152@Composable
5253private fun CommentSection (
5354 uiState : Resource <CommentInfo >,
54- commentsFlow : Flow <PagingData <CommentsInfoItem >>
55+ commentsFlow : Flow <PagingData <CommentsInfoItem >>,
56+ liveChatItems : List <CommentsInfoItem >
5557) {
5658 val comments = commentsFlow.collectAsLazyPagingItems()
5759 val nestedScrollInterop = rememberNestedScrollInteropConnection()
@@ -75,7 +77,7 @@ private fun CommentSection(
7577 val commentInfo = uiState.data
7678 val count = commentInfo.commentCount
7779
78- if (commentInfo.isCommentsDisabled && ! commentInfo.isLiveChat ) {
80+ if (commentInfo.isCommentsDisabled) {
7981 item {
8082 EmptyStateComposable (
8183 spec = EmptyStateSpec .DisabledComments ,
@@ -85,7 +87,7 @@ private fun CommentSection(
8587
8688 )
8789 }
88- } else if (count == 0 && ! commentInfo.isLiveChat ) {
90+ } else if (count == 0 ) {
8991 item {
9092 EmptyStateComposable (
9193 spec = EmptyStateSpec .NoComments ,
@@ -95,7 +97,7 @@ private fun CommentSection(
9597 )
9698 }
9799 } else {
98- // do not show anything if the comment count is unknown
100+ // Show title for regular comments, but not for live chat
99101 if (count >= 0 && ! commentInfo.isLiveChat) {
100102 item {
101103 Text (
@@ -107,47 +109,67 @@ private fun CommentSection(
107109 )
108110 }
109111 }
110- when (val refresh = comments.loadState.refresh) {
111- is LoadState .Loading -> {
112- item {
113- LoadingIndicator (modifier = Modifier .padding(top = 8 .dp))
114- }
115- }
116-
117- is LoadState .Error -> {
118- val errorInfo = ErrorInfo (
119- throwable = refresh.error,
120- userAction = UserAction .REQUESTED_COMMENTS ,
121- request = " comments"
122- )
123112
113+ if (commentInfo.isLiveChat) {
114+ // Live chat: render items directly without Paging 3
115+ if (liveChatItems.isEmpty()) {
124116 item {
125- Box (
117+ EmptyStateComposable (
118+ spec = EmptyStateSpec .NoComments ,
126119 modifier = Modifier
127120 .fillMaxWidth()
128- ) {
129- ErrorPanel (
130- errorInfo = errorInfo,
131- onRetry = { comments.retry() },
132- modifier = Modifier .align(Alignment .Center )
133- )
134- }
121+ .heightIn(min = 128 .dp)
122+ )
123+ }
124+ } else {
125+ items(liveChatItems.size, key = { liveChatItems[it].commentId }) {
126+ Comment (comment = liveChatItems[it]) {}
135127 }
136128 }
129+ } else {
130+ // Normal comments via Paging 3
131+ when (val refresh = comments.loadState.refresh) {
132+ is LoadState .Loading -> {
133+ item {
134+ LoadingIndicator (modifier = Modifier .padding(top = 8 .dp))
135+ }
136+ }
137+
138+ is LoadState .Error -> {
139+ val errorInfo = ErrorInfo (
140+ throwable = refresh.error,
141+ userAction = UserAction .REQUESTED_COMMENTS ,
142+ request = " comments"
143+ )
137144
138- else -> {
139- if (comments.itemCount == 0 && commentInfo.isLiveChat) {
140145 item {
141- EmptyStateComposable (
142- spec = EmptyStateSpec .NoComments ,
146+ Box (
143147 modifier = Modifier
144148 .fillMaxWidth()
145- .heightIn(min = 128 .dp)
146- )
149+ ) {
150+ ErrorPanel (
151+ errorInfo = errorInfo,
152+ onRetry = { comments.retry() },
153+ modifier = Modifier .align(Alignment .Center )
154+ )
155+ }
147156 }
148- } else {
149- items(comments.itemCount) {
150- Comment (comment = comments[it]!! ) {}
157+ }
158+
159+ else -> {
160+ if (comments.itemCount == 0 ) {
161+ item {
162+ EmptyStateComposable (
163+ spec = EmptyStateSpec .NoComments ,
164+ modifier = Modifier
165+ .fillMaxWidth()
166+ .heightIn(min = 128 .dp)
167+ )
168+ }
169+ } else {
170+ items(comments.itemCount) {
171+ Comment (comment = comments[it]!! ) {}
172+ }
151173 }
152174 }
153175 }
@@ -186,7 +208,7 @@ private fun CommentSection(
186208private fun CommentSectionLoadingPreview () {
187209 AppTheme {
188210 Surface {
189- CommentSection (uiState = Resource .Loading , commentsFlow = flowOf())
211+ CommentSection (uiState = Resource .Loading , commentsFlow = flowOf(), liveChatItems = emptyList() )
190212 }
191213 }
192214}
@@ -226,7 +248,8 @@ private fun CommentSectionSuccessPreview() {
226248 isLiveChat = false
227249 )
228250 ),
229- commentsFlow = flowOf(PagingData .from(comments))
251+ commentsFlow = flowOf(PagingData .from(comments)),
252+ liveChatItems = emptyList()
230253 )
231254 }
232255 }
@@ -238,7 +261,7 @@ private fun CommentSectionSuccessPreview() {
238261private fun CommentSectionErrorPreview () {
239262 AppTheme {
240263 Surface {
241- CommentSection (uiState = Resource .Error (RuntimeException ()), commentsFlow = flowOf())
264+ CommentSection (uiState = Resource .Error (RuntimeException ()), commentsFlow = flowOf(), liveChatItems = emptyList() )
242265 }
243266 }
244267}
0 commit comments