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

View File

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

View File

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

View File

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