From 6670a673068028b3fc0fb211fc0ac6029f90442d Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 2 May 2024 11:02:37 +0200 Subject: [PATCH] fix: comments reloading on sheet reopen and scroll position not restored --- .../libretube/ui/fragments/CommentsMainFragment.kt | 13 +++++++++++++ .../github/libretube/ui/fragments/PlayerFragment.kt | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) 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 013f1637f..11da92878 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 @@ -19,7 +19,9 @@ import com.github.libretube.extensions.formatShort import com.github.libretube.ui.adapters.CommentPagingAdapter import com.github.libretube.ui.models.CommentsViewModel import com.github.libretube.ui.sheets.CommentsSheet +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class CommentsMainFragment : Fragment() { private var _binding: FragmentCommentsBinding? = null @@ -74,11 +76,22 @@ class CommentsMainFragment : Fragment() { binding.commentsRV.adapter = commentPagingAdapter viewLifecycleOwner.lifecycleScope.launch { + var restoredScrollPosition = false + repeatOnLifecycle(Lifecycle.State.STARTED) { launch { commentPagingAdapter.loadStateFlow.collect { 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) { binding.errorTV.text = getString(R.string.no_comments_available) binding.errorTV.isVisible = true diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index c487bb1df..12600ee01 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -528,7 +528,9 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { // set the max height to not cover the currently playing video commentsViewModel.handleLink = this::handleLink updateMaxSheetHeight() - commentsViewModel.videoIdLiveData.postValue(videoId) + if (commentsViewModel.videoIdLiveData.value != videoId) { + commentsViewModel.videoIdLiveData.postValue(videoId) + } commentsViewModel.channelAvatar = streams.uploaderAvatar CommentsSheet().show(childFragmentManager) }