Fix live chat to use direct list instead of PagingData

This commit is contained in:
h authored and Tobi committed 2026-11-03 23:04:48 +01:00
1 parent db53c03b57
commit 813b618711
2 files changed
+100 -64

No files matched your search

@@ -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<CommentInfo>,
commentsFlow: Flow<PagingData<CommentsInfoItem>>
commentsFlow: Flow<PagingData<CommentsInfoItem>>,
liveChatItems: List<CommentsInfoItem>
) {
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())
}
}
}
@@ -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<List<CommentsInfoItem>>(emptyList())
val liveChatItems: StateFlow<List<CommentsInfoItem>> = _liveChatItems
@OptIn(ExperimentalCoroutinesApi::class)
val comments: Flow<PagingData<CommentsInfoItem>> = uiState
.filterIsInstance<Resource.Success<CommentInfo>>()
@@ -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<PagingData<CommentsInfoItem>> = 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)
}
}
}
}