Added ability to load more comments through scrolling

This commit is contained in:
Bnyro 2022-05-08 19:32:13 +02:00
parent 39439039af
commit 2bf69b39f9
5 changed files with 57 additions and 10 deletions

View File

@ -13,6 +13,9 @@ interface PipedApi {
@GET("comments/{videoId}")
suspend fun getComments(@Path("videoId") videoId: String): CommentsPage
@GET("nextpage/comments/{videoId}")
suspend fun getCommentsNextPage(@Path("videoId") videoId: String, @Query("nextpage") nextPage: String): CommentsPage
@GET("search")
suspend fun getSearchResults(
@Query("q") searchQuery: String,

View File

@ -33,6 +33,7 @@ import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.adapters.ChannelAdapter
import com.github.libretube.adapters.CommentsAdapter
import com.github.libretube.adapters.TrendingAdapter
import com.github.libretube.obj.PipedStream
@ -78,6 +79,8 @@ class PlayerFragment : Fragment() {
private lateinit var relatedRecView: RecyclerView
private lateinit var commentsRecView: RecyclerView
private var nextPage: String? = null
var commentsAdapter: CommentsAdapter? = null
private lateinit var exoPlayerView: StyledPlayerView
private lateinit var motionLayout: MotionLayout
private lateinit var exoPlayer: ExoPlayer
@ -195,14 +198,13 @@ class PlayerFragment : Fragment() {
}
view.findViewById<RelativeLayout>(R.id.player_title_layout).setOnClickListener {
var visible = playerDescription.isVisible
playerDescription.visibility = if (visible) View.GONE else View.VISIBLE
playerDescription.visibility =
if (playerDescription.isVisible) View.GONE else View.VISIBLE
}
view.findViewById<ConstraintLayout>(R.id.comments_toggle).setOnClickListener {
var visible = commentsRecView.isVisible
commentsRecView.visibility = if (visible) View.GONE else View.VISIBLE
commentsRecView.visibility = if (commentsRecView.isVisible) View.GONE else View.VISIBLE
relatedRecView.visibility = if (relatedRecView.isVisible) View.GONE else View.VISIBLE
}
// FullScreen button trigger
@ -230,6 +232,18 @@ class PlayerFragment : Fragment() {
isFullScreen = false
}
}
val scrollView = view.findViewById<ScrollView>(R.id.player_scrollView)
scrollView.viewTreeObserver
.addOnScrollChangedListener {
if (scrollView.getChildAt(0).bottom
== (scrollView.height + scrollView.scrollY)
) {
fetchNextComments()
}
}
commentsRecView = view.findViewById(R.id.comments_recView)
commentsRecView.layoutManager = LinearLayoutManager(view.context)
@ -500,8 +514,11 @@ class PlayerFragment : Fragment() {
}
}
})
commentsRecView.adapter = CommentsAdapter(commentsResponse.comments)
commentsAdapter = CommentsAdapter(commentsResponse.comments)
commentsRecView.adapter = commentsAdapter
nextPage = commentsResponse.nextpage
relatedRecView.adapter = TrendingAdapter(response.relatedStreams!!)
view.findViewById<TextView>(R.id.player_description).text =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(response.description, Html.FROM_HTML_MODE_COMPACT)
@ -761,4 +778,25 @@ class PlayerFragment : Fragment() {
override fun onResume() {
super.onResume()
}
private fun fetchNextComments(){
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.getCommentsNextPage(videoId!!, nextPage!!)
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response,"+e.response())
return@launchWhenCreated
}
nextPage = response.nextpage
commentsAdapter?.updateItems(response.comments!!)
}
}
run()
}
}

View File

@ -9,9 +9,15 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.formatShort
import com.github.libretube.obj.Comment
import com.github.libretube.obj.StreamItem
import com.squareup.picasso.Picasso
class CommentsAdapter(private val comments: List<Comment>): RecyclerView.Adapter<ViewHolder>(){
class CommentsAdapter(private val comments: MutableList<Comment>): RecyclerView.Adapter<ViewHolder>(){
fun updateItems(newItems: List<Comment>){
comments.addAll(newItems)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
var commentsView = LayoutInflater.from(parent.context).inflate(R.layout.comments_row, parent, false)

View File

@ -4,9 +4,9 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties
@JsonIgnoreProperties(ignoreUnknown = true)
data class CommentsPage(
val comments: List<Comment> = listOf(),
val comments: MutableList<Comment> = arrayListOf(),
val disabled: Boolean? = null,
val nextpage: String? = "",
){
constructor(): this(emptyList(),null,"")
constructor(): this(arrayListOf(),null,"")
}

View File

@ -9,7 +9,7 @@
tools:context=".PlayerFragment">
<ScrollView
android:id="@+id/scrollView2"
android:id="@+id/player_scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="?attr/colorSurface"