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,38 +37,39 @@ 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
trending.views.formatShort() + "" + channelViews.text =
DateUtils.getRelativeTimeSpanString(trending.uploaded!!) trending.views.formatShort() + "" +
holder.v.findViewById<TextView>(R.id.channel_duration).text = DateUtils.getRelativeTimeSpanString(trending.uploaded!!)
DateUtils.formatElapsedTime(trending.duration!!) channelDuration.text =
val thumbnailImage = holder.v.findViewById<ImageView>(R.id.channel_thumbnail) DateUtils.formatElapsedTime(trending.duration!!)
Picasso.get().load(trending.thumbnail).into(thumbnailImage) Picasso.get().load(trending.thumbnail).into(channelThumbnail)
holder.v.setOnClickListener { root.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()
frag.arguments = bundle frag.arguments = bundle
val activity = holder.v.context as AppCompatActivity val activity = holder.v.context as AppCompatActivity
activity.supportFragmentManager.beginTransaction() activity.supportFragmentManager.beginTransaction()
.remove(PlayerFragment()) .remove(PlayerFragment())
.commit() .commit()
activity.supportFragmentManager.beginTransaction() activity.supportFragmentManager.beginTransaction()
.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
}
} }
} }
} }

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,24 +14,24 @@ 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)
chapterTitle.text = chapter.title
val chapterTitle = holder.v.findViewById<TextView>(R.id.chapter_title) root.setOnClickListener {
chapterTitle.text = chapter.title val chapterStart = chapter.start!!.toLong() * 1000 // s -> ms
exoPlayer.seekTo(chapterStart)
holder.v.setOnClickListener { }
val chapterStart = chapter.start!!.toLong() * 1000 // s -> ms
exoPlayer.seekTo(chapterStart)
} }
} }

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,59 +44,58 @@ 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) {
holder.v.findViewById<ImageView>(R.id.pinned_imageView).visibility = View.VISIBLE
}
if (comments[position].hearted == true) {
holder.v.findViewById<ImageView>(R.id.hearted_imageView).visibility = View.VISIBLE
}
channelImage.setOnClickListener {
val activity = holder.v.context as MainActivity
val bundle = bundleOf("channel_id" to comments[position].commentorUrl)
activity.navController.navigate(R.id.channel, bundle)
try {
val mainMotionLayout = activity.findViewById<MotionLayout>(R.id.mainMotionLayout)
if (mainMotionLayout.progress == 0.toFloat()) {
mainMotionLayout.transitionToEnd()
activity.findViewById<MotionLayout>(R.id.playerMotionLayout).transitionToEnd()
}
} catch (e: Exception) {
} }
} if (comment.pinned == true) {
val repliesRecView = holder.v.findViewById<RecyclerView>(R.id.replies_recView) pinnedImageView.visibility = View.VISIBLE
repliesRecView.layoutManager = LinearLayoutManager(holder.v.context) }
val repliesAdapter = RepliesAdapter(CommentsPage().comments) if (comment.hearted == true) {
repliesRecView.adapter = repliesAdapter heartedImageView.visibility = View.VISIBLE
holder.v.setOnClickListener { }
if (repliesAdapter.itemCount == 0) { commentorImage.setOnClickListener {
if (comments[position].repliesPage != null) { val activity = holder.v.context as MainActivity
nextpage = comments[position].repliesPage!! val bundle = bundleOf("channel_id" to comment.commentorUrl)
fetchReplies(nextpage, repliesAdapter) activity.navController.navigate(R.id.channel, bundle)
} else { try {
Toast.makeText(holder.v.context, R.string.no_replies, Toast.LENGTH_SHORT).show() val mainMotionLayout = activity.findViewById<MotionLayout>(R.id.mainMotionLayout)
if (mainMotionLayout.progress == 0.toFloat()) {
mainMotionLayout.transitionToEnd()
activity.findViewById<MotionLayout>(R.id.playerMotionLayout).transitionToEnd()
}
} catch (e: Exception) {
}
}
repliesRecView.layoutManager = LinearLayoutManager(holder.v.context)
val repliesAdapter = RepliesAdapter(CommentsPage().comments)
repliesRecView.adapter = repliesAdapter
root.setOnClickListener {
if (repliesAdapter.itemCount == 0) {
if (comment.repliesPage != null) {
nextpage = comment.repliesPage
fetchReplies(nextpage, repliesAdapter)
} else {
Toast.makeText(holder.v.context, R.string.no_replies, Toast.LENGTH_SHORT).show()
}
} else {
repliesAdapter.clear()
} }
// repliesAdapter.updateItems(repliesPage.comments)
} else {
repliesAdapter.clear()
} }
} }
} }
@ -103,19 +104,18 @@ class CommentsAdapter(
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,41 +35,43 @@ 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) { }
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 { }
val activity = holder.v.context as MainActivity commentorImage.setOnClickListener {
val bundle = bundleOf("channel_id" to replies[position].commentorUrl) val activity = holder.v.context as MainActivity
activity.navController.navigate(R.id.channel, bundle) val bundle = bundleOf("channel_id" to reply.commentorUrl)
try { activity.navController.navigate(R.id.channel, bundle)
val mainMotionLayout = activity.findViewById<MotionLayout>(R.id.mainMotionLayout) try {
if (mainMotionLayout.progress == 0.toFloat()) { val mainMotionLayout = activity.findViewById<MotionLayout>(R.id.mainMotionLayout)
mainMotionLayout.transitionToEnd() if (mainMotionLayout.progress == 0.toFloat()) {
activity.findViewById<MotionLayout>(R.id.playerMotionLayout).transitionToEnd() mainMotionLayout.transitionToEnd()
activity.findViewById<MotionLayout>(R.id.playerMotionLayout).transitionToEnd()
}
} catch (e: Exception) {
} }
} catch (e: Exception) {
} }
} }
} }