mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-13 13:50:30 +05:30
fix: display comments count (again)
This commit is contained in:
parent
7da12c8047
commit
b7261b018b
@ -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())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -5,7 +5,10 @@ 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> {
|
||||
@ -13,6 +16,9 @@ class CommentPagingSource(private val videoId: String) : PagingSource<String, Co
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user