diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/LiveChatSection.kt b/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/LiveChatSection.kt index e03a07816..98de8f1d0 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/LiveChatSection.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/LiveChatSection.kt @@ -39,10 +39,13 @@ import org.schabi.newpipe.error.ErrorUtil import org.schabi.newpipe.ui.components.common.LazyColumnThemedScrollbar import org.schabi.newpipe.ui.components.common.LoadingIndicator import org.schabi.newpipe.ui.components.common.NetworkIssue +import org.schabi.newpipe.ui.emptystate.EmptyStateComposable +import org.schabi.newpipe.ui.emptystate.EmptyStateSpec import org.schabi.newpipe.viewmodels.LiveChatViewModel @Composable fun LiveChatSection(liveChatViewModel: LiveChatViewModel = viewModel()) { + val isLoading by liveChatViewModel.isLoading.collectAsStateWithLifecycle() val liveChatItems by liveChatViewModel.liveChatItems.collectAsStateWithLifecycle() val state = rememberLazyListState() val nestedScrollInterop = rememberNestedScrollInteropConnection() @@ -95,7 +98,7 @@ fun LiveChatSection(liveChatViewModel: LiveChatViewModel = viewModel()) { } } - if (liveChatItems.isEmpty()) { + if (isLoading) { item { Box( modifier = Modifier @@ -106,6 +109,15 @@ fun LiveChatSection(liveChatViewModel: LiveChatViewModel = viewModel()) { LoadingIndicator() } } + } else if (liveChatItems.isEmpty()) { + item { + EmptyStateComposable( + spec = EmptyStateSpec.NoComments, + modifier = Modifier + .fillMaxWidth() + .heightIn(min = 128.dp) + ) + } } else { items(liveChatItems.size, key = { liveChatItems[it].commentId }) { Comment(comment = liveChatItems[it]) {} diff --git a/app/src/main/java/org/schabi/newpipe/viewmodels/LiveChatViewModel.kt b/app/src/main/java/org/schabi/newpipe/viewmodels/LiveChatViewModel.kt index 55ca87dc4..5d3f0e7eb 100644 --- a/app/src/main/java/org/schabi/newpipe/viewmodels/LiveChatViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/viewmodels/LiveChatViewModel.kt @@ -5,6 +5,7 @@ import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import kotlin.time.Duration.Companion.seconds +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow @@ -25,6 +26,9 @@ class LiveChatViewModel(savedStateHandle: SavedStateHandle) : ViewModel() { private val liveChatContinuation: String = savedStateHandle.get(KEY_LIVE_CHAT_CONTINUATION) ?: "" + private val _isLoading = MutableStateFlow(true) + val isLoading: StateFlow = _isLoading + private val _liveChatItems = MutableStateFlow>(emptyList()) val liveChatItems: StateFlow> = _liveChatItems @@ -43,10 +47,14 @@ class LiveChatViewModel(savedStateHandle: SavedStateHandle) : ViewModel() { val info = CommentsInfo.getInfo(extractor) _liveChatItems.value = info.relatedItems.take(MAX_LIVE_CHAT_ITEMS) var nextPage = info.nextPage + _isLoading.value = false + while (isActive) { + delay(3.seconds) + if (nextPage == null) { + break + } try { - delay(3.seconds) - if (nextPage == null) continue val result = CommentsInfo.getMoreItems(service, url, nextPage) if (result.items.isNotEmpty()) { _liveChatItems.value = buildList(MAX_LIVE_CHAT_ITEMS) {