mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-13 22:00:30 +05:30
Remember the comments progress, scroll to top button for comments
This commit is contained in:
parent
3f7872fa43
commit
c4ccdedb93
@ -1,8 +1,6 @@
|
||||
package com.github.libretube.ui.adapters
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.text.method.LinkMovementMethod
|
||||
import android.view.*
|
||||
import android.view.ViewGroup.MarginLayoutParams
|
||||
|
@ -4,6 +4,7 @@ import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
@ -11,6 +12,7 @@ import com.github.libretube.R
|
||||
import com.github.libretube.databinding.FragmentCommentsBinding
|
||||
import com.github.libretube.ui.adapters.CommentsAdapter
|
||||
import com.github.libretube.ui.models.CommentsViewModel
|
||||
import com.github.libretube.ui.sheets.CommentsSheet
|
||||
|
||||
class CommentsMainFragment : Fragment() {
|
||||
private var _binding: FragmentCommentsBinding? = null
|
||||
@ -32,11 +34,25 @@ class CommentsMainFragment : Fragment() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
binding.commentsRV.layoutManager = LinearLayoutManager(requireContext())
|
||||
val layoutManager = LinearLayoutManager(requireContext())
|
||||
binding.commentsRV.layoutManager = layoutManager
|
||||
binding.commentsRV.setItemViewCacheSize(20)
|
||||
|
||||
binding.commentsRV.viewTreeObserver.addOnScrollChangedListener {
|
||||
if (_binding?.commentsRV?.canScrollVertically(1) == false) {
|
||||
val binding = _binding ?: return@addOnScrollChangedListener
|
||||
// save the last scroll position to become used next time when the sheet is opened
|
||||
viewModel.currentCommentsPosition = layoutManager.findFirstVisibleItemPosition()
|
||||
|
||||
// hide or show the scroll to top button
|
||||
val commentsSheetBinding = (parentFragment as? CommentsSheet)?.binding
|
||||
commentsSheetBinding?.btnScrollToTop?.isVisible = viewModel.currentCommentsPosition != 0
|
||||
commentsSheetBinding?.btnScrollToTop?.setOnClickListener {
|
||||
// scroll back to the top / first comment
|
||||
binding.commentsRV.smoothScrollToPosition(0)
|
||||
viewModel.currentCommentsPosition = 0
|
||||
}
|
||||
|
||||
if (!binding.commentsRV.canScrollVertically(1)) {
|
||||
viewModel.fetchNextComments()
|
||||
}
|
||||
}
|
||||
@ -54,6 +70,8 @@ class CommentsMainFragment : Fragment() {
|
||||
if (viewModel.commentsPage.value?.comments.orEmpty().isEmpty()) {
|
||||
binding.progress.visibility = View.VISIBLE
|
||||
viewModel.fetchComments()
|
||||
} else {
|
||||
binding.commentsRV.scrollToPosition(viewModel.currentCommentsPosition)
|
||||
}
|
||||
|
||||
// listen for new comments to be loaded
|
||||
|
@ -18,6 +18,8 @@ class CommentsViewModel : ViewModel() {
|
||||
|
||||
val commentSheetExpand = MutableLiveData<Boolean?>()
|
||||
|
||||
var currentCommentsPosition = 0
|
||||
|
||||
fun setCommentSheetExpand(value: Boolean?) {
|
||||
if (commentSheetExpand.value != value) {
|
||||
commentSheetExpand.value = value
|
||||
@ -73,5 +75,6 @@ class CommentsViewModel : ViewModel() {
|
||||
commentsPage.value = null
|
||||
videoId = null
|
||||
setCommentSheetExpand(null)
|
||||
currentCommentsPosition = 0
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ import com.github.libretube.ui.fragments.CommentsRepliesFragment
|
||||
import com.github.libretube.ui.models.CommentsViewModel
|
||||
|
||||
class CommentsSheet : ExpandedBottomSheet() {
|
||||
private lateinit var binding: CommentsSheetBinding
|
||||
lateinit var binding: CommentsSheetBinding
|
||||
private val commentsViewModel: CommentsViewModel by activityViewModels()
|
||||
|
||||
override fun onCreateView(
|
||||
|
@ -52,6 +52,16 @@
|
||||
android:textAlignment="viewStart"
|
||||
tools:text="Title" />
|
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView
|
||||
android:visibility="gone"
|
||||
android:id="@+id/btnScrollToTop"
|
||||
android:layout_width="35dp"
|
||||
android:layout_height="35dp"
|
||||
android:src="@drawable/ic_arrow_downward"
|
||||
android:rotationX="180"
|
||||
android:padding="7dp"
|
||||
android:background="?selectableItemBackgroundBorderless" />
|
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView
|
||||
android:id="@+id/btnClose"
|
||||
android:layout_width="35dp"
|
||||
|
Loading…
Reference in New Issue
Block a user