Skip to content

Commit

Permalink
Merge pull request #6501 from Bnyro/master
Browse files Browse the repository at this point in the history
fix: display comments count (again)
  • Loading branch information
Bnyro authored Sep 17, 2024
2 parents 7da12c8 + b7261b0 commit 1f759a2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,16 @@ class CommentsMainFragment : Fragment() {
launch {
viewModel.commentsFlow.collect {
commentPagingAdapter.submitData(it)
}
}

launch {
viewModel.commentCountLiveData.observe(viewLifecycleOwner) { commentCount ->
if (commentCount == null) return@observe

val commentCount = commentPagingAdapter.itemCount.toLong().formatShort()
commentsSheet?.updateFragmentInfo(
false,
getString(R.string.comments_count, commentCount)
getString(R.string.comments_count, commentCount.formatShort())
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import com.github.libretube.extensions.updateIfChanged
import com.github.libretube.ui.models.sources.CommentPagingSource
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flatMapLatest

class CommentsViewModel : ViewModel() {
val videoIdLiveData = MutableLiveData<String>()
val commentCountLiveData = MutableLiveData<Long>()

@OptIn(ExperimentalCoroutinesApi::class)
val commentsFlow = videoIdLiveData.asFlow()
.flatMapLatest {
Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) {
CommentPagingSource(it)
CommentPagingSource(it) {
commentCountLiveData.updateIfChanged(it)
}
}.flow
}
.cachedIn(viewModelScope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import androidx.paging.PagingState
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.Comment

class CommentPagingSource(private val videoId: String) : PagingSource<String, Comment>() {
class CommentPagingSource(
private val videoId: String,
private val onCommentCount: (Long) -> Unit
) : PagingSource<String, Comment>() {
override fun getRefreshKey(state: PagingState<String, Comment>) = null

override suspend fun load(params: LoadParams<String>): LoadResult<String, Comment> {
return try {
val result = params.key?.let {
RetrofitInstance.api.getCommentsNextPage(videoId, it)
} ?: RetrofitInstance.api.getComments(videoId)

if (result.commentCount > 0) onCommentCount(result.commentCount)

LoadResult.Page(result.comments, null, result.nextpage)
} catch (e: Exception) {
LoadResult.Error(e)
Expand Down

0 comments on commit 1f759a2

Please sign in to comment.