Merge pull request #4858 from Bnyro/master

fix: crash after being idle in comments sheet main fragment
This commit is contained in:
Bnyro 2023-09-26 12:58:05 +02:00 committed by GitHub
commit 44870a181c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,7 +18,6 @@ import com.github.libretube.ui.sheets.CommentsSheet
class CommentsMainFragment : Fragment() { class CommentsMainFragment : Fragment() {
private var _binding: FragmentCommentsBinding? = null private var _binding: FragmentCommentsBinding? = null
private val binding get() = _binding!!
private lateinit var commentsAdapter: CommentsAdapter private lateinit var commentsAdapter: CommentsAdapter
@ -30,7 +29,7 @@ class CommentsMainFragment : Fragment() {
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
_binding = FragmentCommentsBinding.inflate(inflater, container, false) _binding = FragmentCommentsBinding.inflate(inflater, container, false)
return binding.root return _binding!!.root
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -41,28 +40,26 @@ class CommentsMainFragment : Fragment() {
binding.commentsRV.layoutManager = layoutManager binding.commentsRV.layoutManager = layoutManager
binding.commentsRV.setItemViewCacheSize(20) binding.commentsRV.setItemViewCacheSize(20)
val commentsSheet = parentFragment as? CommentsSheet
commentsSheet?.binding?.btnScrollToTop?.setOnClickListener {
// scroll back to the top / first comment
_binding?.commentsRV?.smoothScrollToPosition(0)
viewModel.currentCommentsPosition = 0
}
binding.commentsRV.viewTreeObserver.addOnScrollChangedListener { binding.commentsRV.viewTreeObserver.addOnScrollChangedListener {
val viewBinding = _binding ?: return@addOnScrollChangedListener val viewBinding = _binding ?: return@addOnScrollChangedListener
// save the last scroll position to become used next time when the sheet is opened // save the last scroll position to become used next time when the sheet is opened
viewModel.currentCommentsPosition = layoutManager.findFirstVisibleItemPosition() viewModel.currentCommentsPosition = layoutManager.findFirstVisibleItemPosition()
// hide or show the scroll to top button // hide or show the scroll to top button
val commentsSheetBinding = (parentFragment as? CommentsSheet)?.binding commentsSheet?.binding?.btnScrollToTop?.isVisible = viewModel.currentCommentsPosition != 0
commentsSheetBinding?.btnScrollToTop?.isVisible = viewModel.currentCommentsPosition != 0
commentsSheetBinding?.btnScrollToTop?.setOnClickListener {
// scroll back to the top / first comment
viewBinding.commentsRV.smoothScrollToPosition(0)
viewModel.currentCommentsPosition = 0
}
if (!viewBinding.commentsRV.canScrollVertically(1)) { if (!viewBinding.commentsRV.canScrollVertically(1)) {
viewModel.fetchNextComments() viewModel.fetchNextComments()
} }
} }
(parentFragment as CommentsSheet).updateFragmentInfo( commentsSheet?.updateFragmentInfo(false, getString(R.string.comments))
false,
getString(R.string.comments)
)
commentsAdapter = CommentsAdapter( commentsAdapter = CommentsAdapter(
this, this,
@ -83,19 +80,21 @@ class CommentsMainFragment : Fragment() {
// listen for new comments to be loaded // listen for new comments to be loaded
viewModel.commentsPage.observe(viewLifecycleOwner) { viewModel.commentsPage.observe(viewLifecycleOwner) {
val viewBinding = _binding ?: return@observe
if (it == null) return@observe if (it == null) return@observe
binding.progress.isGone = true viewBinding.progress.isGone = true
if (it.disabled) { if (it.disabled) {
binding.errorTV.isVisible = true viewBinding.errorTV.isVisible = true
return@observe return@observe
} }
(parentFragment as CommentsSheet).updateFragmentInfo( commentsSheet?.updateFragmentInfo(
false, false,
"${getString(R.string.comments)} (${it.commentCount.formatShort()})" "${getString(R.string.comments)} (${it.commentCount.formatShort()})"
) )
if (it.comments.isEmpty()) { if (it.comments.isEmpty()) {
binding.errorTV.text = getString(R.string.no_comments_available) viewBinding.errorTV.text = getString(R.string.no_comments_available)
binding.errorTV.isVisible = true viewBinding.errorTV.isVisible = true
return@observe return@observe
} }