diff --git a/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt index e7353844d..1ec1c4e25 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/CommentsMainFragment.kt @@ -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()) ) } } diff --git a/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt b/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt index 839a4ab8a..b317ef8c6 100644 --- a/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt +++ b/app/src/main/java/com/github/libretube/ui/models/CommentsViewModel.kt @@ -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() + val commentCountLiveData = MutableLiveData() @OptIn(ExperimentalCoroutinesApi::class) val commentsFlow = videoIdLiveData.asFlow() .flatMapLatest { Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) { - CommentPagingSource(it) + CommentPagingSource(it) { + commentCountLiveData.updateIfChanged(it) + } }.flow } .cachedIn(viewModelScope) diff --git a/app/src/main/java/com/github/libretube/ui/models/sources/CommentPagingSource.kt b/app/src/main/java/com/github/libretube/ui/models/sources/CommentPagingSource.kt index d571fd4d7..efbf339c2 100644 --- a/app/src/main/java/com/github/libretube/ui/models/sources/CommentPagingSource.kt +++ b/app/src/main/java/com/github/libretube/ui/models/sources/CommentPagingSource.kt @@ -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() { +class CommentPagingSource( + private val videoId: String, + private val onCommentCount: (Long) -> Unit +) : PagingSource() { override fun getRefreshKey(state: PagingState) = null override suspend fun load(params: LoadParams): LoadResult { @@ -13,6 +16,9 @@ class CommentPagingSource(private val videoId: String) : PagingSource 0) onCommentCount(result.commentCount) + LoadResult.Page(result.comments, null, result.nextpage) } catch (e: Exception) { LoadResult.Error(e)