fix: display comments count (again)

This commit is contained in:
Bnyro 2024-09-17 17:09:00 +02:00
parent 7da12c8047
commit b7261b018b
3 changed files with 19 additions and 4 deletions

View File

@ -108,11 +108,16 @@ class CommentsMainFragment : Fragment() {
launch { launch {
viewModel.commentsFlow.collect { viewModel.commentsFlow.collect {
commentPagingAdapter.submitData(it) commentPagingAdapter.submitData(it)
}
}
launch {
viewModel.commentCountLiveData.observe(viewLifecycleOwner) { commentCount ->
if (commentCount == null) return@observe
val commentCount = commentPagingAdapter.itemCount.toLong().formatShort()
commentsSheet?.updateFragmentInfo( commentsSheet?.updateFragmentInfo(
false, false,
getString(R.string.comments_count, commentCount) getString(R.string.comments_count, commentCount.formatShort())
) )
} }
} }

View File

@ -8,18 +8,22 @@ import androidx.lifecycle.viewModelScope
import androidx.paging.Pager import androidx.paging.Pager
import androidx.paging.PagingConfig import androidx.paging.PagingConfig
import androidx.paging.cachedIn import androidx.paging.cachedIn
import com.github.libretube.extensions.updateIfChanged
import com.github.libretube.ui.models.sources.CommentPagingSource import com.github.libretube.ui.models.sources.CommentPagingSource
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flatMapLatest
class CommentsViewModel : ViewModel() { class CommentsViewModel : ViewModel() {
val videoIdLiveData = MutableLiveData<String>() val videoIdLiveData = MutableLiveData<String>()
val commentCountLiveData = MutableLiveData<Long>()
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
val commentsFlow = videoIdLiveData.asFlow() val commentsFlow = videoIdLiveData.asFlow()
.flatMapLatest { .flatMapLatest {
Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) { Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) {
CommentPagingSource(it) CommentPagingSource(it) {
commentCountLiveData.updateIfChanged(it)
}
}.flow }.flow
} }
.cachedIn(viewModelScope) .cachedIn(viewModelScope)

View File

@ -5,7 +5,10 @@ import androidx.paging.PagingState
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.Comment 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 fun getRefreshKey(state: PagingState<String, Comment>) = null
override suspend fun load(params: LoadParams<String>): LoadResult<String, Comment> { 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 { val result = params.key?.let {
RetrofitInstance.api.getCommentsNextPage(videoId, it) RetrofitInstance.api.getCommentsNextPage(videoId, it)
} ?: RetrofitInstance.api.getComments(videoId) } ?: RetrofitInstance.api.getComments(videoId)
if (result.commentCount > 0) onCommentCount(result.commentCount)
LoadResult.Page(result.comments, null, result.nextpage) LoadResult.Page(result.comments, null, result.nextpage)
} catch (e: Exception) { } catch (e: Exception) {
LoadResult.Error(e) LoadResult.Error(e)