mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
Merge pull request #5830 from Bnyro/master
fix: various comment/replies related issues
This commit is contained in:
commit
e1ec0aa943
@ -35,15 +35,12 @@ class CommentPagingAdapter(
|
||||
private val fragment: Fragment?,
|
||||
private val videoId: String,
|
||||
private val channelAvatar: String?,
|
||||
private val parentComment: Comment? = null,
|
||||
private val isRepliesAdapter: Boolean = false,
|
||||
private val handleLink: ((url: String) -> Unit)?,
|
||||
private val dismiss: () -> Unit
|
||||
) : PagingDataAdapter<Comment, CommentsViewHolder>(CommentCallback) {
|
||||
private val isRepliesAdapter = parentComment != null
|
||||
private var clickEventConsumedByLinkHandler = false
|
||||
|
||||
override fun getItemCount() = (if (isRepliesAdapter) 1 else 0) + super.getItemCount()
|
||||
|
||||
private fun navigateToReplies(comment: Comment) {
|
||||
if (clickEventConsumedByLinkHandler) {
|
||||
clickEventConsumedByLinkHandler = false
|
||||
@ -58,11 +55,8 @@ class CommentPagingAdapter(
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: CommentsViewHolder, position: Int) {
|
||||
val comment = if (parentComment != null) {
|
||||
if (position == 0) parentComment else getItem(position - 1)!!
|
||||
} else {
|
||||
getItem(position)!!
|
||||
}
|
||||
val comment = getItem(position)!!
|
||||
|
||||
holder.binding.apply {
|
||||
commentAuthor.text = comment.author
|
||||
commentAuthor.setBackgroundResource(
|
||||
|
@ -14,6 +14,8 @@ import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import androidx.paging.LoadState
|
||||
import androidx.paging.Pager
|
||||
import androidx.paging.PagingConfig
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.api.obj.Comment
|
||||
@ -23,6 +25,7 @@ import com.github.libretube.extensions.formatShort
|
||||
import com.github.libretube.extensions.parcelable
|
||||
import com.github.libretube.ui.adapters.CommentPagingAdapter
|
||||
import com.github.libretube.ui.models.CommentsViewModel
|
||||
import com.github.libretube.ui.models.sources.CommentRepliesPagingSource
|
||||
import com.github.libretube.ui.sheets.CommentsSheet
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@ -58,8 +61,8 @@ class CommentsRepliesFragment : Fragment() {
|
||||
null,
|
||||
videoId,
|
||||
viewModel.channelAvatar,
|
||||
comment,
|
||||
viewModel.handleLink
|
||||
isRepliesAdapter = true,
|
||||
handleLink = viewModel.handleLink
|
||||
) {
|
||||
viewModel.commentsSheetDismiss?.invoke()
|
||||
}
|
||||
@ -100,7 +103,9 @@ class CommentsRepliesFragment : Fragment() {
|
||||
|
||||
binding.commentsRV.viewTreeObserver.addOnScrollChangedListener(scrollListener)
|
||||
|
||||
viewModel.selectedCommentLiveData.postValue(comment.repliesPage)
|
||||
val commentRepliesFlow = Pager(PagingConfig(20, enablePlaceholders = false)) {
|
||||
CommentRepliesPagingSource(videoId, comment)
|
||||
}.flow
|
||||
|
||||
viewLifecycleOwner.lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
@ -111,7 +116,7 @@ class CommentsRepliesFragment : Fragment() {
|
||||
}
|
||||
|
||||
launch {
|
||||
viewModel.commentRepliesFlow.collect {
|
||||
commentRepliesFlow.collect {
|
||||
repliesAdapter.submitData(it)
|
||||
}
|
||||
}
|
||||
|
@ -9,14 +9,11 @@ import androidx.paging.Pager
|
||||
import androidx.paging.PagingConfig
|
||||
import androidx.paging.cachedIn
|
||||
import com.github.libretube.ui.models.sources.CommentPagingSource
|
||||
import com.github.libretube.ui.models.sources.CommentRepliesPagingSource
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
|
||||
class CommentsViewModel : ViewModel() {
|
||||
val videoIdLiveData = MutableLiveData<String>()
|
||||
val selectedCommentLiveData = MutableLiveData<String>()
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
val commentsFlow = videoIdLiveData.asFlow()
|
||||
@ -27,16 +24,6 @@ class CommentsViewModel : ViewModel() {
|
||||
}
|
||||
.cachedIn(viewModelScope)
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
val commentRepliesFlow = videoIdLiveData.asFlow()
|
||||
.combine(selectedCommentLiveData.asFlow()) { videoId, comment -> videoId to comment }
|
||||
.flatMapLatest { (videoId, commentPage) ->
|
||||
Pager(PagingConfig(20, enablePlaceholders = false)) {
|
||||
CommentRepliesPagingSource(videoId, commentPage)
|
||||
}.flow
|
||||
}
|
||||
.cachedIn(viewModelScope)
|
||||
|
||||
val commentSheetExpand = MutableLiveData<Boolean?>()
|
||||
|
||||
var channelAvatar: String? = null
|
||||
|
@ -7,15 +7,21 @@ import com.github.libretube.api.obj.Comment
|
||||
|
||||
class CommentRepliesPagingSource(
|
||||
private val videoId: String,
|
||||
private val commentNextPage: String?
|
||||
private val originalComment: Comment
|
||||
) : PagingSource<String, Comment>() {
|
||||
override fun getRefreshKey(state: PagingState<String, Comment>) = null
|
||||
|
||||
override suspend fun load(params: LoadParams<String>): LoadResult<String, Comment> {
|
||||
return try {
|
||||
val key = params.key.orEmpty().ifEmpty { commentNextPage.orEmpty() }
|
||||
val key = params.key.orEmpty().ifEmpty { originalComment.repliesPage.orEmpty() }
|
||||
val result = RetrofitInstance.api.getCommentsNextPage(videoId, key)
|
||||
LoadResult.Page(result.comments, null, result.nextpage)
|
||||
|
||||
val replies = result.comments.toMutableList()
|
||||
if (params.key.isNullOrEmpty()) {
|
||||
replies.add(0, originalComment)
|
||||
}
|
||||
|
||||
LoadResult.Page(replies, null, result.nextpage)
|
||||
} catch (e: Exception) {
|
||||
LoadResult.Error(e)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user