convert adapters to viewbinding

This commit is contained in:
Bnyro 2022-07-01 11:37:14 +02:00
parent e6aefe3c93
commit 873fcf9e62
4 changed files with 131 additions and 126 deletions

View File

@ -11,6 +11,8 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.ChannelSubscriptionRowBinding
import com.github.libretube.databinding.VideoChannelRowBinding
import com.github.libretube.dialogs.VideoOptionsDialog import com.github.libretube.dialogs.VideoOptionsDialog
import com.github.libretube.fragments.PlayerFragment import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.obj.StreamItem import com.github.libretube.obj.StreamItem
@ -22,6 +24,8 @@ class ChannelAdapter(
private val childFragmentManager: FragmentManager private val childFragmentManager: FragmentManager
) : ) :
RecyclerView.Adapter<ChannelViewHolder>() { RecyclerView.Adapter<ChannelViewHolder>() {
private lateinit var binding: VideoChannelRowBinding
override fun getItemCount(): Int { override fun getItemCount(): Int {
return videoFeed.size return videoFeed.size
} }
@ -33,21 +37,21 @@ class ChannelAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChannelViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChannelViewHolder {
val layoutInflater = LayoutInflater.from(parent.context) val layoutInflater = LayoutInflater.from(parent.context)
val cell = layoutInflater.inflate(R.layout.video_channel_row, parent, false) binding = VideoChannelRowBinding.inflate(layoutInflater, parent, false)
return ChannelViewHolder(cell) return ChannelViewHolder(binding.root)
} }
override fun onBindViewHolder(holder: ChannelViewHolder, position: Int) { override fun onBindViewHolder(holder: ChannelViewHolder, position: Int) {
val trending = videoFeed[position] val trending = videoFeed[position]
holder.v.findViewById<TextView>(R.id.channel_description).text = trending.title binding.apply {
holder.v.findViewById<TextView>(R.id.channel_views).text = channelDescription.text = trending.title
channelViews.text =
trending.views.formatShort() + "" + trending.views.formatShort() + "" +
DateUtils.getRelativeTimeSpanString(trending.uploaded!!) DateUtils.getRelativeTimeSpanString(trending.uploaded!!)
holder.v.findViewById<TextView>(R.id.channel_duration).text = channelDuration.text =
DateUtils.formatElapsedTime(trending.duration!!) DateUtils.formatElapsedTime(trending.duration!!)
val thumbnailImage = holder.v.findViewById<ImageView>(R.id.channel_thumbnail) Picasso.get().load(trending.thumbnail).into(channelThumbnail)
Picasso.get().load(trending.thumbnail).into(thumbnailImage) root.setOnClickListener {
holder.v.setOnClickListener {
var bundle = Bundle() var bundle = Bundle()
bundle.putString("videoId", trending.url!!.replace("/watch?v=", "")) bundle.putString("videoId", trending.url!!.replace("/watch?v=", ""))
var frag = PlayerFragment() var frag = PlayerFragment()
@ -60,13 +64,14 @@ class ChannelAdapter(
.replace(R.id.container, frag) .replace(R.id.container, frag)
.commitNow() .commitNow()
} }
holder.v.setOnLongClickListener { root.setOnLongClickListener {
val videoId = trending.url!!.replace("/watch?v=", "") val videoId = trending.url!!.replace("/watch?v=", "")
VideoOptionsDialog(videoId, holder.v.context) VideoOptionsDialog(videoId, holder.v.context)
.show(childFragmentManager, VideoOptionsDialog.TAG) .show(childFragmentManager, VideoOptionsDialog.TAG)
true true
} }
} }
}
} }
class ChannelViewHolder(val v: View) : RecyclerView.ViewHolder(v) { class ChannelViewHolder(val v: View) : RecyclerView.ViewHolder(v) {

View File

@ -3,10 +3,8 @@ package com.github.libretube.adapters
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R import com.github.libretube.databinding.ChapterColumnBinding
import com.github.libretube.obj.ChapterSegment import com.github.libretube.obj.ChapterSegment
import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.ExoPlayer
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
@ -16,26 +14,26 @@ class ChaptersAdapter(
private val exoPlayer: ExoPlayer private val exoPlayer: ExoPlayer
) : RecyclerView.Adapter<ChaptersViewHolder>() { ) : RecyclerView.Adapter<ChaptersViewHolder>() {
val TAG = "ChaptersAdapter" val TAG = "ChaptersAdapter"
private lateinit var binding: ChapterColumnBinding
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChaptersViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChaptersViewHolder {
val layoutInflater = LayoutInflater.from(parent.context) val layoutInflater = LayoutInflater.from(parent.context)
val cell = layoutInflater.inflate(R.layout.chapter_column, parent, false) binding = ChapterColumnBinding.inflate(layoutInflater, parent, false)
return ChaptersViewHolder(cell) return ChaptersViewHolder(binding.root)
} }
override fun onBindViewHolder(holder: ChaptersViewHolder, position: Int) { override fun onBindViewHolder(holder: ChaptersViewHolder, position: Int) {
val chapter = chapters[position] val chapter = chapters[position]
val chapterImage = holder.v.findViewById<ImageView>(R.id.chapter_image) binding.apply {
Picasso.get().load(chapter.image).fit().centerCrop().into(chapterImage) Picasso.get().load(chapter.image).fit().centerCrop().into(chapterImage)
val chapterTitle = holder.v.findViewById<TextView>(R.id.chapter_title)
chapterTitle.text = chapter.title chapterTitle.text = chapter.title
holder.v.setOnClickListener { root.setOnClickListener {
val chapterStart = chapter.start!!.toLong() * 1000 // s -> ms val chapterStart = chapter.start!!.toLong() * 1000 // s -> ms
exoPlayer.seekTo(chapterStart) exoPlayer.seekTo(chapterStart)
} }
} }
}
override fun getItemCount(): Int { override fun getItemCount(): Int {
return chapters.size return chapters.size

View File

@ -14,6 +14,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.MainActivity import com.github.libretube.MainActivity
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.CommentsRowBinding
import com.github.libretube.obj.Comment import com.github.libretube.obj.Comment
import com.github.libretube.obj.CommentsPage import com.github.libretube.obj.CommentsPage
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
@ -29,8 +30,9 @@ class CommentsAdapter(
private val videoId: String, private val videoId: String,
private val comments: MutableList<Comment> private val comments: MutableList<Comment>
) : RecyclerView.Adapter<CommentsViewHolder>() { ) : RecyclerView.Adapter<CommentsViewHolder>() {
private val TAG = "CommentsAdapter" private val TAG = "CommentsAdapter"
private lateinit var binding: CommentsRowBinding
private var isLoading = false private var isLoading = false
private var nextpage = "" private var nextpage = ""
private var repliesPage = CommentsPage() private var repliesPage = CommentsPage()
@ -42,34 +44,34 @@ class CommentsAdapter(
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommentsViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommentsViewHolder {
val commentsView = val layoutInflater = LayoutInflater.from(parent.context)
LayoutInflater.from(parent.context).inflate(R.layout.comments_row, parent, false) binding = CommentsRowBinding.inflate(layoutInflater, parent, false)
return CommentsViewHolder(commentsView) return CommentsViewHolder(binding.root)
} }
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: CommentsViewHolder, position: Int) { override fun onBindViewHolder(holder: CommentsViewHolder, position: Int) {
holder.v.findViewById<TextView>(R.id.comment_infos).text = val comment = comments[position]
comments[position].author.toString() + binding.apply {
"" + comments[position].commentedTime.toString() commentInfos.text =
holder.v.findViewById<TextView>(R.id.comment_text).text = comment.author.toString() +
comments[position].commentText.toString() "" + comment.commentedTime.toString()
val channelImage = holder.v.findViewById<ImageView>(R.id.commentor_image) commentText.text =
Picasso.get().load(comments[position].thumbnail).fit().centerCrop().into(channelImage) comment.commentText.toString()
holder.v.findViewById<TextView>(R.id.likes_textView).text = Picasso.get().load(comment.thumbnail).fit().centerCrop().into(commentorImage)
comments[position].likeCount?.toLong().formatShort() likesTextView.text =
if (comments[position].verified == true) { comment.likeCount?.toLong().formatShort()
holder.v.findViewById<ImageView>(R.id.verified_imageView).visibility = View.VISIBLE if (comment.verified == true) {
verifiedImageView.visibility = View.VISIBLE
} }
if (comments[position].pinned == true) { if (comment.pinned == true) {
holder.v.findViewById<ImageView>(R.id.pinned_imageView).visibility = View.VISIBLE pinnedImageView.visibility = View.VISIBLE
} }
if (comments[position].hearted == true) { if (comment.hearted == true) {
holder.v.findViewById<ImageView>(R.id.hearted_imageView).visibility = View.VISIBLE heartedImageView.visibility = View.VISIBLE
} }
channelImage.setOnClickListener { commentorImage.setOnClickListener {
val activity = holder.v.context as MainActivity val activity = holder.v.context as MainActivity
val bundle = bundleOf("channel_id" to comments[position].commentorUrl) val bundle = bundleOf("channel_id" to comment.commentorUrl)
activity.navController.navigate(R.id.channel, bundle) activity.navController.navigate(R.id.channel, bundle)
try { try {
val mainMotionLayout = activity.findViewById<MotionLayout>(R.id.mainMotionLayout) val mainMotionLayout = activity.findViewById<MotionLayout>(R.id.mainMotionLayout)
@ -80,42 +82,40 @@ class CommentsAdapter(
} catch (e: Exception) { } catch (e: Exception) {
} }
} }
val repliesRecView = holder.v.findViewById<RecyclerView>(R.id.replies_recView)
repliesRecView.layoutManager = LinearLayoutManager(holder.v.context) repliesRecView.layoutManager = LinearLayoutManager(holder.v.context)
val repliesAdapter = RepliesAdapter(CommentsPage().comments) val repliesAdapter = RepliesAdapter(CommentsPage().comments)
repliesRecView.adapter = repliesAdapter repliesRecView.adapter = repliesAdapter
holder.v.setOnClickListener { root.setOnClickListener {
if (repliesAdapter.itemCount == 0) { if (repliesAdapter.itemCount == 0) {
if (comments[position].repliesPage != null) { if (comment.repliesPage != null) {
nextpage = comments[position].repliesPage!! nextpage = comment.repliesPage
fetchReplies(nextpage, repliesAdapter) fetchReplies(nextpage, repliesAdapter)
} else { } else {
Toast.makeText(holder.v.context, R.string.no_replies, Toast.LENGTH_SHORT).show() Toast.makeText(holder.v.context, R.string.no_replies, Toast.LENGTH_SHORT).show()
} }
// repliesAdapter.updateItems(repliesPage.comments)
} else { } else {
repliesAdapter.clear() repliesAdapter.clear()
} }
} }
} }
}
override fun getItemCount(): Int { override fun getItemCount(): Int {
return comments.size return comments.size
} }
private fun fetchReplies(nextpage: String, repliesAdapter: RepliesAdapter) { private fun fetchReplies(nextPage: String, repliesAdapter: RepliesAdapter) {
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
if (!isLoading) { if (!isLoading) {
isLoading = true isLoading = true
try { try {
repliesPage = RetrofitInstance.api.getCommentsNextPage(videoId, nextpage) repliesPage = RetrofitInstance.api.getCommentsNextPage(videoId, nextPage)
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")
} catch (e: HttpException) { } catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response," + e.response()) Log.e(TAG, "HttpException, unexpected response," + e.response())
} }
// nextpage = if (repliesPage.nextpage!! != null) repliesPage.nextpage!! else ""
repliesAdapter.updateItems(repliesPage.comments) repliesAdapter.updateItems(repliesPage.comments)
isLoading = false isLoading = false
} }

View File

@ -10,6 +10,7 @@ import androidx.core.os.bundleOf
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.MainActivity import com.github.libretube.MainActivity
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.RepliesRowBinding
import com.github.libretube.obj.Comment import com.github.libretube.obj.Comment
import com.github.libretube.util.formatShort import com.github.libretube.util.formatShort
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
@ -19,8 +20,7 @@ class RepliesAdapter(
) : RecyclerView.Adapter<RepliesViewHolder>() { ) : RecyclerView.Adapter<RepliesViewHolder>() {
private val TAG = "RepliesAdapter" private val TAG = "RepliesAdapter"
private var isLoading = false private lateinit var binding: RepliesRowBinding
private var nextPage = ""
fun clear() { fun clear() {
val size: Int = replies.size val size: Int = replies.size
@ -35,33 +35,34 @@ class RepliesAdapter(
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RepliesViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RepliesViewHolder {
var repliesView = val layoutInflater = LayoutInflater.from(parent.context)
LayoutInflater.from(parent.context).inflate(R.layout.replies_row, parent, false) binding = RepliesRowBinding.inflate(layoutInflater, parent, false)
return RepliesViewHolder(repliesView) return RepliesViewHolder(binding.root)
} }
override fun onBindViewHolder(holder: RepliesViewHolder, position: Int) { override fun onBindViewHolder(holder: RepliesViewHolder, position: Int) {
holder.v.findViewById<TextView>(R.id.comment_infos).text = binding.apply {
replies[position].author.toString() + val reply = replies[position]
"" + replies[position].commentedTime.toString() commentInfos.text =
holder.v.findViewById<TextView>(R.id.comment_text).text = reply.author.toString() +
replies[position].commentText.toString() "" + reply.commentedTime.toString()
val channelImage = holder.v.findViewById<ImageView>(R.id.commentor_image) commentText.text =
Picasso.get().load(replies[position].thumbnail).fit().centerCrop().into(channelImage) reply.commentText.toString()
holder.v.findViewById<TextView>(R.id.likes_textView).text = Picasso.get().load(reply.thumbnail).fit().centerCrop().into(commentorImage)
replies[position].likeCount?.toLong().formatShort() likesTextView.text =
if (replies[position].verified == true) { reply.likeCount?.toLong().formatShort()
holder.v.findViewById<ImageView>(R.id.verified_imageView).visibility = View.VISIBLE if (reply.verified == true) {
verifiedImageView.visibility = View.VISIBLE
} }
if (replies[position].pinned == true) { if (reply.pinned == true) {
holder.v.findViewById<ImageView>(R.id.pinned_imageView).visibility = View.VISIBLE pinnedImageView.visibility = View.VISIBLE
} }
if (replies[position].hearted == true) { if (reply.hearted == true) {
holder.v.findViewById<ImageView>(R.id.hearted_imageView).visibility = View.VISIBLE heartedImageView.visibility = View.VISIBLE
} }
channelImage.setOnClickListener { commentorImage.setOnClickListener {
val activity = holder.v.context as MainActivity val activity = holder.v.context as MainActivity
val bundle = bundleOf("channel_id" to replies[position].commentorUrl) val bundle = bundleOf("channel_id" to reply.commentorUrl)
activity.navController.navigate(R.id.channel, bundle) activity.navController.navigate(R.id.channel, bundle)
try { try {
val mainMotionLayout = activity.findViewById<MotionLayout>(R.id.mainMotionLayout) val mainMotionLayout = activity.findViewById<MotionLayout>(R.id.mainMotionLayout)
@ -73,6 +74,7 @@ class RepliesAdapter(
} }
} }
} }
}
override fun getItemCount(): Int { override fun getItemCount(): Int {
return replies.size return replies.size