fix: comments reloading on sheet reopen and scroll position not restored

This commit is contained in:
Bnyro 2024-05-02 11:02:37 +02:00
parent 3b85a302ad
commit 6670a67306
2 changed files with 16 additions and 1 deletions

View File

@ -19,7 +19,9 @@ import com.github.libretube.extensions.formatShort
import com.github.libretube.ui.adapters.CommentPagingAdapter import com.github.libretube.ui.adapters.CommentPagingAdapter
import com.github.libretube.ui.models.CommentsViewModel import com.github.libretube.ui.models.CommentsViewModel
import com.github.libretube.ui.sheets.CommentsSheet import com.github.libretube.ui.sheets.CommentsSheet
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class CommentsMainFragment : Fragment() { class CommentsMainFragment : Fragment() {
private var _binding: FragmentCommentsBinding? = null private var _binding: FragmentCommentsBinding? = null
@ -74,11 +76,22 @@ class CommentsMainFragment : Fragment() {
binding.commentsRV.adapter = commentPagingAdapter binding.commentsRV.adapter = commentPagingAdapter
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
var restoredScrollPosition = false
repeatOnLifecycle(Lifecycle.State.STARTED) { repeatOnLifecycle(Lifecycle.State.STARTED) {
launch { launch {
commentPagingAdapter.loadStateFlow.collect { commentPagingAdapter.loadStateFlow.collect {
binding.progress.isVisible = it.refresh is LoadState.Loading binding.progress.isVisible = it.refresh is LoadState.Loading
if (!restoredScrollPosition && it.refresh is LoadState.NotLoading) {
viewModel.currentCommentsPosition.value?.let { position ->
withContext(Dispatchers.Main) {
binding.commentsRV.scrollToPosition(position)
}
}
restoredScrollPosition = true
}
if (it.append is LoadState.NotLoading && it.append.endOfPaginationReached && commentPagingAdapter.itemCount == 0) { if (it.append is LoadState.NotLoading && it.append.endOfPaginationReached && commentPagingAdapter.itemCount == 0) {
binding.errorTV.text = getString(R.string.no_comments_available) binding.errorTV.text = getString(R.string.no_comments_available)
binding.errorTV.isVisible = true binding.errorTV.isVisible = true

View File

@ -528,7 +528,9 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// set the max height to not cover the currently playing video // set the max height to not cover the currently playing video
commentsViewModel.handleLink = this::handleLink commentsViewModel.handleLink = this::handleLink
updateMaxSheetHeight() updateMaxSheetHeight()
commentsViewModel.videoIdLiveData.postValue(videoId) if (commentsViewModel.videoIdLiveData.value != videoId) {
commentsViewModel.videoIdLiveData.postValue(videoId)
}
commentsViewModel.channelAvatar = streams.uploaderAvatar commentsViewModel.channelAvatar = streams.uploaderAvatar
CommentsSheet().show(childFragmentManager) CommentsSheet().show(childFragmentManager)
} }