mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2026-11-23 15:47:20 +00:00
[Live Chat] Stop loading when there is no next page
Some music videos have a live chat but no comments so the next page is null.
This commit is contained in:
1 parent
f313f86c69
commit
ebadd9b208
2 files changed
+23
-3
No files matched your search
+13
-1
@@ -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]) {}
|
||||
|
||||
@@ -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<String>(KEY_LIVE_CHAT_CONTINUATION) ?: ""
|
||||
|
||||
private val _isLoading = MutableStateFlow<Boolean>(true)
|
||||
val isLoading: StateFlow<Boolean> = _isLoading
|
||||
|
||||
private val _liveChatItems = MutableStateFlow<List<CommentsInfoItem>>(emptyList())
|
||||
val liveChatItems: StateFlow<List<CommentsInfoItem>> = _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) {
|
||||
|
||||
Reference in new issue
Block a user