From 77739f05972b3e8ddbd888ca67b3c0ef586a9c87 Mon Sep 17 00:00:00 2001 From: TobiGr Date: Tue, 3 Nov 2026 14:29:14 +0100 Subject: [PATCH] Report live chat errors via snackbar --- .../components/video/comment/LiveChatSection.kt | 15 +++++++++++++++ .../newpipe/viewmodels/LiveChatViewModel.kt | 14 ++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) 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 5e22d6b35..cb7e81814 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 @@ -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( 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 f278a70f7..d78ad8549 100644 --- a/app/src/main/java/org/schabi/newpipe/viewmodels/LiveChatViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/viewmodels/LiveChatViewModel.kt @@ -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>(emptyList()) val liveChatItems: StateFlow> = _liveChatItems + private val _error = MutableStateFlow(null) + val error: StateFlow = _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 } } }