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: display comments count (again) #6501

Merged
merged 1 commit into from
Sep 17, 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 @@ -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
Loading