mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2026-11-23 23:57:23 +00:00
Add separate live chat tab alongside comments tab
This commit is contained in:
1 parent
f8a3142835
commit
450b8978c0
9 files changed
+252
-132
No files matched your search
@@ -40,6 +40,11 @@ public class TabAdapter extends FragmentPagerAdapter {
|
||||
mFragmentTitleList.add(title);
|
||||
}
|
||||
|
||||
public void addFragment(final Fragment fragment, final String title, final int position) {
|
||||
mFragmentList.add(position, fragment);
|
||||
mFragmentTitleList.add(position, title);
|
||||
}
|
||||
|
||||
public void clearAllItems() {
|
||||
mFragmentList.clear();
|
||||
mFragmentTitleList.clear();
|
||||
|
||||
@@ -85,6 +85,7 @@ import org.schabi.newpipe.fragments.BaseStateFragment
|
||||
import org.schabi.newpipe.fragments.EmptyFragment
|
||||
import org.schabi.newpipe.fragments.MainFragment
|
||||
import org.schabi.newpipe.fragments.list.comments.CommentsFragment.Companion.getInstance
|
||||
import org.schabi.newpipe.fragments.list.comments.LiveChatFragment
|
||||
import org.schabi.newpipe.fragments.list.videos.RelatedItemsFragment.Companion.getInstance
|
||||
import org.schabi.newpipe.ktx.AnimationType
|
||||
import org.schabi.newpipe.ktx.animate
|
||||
@@ -324,7 +325,10 @@ class VideoDetailFragment :
|
||||
if (tabSettingsChanged) {
|
||||
tabSettingsChanged = false
|
||||
initTabs()
|
||||
currentInfo?.let { updateTabs(it) }
|
||||
currentInfo?.let {
|
||||
updateTabs(it)
|
||||
addLiveChatTabIfNeeded(it)
|
||||
}
|
||||
}
|
||||
|
||||
// Check if it was loading when the fragment was stopped/paused
|
||||
@@ -913,6 +917,33 @@ class VideoDetailFragment :
|
||||
}
|
||||
}
|
||||
|
||||
private fun addLiveChatTabIfNeeded(streamInfo: StreamInfo) {
|
||||
if (!streamInfo.hasLiveChat()) {
|
||||
return
|
||||
}
|
||||
val continuation = streamInfo.liveChatContinuation ?: return
|
||||
if (continuation.isEmpty()) {
|
||||
return
|
||||
}
|
||||
if (pageAdapter.getItemPositionByTitle(LIVE_CHAT_TAB_TAG) != -1) {
|
||||
return
|
||||
}
|
||||
|
||||
val commentsPosition = pageAdapter.getItemPositionByTitle(COMMENTS_TAB_TAG)
|
||||
val insertPosition = if (commentsPosition >= 0) commentsPosition + 1 else pageAdapter.count
|
||||
|
||||
pageAdapter.addFragment(
|
||||
LiveChatFragment.getInstance(serviceId, url, continuation),
|
||||
LIVE_CHAT_TAB_TAG,
|
||||
insertPosition
|
||||
)
|
||||
tabIcons.add(insertPosition, R.drawable.ic_live_tv)
|
||||
tabContentDescriptions.add(insertPosition, R.string.live_chat_tab_description)
|
||||
pageAdapter.notifyDataSetUpdate()
|
||||
updateTabIconsAndContentDescriptions()
|
||||
updateTabLayoutVisibility()
|
||||
}
|
||||
|
||||
fun updateTabLayoutVisibility() {
|
||||
if (nullableBinding == null) {
|
||||
// If binding is null we do not need to and should not do anything with its object(s)
|
||||
@@ -1423,6 +1454,7 @@ class VideoDetailFragment :
|
||||
setInitialData(info.serviceId, info.originalUrl, info.name, playQueue)
|
||||
|
||||
updateTabs(info)
|
||||
addLiveChatTabIfNeeded(info)
|
||||
|
||||
binding.detailThumbnailPlayButton.animate(true, 200)
|
||||
binding.detailVideoTitleView.text = title
|
||||
@@ -2347,6 +2379,7 @@ class VideoDetailFragment :
|
||||
App.PACKAGE_NAME + ".VideoDetailFragment.ACTION_VIDEO_FRAGMENT_STOPPED"
|
||||
|
||||
private const val COMMENTS_TAB_TAG = "COMMENTS"
|
||||
private const val LIVE_CHAT_TAB_TAG = "LIVE_CHAT"
|
||||
private const val RELATED_TAB_TAG = "NEXT VIDEO"
|
||||
private const val DESCRIPTION_TAB_TAG = "DESCRIPTION TAB"
|
||||
private const val EMPTY_TAB_TAG = "EMPTY TAB"
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.schabi.newpipe.fragments.list.comments
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.core.os.bundleOf
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.compose.content
|
||||
import org.schabi.newpipe.ui.components.video.comment.LiveChatSection
|
||||
import org.schabi.newpipe.ui.theme.AppTheme
|
||||
import org.schabi.newpipe.util.KEY_SERVICE_ID
|
||||
import org.schabi.newpipe.util.KEY_URL
|
||||
import org.schabi.newpipe.viewmodels.LiveChatViewModel
|
||||
|
||||
class LiveChatFragment : Fragment() {
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
) = content {
|
||||
AppTheme {
|
||||
Surface {
|
||||
LiveChatSection()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun getInstance(serviceId: Int, url: String?, liveChatContinuation: String) = LiveChatFragment().apply {
|
||||
arguments = bundleOf(
|
||||
KEY_SERVICE_ID to serviceId,
|
||||
KEY_URL to url,
|
||||
LiveChatViewModel.KEY_LIVE_CHAT_CONTINUATION to liveChatContinuation
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,17 +3,15 @@ package org.schabi.newpipe.ui.components.video.comment
|
||||
import androidx.compose.runtime.Immutable
|
||||
import org.schabi.newpipe.extractor.Page
|
||||
import org.schabi.newpipe.extractor.comments.CommentsInfo
|
||||
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
|
||||
|
||||
@Immutable
|
||||
class CommentInfo(
|
||||
val serviceId: Int,
|
||||
val url: String,
|
||||
val comments: List<CommentsInfoItem>,
|
||||
val comments: List<org.schabi.newpipe.extractor.comments.CommentsInfoItem>,
|
||||
val nextPage: Page?,
|
||||
val commentCount: Int,
|
||||
val isCommentsDisabled: Boolean,
|
||||
val isLiveChat: Boolean
|
||||
val isCommentsDisabled: Boolean
|
||||
) {
|
||||
constructor(commentsInfo: CommentsInfo) : this(
|
||||
commentsInfo.serviceId,
|
||||
@@ -21,7 +19,6 @@ class CommentInfo(
|
||||
commentsInfo.relatedItems,
|
||||
commentsInfo.nextPage,
|
||||
commentsInfo.commentsCount,
|
||||
commentsInfo.isCommentsDisabled,
|
||||
commentsInfo.isLiveChat
|
||||
commentsInfo.isCommentsDisabled
|
||||
)
|
||||
}
|
||||
+41
-76
@@ -12,7 +12,6 @@ import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -31,7 +30,6 @@ import kotlinx.coroutines.flow.flowOf
|
||||
import org.schabi.newpipe.R
|
||||
import org.schabi.newpipe.error.ErrorInfo
|
||||
import org.schabi.newpipe.error.UserAction
|
||||
import org.schabi.newpipe.extractor.Page
|
||||
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
|
||||
import org.schabi.newpipe.extractor.stream.Description
|
||||
import org.schabi.newpipe.ui.components.common.ErrorPanel
|
||||
@@ -46,28 +44,18 @@ import org.schabi.newpipe.viewmodels.util.Resource
|
||||
@Composable
|
||||
fun CommentSection(commentsViewModel: CommentsViewModel = viewModel()) {
|
||||
val state by commentsViewModel.uiState.collectAsStateWithLifecycle()
|
||||
val liveChatItems by commentsViewModel.liveChatItems.collectAsStateWithLifecycle()
|
||||
CommentSection(state, commentsViewModel.comments, liveChatItems)
|
||||
CommentSection(state, commentsViewModel.comments)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun CommentSection(
|
||||
uiState: Resource<CommentInfo>,
|
||||
commentsFlow: Flow<PagingData<CommentsInfoItem>>,
|
||||
liveChatItems: List<CommentsInfoItem>
|
||||
commentsFlow: Flow<PagingData<CommentsInfoItem>>
|
||||
) {
|
||||
val comments = commentsFlow.collectAsLazyPagingItems()
|
||||
val nestedScrollInterop = rememberNestedScrollInteropConnection()
|
||||
val state = rememberLazyListState()
|
||||
|
||||
// Auto-scroll to top when new live chat messages arrive
|
||||
val isLiveChat = uiState is Resource.Success && uiState.data.isLiveChat
|
||||
LaunchedEffect(liveChatItems.size) {
|
||||
if (isLiveChat && liveChatItems.isNotEmpty()) {
|
||||
state.scrollToItem(0)
|
||||
}
|
||||
}
|
||||
|
||||
LazyColumnThemedScrollbar(state = state) {
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
@@ -86,17 +74,16 @@ private fun CommentSection(
|
||||
val commentInfo = uiState.data
|
||||
val count = commentInfo.commentCount
|
||||
|
||||
if (commentInfo.isCommentsDisabled && !commentInfo.isLiveChat) {
|
||||
if (commentInfo.isCommentsDisabled) {
|
||||
item {
|
||||
EmptyStateComposable(
|
||||
spec = EmptyStateSpec.DisabledComments,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.heightIn(min = 128.dp)
|
||||
|
||||
)
|
||||
}
|
||||
} else if (count == 0 && !commentInfo.isLiveChat) {
|
||||
} else if (count == 0) {
|
||||
item {
|
||||
EmptyStateComposable(
|
||||
spec = EmptyStateSpec.NoComments,
|
||||
@@ -106,8 +93,7 @@ private fun CommentSection(
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// Show title for regular comments, but not for live chat
|
||||
if (count >= 0 && !commentInfo.isLiveChat) {
|
||||
if (count >= 0) {
|
||||
item {
|
||||
Text(
|
||||
modifier = Modifier
|
||||
@@ -119,66 +105,47 @@ private fun CommentSection(
|
||||
}
|
||||
}
|
||||
|
||||
if (commentInfo.isLiveChat) {
|
||||
// Live chat: render items directly without Paging 3
|
||||
if (liveChatItems.isEmpty()) {
|
||||
when (val refresh = comments.loadState.refresh) {
|
||||
is LoadState.Loading -> {
|
||||
item {
|
||||
EmptyStateComposable(
|
||||
spec = EmptyStateSpec.NoComments,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.heightIn(min = 128.dp)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
items(liveChatItems.size, key = { liveChatItems[it].commentId }) {
|
||||
Comment(comment = liveChatItems[it]) {}
|
||||
LoadingIndicator(modifier = Modifier.padding(top = 8.dp))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Normal comments via Paging 3
|
||||
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"
|
||||
)
|
||||
|
||||
item {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
) {
|
||||
ErrorPanel(
|
||||
errorInfo = errorInfo,
|
||||
onRetry = { comments.retry() },
|
||||
modifier = Modifier.align(Alignment.Center)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
is LoadState.Error -> {
|
||||
val errorInfo = ErrorInfo(
|
||||
throwable = refresh.error,
|
||||
userAction = UserAction.REQUESTED_COMMENTS,
|
||||
request = "comments"
|
||||
)
|
||||
|
||||
else -> {
|
||||
if (comments.itemCount == 0) {
|
||||
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 -> {
|
||||
if (comments.itemCount == 0) {
|
||||
item {
|
||||
EmptyStateComposable(
|
||||
spec = EmptyStateSpec.NoComments,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.heightIn(min = 128.dp)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
items(comments.itemCount) {
|
||||
Comment(comment = comments[it]!!) {}
|
||||
}
|
||||
} else {
|
||||
items(comments.itemCount) {
|
||||
Comment(comment = comments[it]!!) {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,7 +215,7 @@ private fun CommentSection(
|
||||
private fun CommentSectionLoadingPreview() {
|
||||
AppTheme {
|
||||
Surface {
|
||||
CommentSection(uiState = Resource.Loading, commentsFlow = flowOf(), liveChatItems = emptyList())
|
||||
CommentSection(uiState = Resource.Loading, commentsFlow = flowOf())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -264,7 +231,7 @@ private fun CommentSectionSuccessPreview() {
|
||||
Description.Type.PLAIN_TEXT
|
||||
),
|
||||
uploaderName = "Test",
|
||||
replies = Page(""),
|
||||
replies = org.schabi.newpipe.extractor.Page(""),
|
||||
replyCount = 10
|
||||
)
|
||||
) + (2..10).map {
|
||||
@@ -284,12 +251,10 @@ private fun CommentSectionSuccessPreview() {
|
||||
comments = comments,
|
||||
nextPage = null,
|
||||
commentCount = 10,
|
||||
isCommentsDisabled = false,
|
||||
isLiveChat = false
|
||||
isCommentsDisabled = false
|
||||
)
|
||||
),
|
||||
commentsFlow = flowOf(PagingData.from(comments)),
|
||||
liveChatItems = emptyList()
|
||||
commentsFlow = flowOf(PagingData.from(comments))
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -301,7 +266,7 @@ private fun CommentSectionSuccessPreview() {
|
||||
private fun CommentSectionErrorPreview() {
|
||||
AppTheme {
|
||||
Surface {
|
||||
CommentSection(uiState = Resource.Error(RuntimeException()), commentsFlow = flowOf(), liveChatItems = emptyList())
|
||||
CommentSection(uiState = Resource.Error(RuntimeException()), commentsFlow = flowOf())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.schabi.newpipe.ui.components.video.comment
|
||||
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import org.schabi.newpipe.ui.components.common.LazyColumnThemedScrollbar
|
||||
import org.schabi.newpipe.ui.components.common.LoadingIndicator
|
||||
import org.schabi.newpipe.viewmodels.LiveChatViewModel
|
||||
|
||||
@Composable
|
||||
fun LiveChatSection(liveChatViewModel: LiveChatViewModel = viewModel()) {
|
||||
val liveChatItems by liveChatViewModel.liveChatItems.collectAsStateWithLifecycle()
|
||||
val state = rememberLazyListState()
|
||||
val nestedScrollInterop = rememberNestedScrollInteropConnection()
|
||||
|
||||
LaunchedEffect(liveChatItems.size) {
|
||||
if (liveChatItems.isNotEmpty()) {
|
||||
state.scrollToItem(0)
|
||||
}
|
||||
}
|
||||
|
||||
LazyColumnThemedScrollbar(state = state) {
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.nestedScroll(nestedScrollInterop),
|
||||
state = state
|
||||
) {
|
||||
item {
|
||||
Text(
|
||||
modifier = Modifier.padding(start = 12.dp, end = 12.dp, bottom = 4.dp),
|
||||
text = "Live Chat",
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
}
|
||||
|
||||
if (liveChatItems.isEmpty()) {
|
||||
item {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.heightIn(min = 128.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
LoadingIndicator()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
items(liveChatItems.size, key = { liveChatItems[it].commentId }) {
|
||||
Comment(comment = liveChatItems[it]) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,21 +9,14 @@ import androidx.paging.PagingData
|
||||
import androidx.paging.cachedIn
|
||||
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.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
|
||||
import org.schabi.newpipe.paging.CommentsSource
|
||||
import org.schabi.newpipe.ui.components.video.comment.CommentInfo
|
||||
import org.schabi.newpipe.util.KEY_URL
|
||||
@@ -42,51 +35,13 @@ class CommentsViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
|
||||
.flowOn(Dispatchers.IO)
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), Resource.Loading)
|
||||
|
||||
// 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
|
||||
val comments: Flow<PagingData<org.schabi.newpipe.extractor.comments.CommentsInfoItem>> = uiState
|
||||
.filterIsInstance<Resource.Success<CommentInfo>>()
|
||||
.flatMapLatest {
|
||||
val info = it.data
|
||||
if (info.isLiveChat) {
|
||||
_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)
|
||||
}.flow
|
||||
}
|
||||
Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) {
|
||||
CommentsSource(it.data)
|
||||
}.flow
|
||||
}
|
||||
.cachedIn(viewModelScope)
|
||||
|
||||
private fun startLiveChatPolling(info: CommentInfo) {
|
||||
var nextPage = info.nextPage
|
||||
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
while (isActive) {
|
||||
delay(3000)
|
||||
if (nextPage == null) {
|
||||
continue
|
||||
}
|
||||
try {
|
||||
val result = CommentsInfo.getMoreItems(
|
||||
NewPipe.getService(info.serviceId),
|
||||
info.url,
|
||||
nextPage
|
||||
)
|
||||
if (result.items.isNotEmpty()) {
|
||||
_liveChatItems.value = result.items + _liveChatItems.value
|
||||
}
|
||||
nextPage = result.nextPage
|
||||
} catch (e: Exception) {
|
||||
// Silently ignore polling errors
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.schabi.newpipe.viewmodels
|
||||
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
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.extractor.NewPipe
|
||||
import org.schabi.newpipe.extractor.comments.CommentsInfo
|
||||
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
|
||||
import org.schabi.newpipe.util.KEY_SERVICE_ID
|
||||
import org.schabi.newpipe.util.KEY_URL
|
||||
|
||||
class LiveChatViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
|
||||
private val serviceId: Int = savedStateHandle[KEY_SERVICE_ID] ?: 0
|
||||
private val url: String = savedStateHandle[KEY_URL] ?: ""
|
||||
private val liveChatContinuation: String =
|
||||
savedStateHandle.get<String>(KEY_LIVE_CHAT_CONTINUATION) ?: ""
|
||||
|
||||
private val _liveChatItems = MutableStateFlow<List<CommentsInfoItem>>(emptyList())
|
||||
val liveChatItems: StateFlow<List<CommentsInfoItem>> = _liveChatItems
|
||||
|
||||
init {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
val service = NewPipe.getService(serviceId)
|
||||
val extractor = service.getCommentsExtractor(url)
|
||||
extractor.setLiveChatContinuation(liveChatContinuation)
|
||||
val info = CommentsInfo.getInfo(extractor)
|
||||
_liveChatItems.value = info.relatedItems
|
||||
var nextPage = info.nextPage
|
||||
while (isActive) {
|
||||
delay(3000)
|
||||
if (nextPage == null) continue
|
||||
val result = CommentsInfo.getMoreItems(service, url, nextPage)
|
||||
if (result.items.isNotEmpty()) {
|
||||
_liveChatItems.value = result.items + _liveChatItems.value
|
||||
}
|
||||
nextPage = result.nextPage
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// Ignore initialization errors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val KEY_LIVE_CHAT_CONTINUATION = "live_chat_continuation"
|
||||
}
|
||||
}
|
||||
@@ -287,6 +287,7 @@
|
||||
<string name="detail_likes_img_view_description">Likes</string>
|
||||
<string name="detail_dislikes_img_view_description">Dislikes</string>
|
||||
<string name="comments_tab_description">Comments</string>
|
||||
<string name="live_chat_tab_description">Live Chat</string>
|
||||
<string name="related_items_tab_description">Related items</string>
|
||||
<string name="description_tab_description">Description</string>
|
||||
<string name="search_no_results">No results</string>
|
||||
|
||||
Reference in new issue
Block a user