Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: various comment/replies related issues #5830

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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
Expand All @@ -23,6 +25,7 @@
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

Expand Down Expand Up @@ -58,8 +61,8 @@
null,
videoId,
viewModel.channelAvatar,
comment,
viewModel.handleLink
isRepliesAdapter = true,
handleLink = viewModel.handleLink

Check failure on line 65 in app/src/main/java/com/github/libretube/ui/fragments/CommentsRepliesFragment.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Missing trailing comma before ")" Raw Output: app/src/main/java/com/github/libretube/ui/fragments/CommentsRepliesFragment.kt:65:46: error: Missing trailing comma before ")" (standard:trailing-comma-on-call-site)
) {
viewModel.commentsSheetDismiss?.invoke()
}
Expand Down Expand Up @@ -100,7 +103,9 @@

binding.commentsRV.viewTreeObserver.addOnScrollChangedListener(scrollListener)

viewModel.selectedCommentLiveData.postValue(comment.repliesPage)
val commentRepliesFlow = Pager(PagingConfig(20, enablePlaceholders = false)) {

Check failure on line 106 in app/src/main/java/com/github/libretube/ui/fragments/CommentsRepliesFragment.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/ui/fragments/CommentsRepliesFragment.kt:106:34: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
CommentRepliesPagingSource(videoId, comment)
}.flow

viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
Expand All @@ -111,7 +116,7 @@
}

launch {
viewModel.commentRepliesFlow.collect {
commentRepliesFlow.collect {
repliesAdapter.submitData(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@

class CommentRepliesPagingSource(
private val videoId: String,
private val commentNextPage: String?
private val originalComment: Comment

Check failure on line 10 in app/src/main/java/com/github/libretube/ui/models/sources/CommentRepliesPagingSource.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Missing trailing comma before ")" Raw Output: app/src/main/java/com/github/libretube/ui/models/sources/CommentRepliesPagingSource.kt:10:41: error: Missing trailing comma before ")" (standard:trailing-comma-on-declaration-site)
) : 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)
}
Expand Down
Loading