From 813b618711d0ff39488fd382aefa80b29f10c2d7 Mon Sep 17 00:00:00 2001 From: h Date: Thu, 23 Apr 2026 19:18:00 +0200 Subject: [PATCH] Fix live chat to use direct list instead of PagingData --- .../video/comment/CommentSection.kt | 101 +++++++++++------- .../newpipe/viewmodels/CommentsViewModel.kt | 63 ++++++----- 2 files changed, 100 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentSection.kt b/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentSection.kt index 1ef6e7690..a42f11f15 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentSection.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentSection.kt @@ -45,13 +45,15 @@ import org.schabi.newpipe.viewmodels.util.Resource @Composable fun CommentSection(commentsViewModel: CommentsViewModel = viewModel()) { val state by commentsViewModel.uiState.collectAsStateWithLifecycle() - CommentSection(state, commentsViewModel.comments) + val liveChatItems by commentsViewModel.liveChatItems.collectAsStateWithLifecycle() + CommentSection(state, commentsViewModel.comments, liveChatItems) } @Composable private fun CommentSection( uiState: Resource, - commentsFlow: Flow> + commentsFlow: Flow>, + liveChatItems: List ) { val comments = commentsFlow.collectAsLazyPagingItems() val nestedScrollInterop = rememberNestedScrollInteropConnection() @@ -75,7 +77,7 @@ private fun CommentSection( val commentInfo = uiState.data val count = commentInfo.commentCount - if (commentInfo.isCommentsDisabled && !commentInfo.isLiveChat) { + if (commentInfo.isCommentsDisabled) { item { EmptyStateComposable( spec = EmptyStateSpec.DisabledComments, @@ -85,7 +87,7 @@ private fun CommentSection( ) } - } else if (count == 0 && !commentInfo.isLiveChat) { + } else if (count == 0) { item { EmptyStateComposable( spec = EmptyStateSpec.NoComments, @@ -95,7 +97,7 @@ private fun CommentSection( ) } } else { - // do not show anything if the comment count is unknown + // Show title for regular comments, but not for live chat if (count >= 0 && !commentInfo.isLiveChat) { item { Text( @@ -107,47 +109,67 @@ private fun CommentSection( ) } } - when (val refresh = comments.loadState.refresh) { - is LoadState.Loading -> { - item { - LoadingIndicator(modifier = Modifier.padding(top = 8.dp)) - } - } - - is LoadState.Error -> { - val errorInfo = ErrorInfo( - throwable = refresh.error, - userAction = UserAction.REQUESTED_COMMENTS, - request = "comments" - ) + if (commentInfo.isLiveChat) { + // Live chat: render items directly without Paging 3 + if (liveChatItems.isEmpty()) { item { - Box( + EmptyStateComposable( + spec = EmptyStateSpec.NoComments, modifier = Modifier .fillMaxWidth() - ) { - ErrorPanel( - errorInfo = errorInfo, - onRetry = { comments.retry() }, - modifier = Modifier.align(Alignment.Center) - ) - } + .heightIn(min = 128.dp) + ) + } + } else { + items(liveChatItems.size, key = { liveChatItems[it].commentId }) { + Comment(comment = liveChatItems[it]) {} } } - - else -> { - if (comments.itemCount == 0 && commentInfo.isLiveChat) { + } else { + // Normal comments via Paging 3 + when (val refresh = comments.loadState.refresh) { + is LoadState.Loading -> { item { - EmptyStateComposable( - spec = EmptyStateSpec.NoComments, + LoadingIndicator(modifier = Modifier.padding(top = 8.dp)) + } + } + + is LoadState.Error -> { + val errorInfo = ErrorInfo( + throwable = refresh.error, + userAction = UserAction.REQUESTED_COMMENTS, + request = "comments" + ) + + item { + Box( modifier = Modifier .fillMaxWidth() - .heightIn(min = 128.dp) - ) + ) { + ErrorPanel( + errorInfo = errorInfo, + onRetry = { comments.retry() }, + modifier = Modifier.align(Alignment.Center) + ) + } } - } else { - items(comments.itemCount) { - Comment(comment = comments[it]!!) {} + } + + else -> { + if (comments.itemCount == 0) { + item { + EmptyStateComposable( + spec = EmptyStateSpec.NoComments, + modifier = Modifier + .fillMaxWidth() + .heightIn(min = 128.dp) + ) + } + } else { + items(comments.itemCount) { + Comment(comment = comments[it]!!) {} + } } } @@ -217,7 +239,7 @@ private fun CommentSection( private fun CommentSectionLoadingPreview() { AppTheme { Surface { - CommentSection(uiState = Resource.Loading, commentsFlow = flowOf()) + CommentSection(uiState = Resource.Loading, commentsFlow = flowOf(), liveChatItems = emptyList()) } } } @@ -257,7 +279,8 @@ private fun CommentSectionSuccessPreview() { isLiveChat = false ) ), - commentsFlow = flowOf(PagingData.from(comments)) + commentsFlow = flowOf(PagingData.from(comments)), + liveChatItems = emptyList() ) } } @@ -269,7 +292,7 @@ private fun CommentSectionSuccessPreview() { private fun CommentSectionErrorPreview() { AppTheme { Surface { - CommentSection(uiState = Resource.Error(RuntimeException()), commentsFlow = flowOf()) + CommentSection(uiState = Resource.Error(RuntimeException()), commentsFlow = flowOf(), liveChatItems = emptyList()) } } } diff --git a/app/src/main/java/org/schabi/newpipe/viewmodels/CommentsViewModel.kt b/app/src/main/java/org/schabi/newpipe/viewmodels/CommentsViewModel.kt index 0763ecebb..9f475d8d1 100644 --- a/app/src/main/java/org/schabi/newpipe/viewmodels/CommentsViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/viewmodels/CommentsViewModel.kt @@ -12,13 +12,17 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.filterIsInstance import kotlinx.coroutines.flow.flatMapLatest -import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch import org.schabi.newpipe.extractor.NewPipe import org.schabi.newpipe.extractor.comments.CommentsInfo import org.schabi.newpipe.extractor.comments.CommentsInfoItem @@ -51,7 +55,10 @@ class CommentsViewModel(savedStateHandle: SavedStateHandle) : ViewModel() { .flowOn(Dispatchers.IO) .stateIn(viewModelScope, SharingStarted.WhileSubscribed(), Resource.Loading) - // Live chat via PagingData (broken approach) + // Separate flow for live chat items (not using Paging 3) + private val _liveChatItems = MutableStateFlow>(emptyList()) + val liveChatItems: StateFlow> = _liveChatItems + @OptIn(ExperimentalCoroutinesApi::class) val comments: Flow> = uiState .filterIsInstance>() @@ -59,7 +66,10 @@ class CommentsViewModel(savedStateHandle: SavedStateHandle) : ViewModel() { val info = it.data Log.i(TAG, "flatMapLatest: isLiveChat=${info.isLiveChat}, items=${info.comments.size}") if (info.isLiveChat) { - liveChatPagingData(info) + _liveChatItems.value = info.comments + startLiveChatPolling(info) + // Return empty PagingData for live chat (items come from liveChatItems flow) + kotlinx.coroutines.flow.flowOf(androidx.paging.PagingData.empty()) } else { Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) { CommentsSource(info) @@ -68,29 +78,32 @@ class CommentsViewModel(savedStateHandle: SavedStateHandle) : ViewModel() { } .cachedIn(viewModelScope) - private fun liveChatPagingData(info: CommentInfo): Flow> = flow { - val allItems = info.comments.toMutableList() - emit(PagingData.from(allItems)) + private fun startLiveChatPolling(info: CommentInfo) { var nextPage = info.nextPage - while (true) { - delay(3000) - if (nextPage == null) { - Log.d(TAG, "liveChatPolling: nextPage is null, skipping") - continue - } - try { - Log.d(TAG, "liveChatPolling: fetching more items...") - val result = CommentsInfo.getMoreItems( - NewPipe.getService(info.serviceId), - info.url, - nextPage - ) - Log.i(TAG, "liveChatPolling: fetched ${result.items.size} items") - allItems.addAll(result.items) - emit(PagingData.from(allItems)) - nextPage = result.nextPage - } catch (e: Exception) { - Log.e(TAG, "liveChatPolling: failed to fetch more items", e) + Log.i(TAG, "startLiveChatPolling() items=${info.comments.size}, nextPage=${nextPage != null}") + + viewModelScope.launch(Dispatchers.IO) { + while (isActive) { + delay(3000) + if (nextPage == null) { + Log.d(TAG, "liveChatPolling: nextPage is null, skipping") + continue + } + try { + Log.d(TAG, "liveChatPolling: fetching more items...") + val result = CommentsInfo.getMoreItems( + NewPipe.getService(info.serviceId), + info.url, + nextPage + ) + Log.i(TAG, "liveChatPolling: fetched ${result.items.size} items, nextPage=${result.nextPage != null}") + if (result.items.isNotEmpty()) { + _liveChatItems.value = _liveChatItems.value + result.items + } + nextPage = result.nextPage + } catch (e: Exception) { + Log.e(TAG, "liveChatPolling: failed to fetch more items", e) + } } } }