From 873fcf9e62be9c12835ad731672674eab2394e0b Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 1 Jul 2022 11:37:14 +0200 Subject: [PATCH] convert adapters to viewbinding --- .../libretube/adapters/ChannelAdapter.kt | 61 +++++----- .../libretube/adapters/ChaptersAdapter.kt | 24 ++-- .../libretube/adapters/CommentsAdapter.kt | 104 +++++++++--------- .../libretube/adapters/RepliesAdapter.kt | 68 ++++++------ 4 files changed, 131 insertions(+), 126 deletions(-) diff --git a/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt index 7dbf5d9d7..d6ea63c02 100644 --- a/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt @@ -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() { + private lateinit var binding: VideoChannelRowBinding + override fun getItemCount(): Int { return videoFeed.size } @@ -33,38 +37,39 @@ 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(R.id.channel_description).text = trending.title - holder.v.findViewById(R.id.channel_views).text = - trending.views.formatShort() + " • " + - DateUtils.getRelativeTimeSpanString(trending.uploaded!!) - holder.v.findViewById(R.id.channel_duration).text = - DateUtils.formatElapsedTime(trending.duration!!) - val thumbnailImage = holder.v.findViewById(R.id.channel_thumbnail) - Picasso.get().load(trending.thumbnail).into(thumbnailImage) - holder.v.setOnClickListener { - var bundle = Bundle() - bundle.putString("videoId", trending.url!!.replace("/watch?v=", "")) - var frag = PlayerFragment() - frag.arguments = bundle - val activity = holder.v.context as AppCompatActivity - activity.supportFragmentManager.beginTransaction() - .remove(PlayerFragment()) - .commit() - activity.supportFragmentManager.beginTransaction() - .replace(R.id.container, frag) - .commitNow() - } - holder.v.setOnLongClickListener { - val videoId = trending.url!!.replace("/watch?v=", "") - VideoOptionsDialog(videoId, holder.v.context) - .show(childFragmentManager, VideoOptionsDialog.TAG) - true + binding.apply { + channelDescription.text = trending.title + channelViews.text = + trending.views.formatShort() + " • " + + DateUtils.getRelativeTimeSpanString(trending.uploaded!!) + channelDuration.text = + DateUtils.formatElapsedTime(trending.duration!!) + Picasso.get().load(trending.thumbnail).into(channelThumbnail) + root.setOnClickListener { + var bundle = Bundle() + bundle.putString("videoId", trending.url!!.replace("/watch?v=", "")) + var frag = PlayerFragment() + frag.arguments = bundle + val activity = holder.v.context as AppCompatActivity + activity.supportFragmentManager.beginTransaction() + .remove(PlayerFragment()) + .commit() + activity.supportFragmentManager.beginTransaction() + .replace(R.id.container, frag) + .commitNow() + } + root.setOnLongClickListener { + val videoId = trending.url!!.replace("/watch?v=", "") + VideoOptionsDialog(videoId, holder.v.context) + .show(childFragmentManager, VideoOptionsDialog.TAG) + true + } } } } diff --git a/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt b/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt index 4f625c3aa..b197a6202 100644 --- a/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt @@ -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,24 +14,24 @@ class ChaptersAdapter( private val exoPlayer: ExoPlayer ) : RecyclerView.Adapter() { 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(R.id.chapter_image) - Picasso.get().load(chapter.image).fit().centerCrop().into(chapterImage) + binding.apply { + Picasso.get().load(chapter.image).fit().centerCrop().into(chapterImage) + chapterTitle.text = chapter.title - val chapterTitle = holder.v.findViewById(R.id.chapter_title) - chapterTitle.text = chapter.title - - holder.v.setOnClickListener { - val chapterStart = chapter.start!!.toLong() * 1000 // s -> ms - exoPlayer.seekTo(chapterStart) + root.setOnClickListener { + val chapterStart = chapter.start!!.toLong() * 1000 // s -> ms + exoPlayer.seekTo(chapterStart) + } } } diff --git a/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt b/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt index 3ce71ac4a..0f7088278 100644 --- a/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt @@ -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 ) : RecyclerView.Adapter() { - private val TAG = "CommentsAdapter" + private lateinit var binding: CommentsRowBinding + private var isLoading = false private var nextpage = "" private var repliesPage = CommentsPage() @@ -42,59 +44,58 @@ 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(R.id.comment_infos).text = - comments[position].author.toString() + - " • " + comments[position].commentedTime.toString() - holder.v.findViewById(R.id.comment_text).text = - comments[position].commentText.toString() - val channelImage = holder.v.findViewById(R.id.commentor_image) - Picasso.get().load(comments[position].thumbnail).fit().centerCrop().into(channelImage) - holder.v.findViewById(R.id.likes_textView).text = - comments[position].likeCount?.toLong().formatShort() - if (comments[position].verified == true) { - holder.v.findViewById(R.id.verified_imageView).visibility = View.VISIBLE - } - if (comments[position].pinned == true) { - holder.v.findViewById(R.id.pinned_imageView).visibility = View.VISIBLE - } - if (comments[position].hearted == true) { - holder.v.findViewById(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(R.id.mainMotionLayout) - if (mainMotionLayout.progress == 0.toFloat()) { - mainMotionLayout.transitionToEnd() - activity.findViewById(R.id.playerMotionLayout).transitionToEnd() - } - } catch (e: Exception) { + 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 } - } - val repliesRecView = holder.v.findViewById(R.id.replies_recView) - repliesRecView.layoutManager = LinearLayoutManager(holder.v.context) - val repliesAdapter = RepliesAdapter(CommentsPage().comments) - repliesRecView.adapter = repliesAdapter - holder.v.setOnClickListener { - if (repliesAdapter.itemCount == 0) { - if (comments[position].repliesPage != null) { - nextpage = comments[position].repliesPage!! - fetchReplies(nextpage, repliesAdapter) - } else { - Toast.makeText(holder.v.context, R.string.no_replies, Toast.LENGTH_SHORT).show() + if (comment.pinned == true) { + pinnedImageView.visibility = View.VISIBLE + } + if (comment.hearted == true) { + heartedImageView.visibility = View.VISIBLE + } + commentorImage.setOnClickListener { + val activity = holder.v.context as MainActivity + val bundle = bundleOf("channel_id" to comment.commentorUrl) + activity.navController.navigate(R.id.channel, bundle) + try { + val mainMotionLayout = activity.findViewById(R.id.mainMotionLayout) + if (mainMotionLayout.progress == 0.toFloat()) { + mainMotionLayout.transitionToEnd() + activity.findViewById(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 } - 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 } diff --git a/app/src/main/java/com/github/libretube/adapters/RepliesAdapter.kt b/app/src/main/java/com/github/libretube/adapters/RepliesAdapter.kt index 7cd46cfc8..43819b104 100644 --- a/app/src/main/java/com/github/libretube/adapters/RepliesAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/RepliesAdapter.kt @@ -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() { private val TAG = "RepliesAdapter" - private var isLoading = false - private var nextPage = "" + private lateinit var binding: RepliesRowBinding fun clear() { val size: Int = replies.size @@ -35,41 +35,43 @@ 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(R.id.comment_infos).text = - replies[position].author.toString() + - " • " + replies[position].commentedTime.toString() - holder.v.findViewById(R.id.comment_text).text = - replies[position].commentText.toString() - val channelImage = holder.v.findViewById(R.id.commentor_image) - Picasso.get().load(replies[position].thumbnail).fit().centerCrop().into(channelImage) - holder.v.findViewById(R.id.likes_textView).text = - replies[position].likeCount?.toLong().formatShort() - if (replies[position].verified == true) { - holder.v.findViewById(R.id.verified_imageView).visibility = View.VISIBLE - } - if (replies[position].pinned == true) { - holder.v.findViewById(R.id.pinned_imageView).visibility = View.VISIBLE - } - if (replies[position].hearted == true) { - holder.v.findViewById(R.id.hearted_imageView).visibility = View.VISIBLE - } - channelImage.setOnClickListener { - val activity = holder.v.context as MainActivity - val bundle = bundleOf("channel_id" to replies[position].commentorUrl) - activity.navController.navigate(R.id.channel, bundle) - try { - val mainMotionLayout = activity.findViewById(R.id.mainMotionLayout) - if (mainMotionLayout.progress == 0.toFloat()) { - mainMotionLayout.transitionToEnd() - activity.findViewById(R.id.playerMotionLayout).transitionToEnd() + 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 (reply.pinned == true) { + pinnedImageView.visibility = View.VISIBLE + } + if (reply.hearted == true) { + heartedImageView.visibility = View.VISIBLE + } + commentorImage.setOnClickListener { + val activity = holder.v.context as MainActivity + val bundle = bundleOf("channel_id" to reply.commentorUrl) + activity.navController.navigate(R.id.channel, bundle) + try { + val mainMotionLayout = activity.findViewById(R.id.mainMotionLayout) + if (mainMotionLayout.progress == 0.toFloat()) { + mainMotionLayout.transitionToEnd() + activity.findViewById(R.id.playerMotionLayout).transitionToEnd() + } + } catch (e: Exception) { } - } catch (e: Exception) { } } }