Report live chat errors via snackbar

This commit is contained in:
TobiGr authored and Tobi committed 2026-11-03 23:04:48 +01:00
1 parent 22495750b9
commit 77739f0597
2 files changed
+27 -2

No files matched your search

@@ -27,6 +27,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
@@ -34,6 +35,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import kotlinx.coroutines.launch
import org.schabi.newpipe.R
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.viewmodels.LiveChatViewModel
@@ -44,6 +46,7 @@ fun LiveChatSection(liveChatViewModel: LiveChatViewModel = viewModel()) {
val state = rememberLazyListState()
val nestedScrollInterop = rememberNestedScrollInteropConnection()
val coroutineScope = rememberCoroutineScope()
val context = LocalContext.current
// Track whether user is at the top of the list
val isAtTop by remember { derivedStateOf { state.firstVisibleItemIndex == 0 } }
@@ -56,6 +59,18 @@ fun LiveChatSection(liveChatViewModel: LiveChatViewModel = viewModel()) {
val unreadCount = liveChatItems.size - lastSeenCount.value
// Track whether an error was caught and show it if error occurs
val errorSeen = remember { mutableStateOf(false) }
val error by liveChatViewModel.error.collectAsStateWithLifecycle()
if (error != null && !errorSeen.value) {
errorSeen.value = true
ErrorUtil.showUiErrorSnackbar(
context = context,
request = "Loading live chat comments",
throwable = error!!
)
}
Box(modifier = Modifier.fillMaxSize()) {
LazyColumnThemedScrollbar(state = state) {
LazyColumn(
@@ -1,17 +1,23 @@
package org.schabi.newpipe.viewmodels
import android.util.Log
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import org.schabi.newpipe.App
import org.schabi.newpipe.error.ErrorUtil
import org.schabi.newpipe.extractor.NewPipe
import org.schabi.newpipe.extractor.comments.CommentsInfo
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
import org.schabi.newpipe.extractor.exceptions.ExtractionException
import org.schabi.newpipe.util.KEY_SERVICE_ID
import org.schabi.newpipe.util.KEY_URL
@@ -24,6 +30,9 @@ class LiveChatViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
private val _liveChatItems = MutableStateFlow<List<CommentsInfoItem>>(emptyList())
val liveChatItems: StateFlow<List<CommentsInfoItem>> = _liveChatItems
private val _error = MutableStateFlow<Throwable?>(null)
val error: StateFlow<Throwable?> = _error
init {
viewModelScope.launch(Dispatchers.IO) {
try {
@@ -34,7 +43,7 @@ class LiveChatViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
_liveChatItems.value = info.relatedItems
var nextPage = info.nextPage
while (isActive) {
delay(3000)
delay(3.seconds)
if (nextPage == null) continue
val result = CommentsInfo.getMoreItems(service, url, nextPage)
if (result.items.isNotEmpty()) {
@@ -43,7 +52,8 @@ class LiveChatViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
nextPage = result.nextPage
}
} catch (e: Exception) {
// Ignore initialization errors
Log.e("LiveChatViewModel", "Error loading live chat comments", e)
_error.value = e
}
}
}