mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-15 14:50:30 +05:30
Merge pull request #665 from Bnyro/master
convert search adapter to viewbinding
This commit is contained in:
commit
6936e76fa7
@ -3,10 +3,7 @@ package com.github.libretube.adapters
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.text.format.DateUtils
|
import android.text.format.DateUtils
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.ImageView
|
|
||||||
import android.widget.TextView
|
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.os.bundleOf
|
import androidx.core.os.bundleOf
|
||||||
import androidx.fragment.app.FragmentManager
|
import androidx.fragment.app.FragmentManager
|
||||||
@ -30,7 +27,7 @@ class SearchAdapter(
|
|||||||
RecyclerView.Adapter<SearchViewHolder>() {
|
RecyclerView.Adapter<SearchViewHolder>() {
|
||||||
|
|
||||||
fun updateItems(newItems: List<SearchItem>) {
|
fun updateItems(newItems: List<SearchItem>) {
|
||||||
var searchItemsSize = searchItems.size
|
val searchItemsSize = searchItems.size
|
||||||
searchItems.addAll(newItems)
|
searchItems.addAll(newItems)
|
||||||
notifyItemRangeInserted(searchItemsSize, newItems.size)
|
notifyItemRangeInserted(searchItemsSize, newItems.size)
|
||||||
}
|
}
|
||||||
@ -42,17 +39,30 @@ class SearchAdapter(
|
|||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchViewHolder {
|
||||||
val layoutInflater = LayoutInflater.from(parent.context)
|
val layoutInflater = LayoutInflater.from(parent.context)
|
||||||
|
|
||||||
val binding = when (viewType) {
|
return when (viewType) {
|
||||||
0 -> VideoSearchRowBinding.inflate(layoutInflater, parent, false)
|
0 -> SearchViewHolder(
|
||||||
1 -> ChannelSearchRowBinding.inflate(layoutInflater, parent, false)
|
VideoSearchRowBinding.inflate(layoutInflater, parent, false)
|
||||||
2 -> PlaylistSearchRowBinding.inflate(layoutInflater, parent, false)
|
)
|
||||||
|
1 -> SearchViewHolder(
|
||||||
|
ChannelSearchRowBinding.inflate(layoutInflater, parent, false)
|
||||||
|
)
|
||||||
|
2 -> SearchViewHolder(
|
||||||
|
PlaylistSearchRowBinding.inflate(layoutInflater, parent, false)
|
||||||
|
)
|
||||||
else -> throw IllegalArgumentException("Invalid type")
|
else -> throw IllegalArgumentException("Invalid type")
|
||||||
}
|
}
|
||||||
return SearchViewHolder(binding.root, childFragmentManager)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: SearchViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: SearchViewHolder, position: Int) {
|
||||||
holder.bind(searchItems[position])
|
val searchItem = searchItems[position]
|
||||||
|
|
||||||
|
val videoRowBinding = holder.videoRowBinding
|
||||||
|
val channelRowBinding = holder.channelRowBinding
|
||||||
|
val playlistRowBinding = holder.playlistRowBinding
|
||||||
|
|
||||||
|
if (videoRowBinding != null) bindWatch(searchItem, videoRowBinding)
|
||||||
|
else if (channelRowBinding != null) bindChannel(searchItem, channelRowBinding)
|
||||||
|
else if (playlistRowBinding != null) bindPlaylist(searchItem, playlistRowBinding)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getItemViewType(position: Int): Int {
|
override fun getItemViewType(position: Int): Int {
|
||||||
@ -63,44 +73,33 @@ class SearchAdapter(
|
|||||||
else -> 3
|
else -> 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class SearchViewHolder(
|
private fun bindWatch(item: SearchItem, binding: VideoSearchRowBinding) {
|
||||||
private val v: View,
|
binding.apply {
|
||||||
private val childFragmentManager: FragmentManager
|
Picasso.get().load(item.thumbnail).fit().centerCrop().into(searchThumbnail)
|
||||||
) : RecyclerView.ViewHolder(v) {
|
|
||||||
|
|
||||||
private fun bindWatch(item: SearchItem) {
|
|
||||||
val thumbnailImage = v.findViewById<ImageView>(R.id.search_thumbnail)
|
|
||||||
Picasso.get().load(item.thumbnail).fit().centerCrop().into(thumbnailImage)
|
|
||||||
val thumbnailDuration = v.findViewById<TextView>(R.id.search_thumbnail_duration)
|
|
||||||
if (item.duration != -1L) {
|
if (item.duration != -1L) {
|
||||||
thumbnailDuration.text = DateUtils.formatElapsedTime(item.duration!!)
|
searchThumbnailDuration.text = DateUtils.formatElapsedTime(item.duration!!)
|
||||||
} else {
|
} else {
|
||||||
thumbnailDuration.text = v.context.getString(R.string.live)
|
searchThumbnailDuration.text = root.context.getString(R.string.live)
|
||||||
thumbnailDuration.setBackgroundColor(R.attr.colorPrimaryDark)
|
searchThumbnailDuration.setBackgroundColor(R.attr.colorPrimaryDark)
|
||||||
}
|
}
|
||||||
val channelImage = v.findViewById<ImageView>(R.id.search_channel_image)
|
Picasso.get().load(item.uploaderAvatar).fit().centerCrop().into(searchChannelImage)
|
||||||
Picasso.get().load(item.uploaderAvatar).fit().centerCrop().into(channelImage)
|
searchDescription.text = item.title
|
||||||
val title = v.findViewById<TextView>(R.id.search_description)
|
|
||||||
title.text = item.title
|
|
||||||
val views = v.findViewById<TextView>(R.id.search_views)
|
|
||||||
val viewsString = if (item.views?.toInt() != -1) item.views.formatShort() else ""
|
val viewsString = if (item.views?.toInt() != -1) item.views.formatShort() else ""
|
||||||
val uploadDate = if (item.uploadedDate != null) item.uploadedDate else ""
|
val uploadDate = if (item.uploadedDate != null) item.uploadedDate else ""
|
||||||
views.text =
|
searchViews.text =
|
||||||
if (viewsString != "" && uploadDate != "") {
|
if (viewsString != "" && uploadDate != "") {
|
||||||
"$viewsString • $uploadDate"
|
"$viewsString • $uploadDate"
|
||||||
} else {
|
} else {
|
||||||
viewsString + uploadDate
|
viewsString + uploadDate
|
||||||
}
|
}
|
||||||
val channelName = v.findViewById<TextView>(R.id.search_channel_name)
|
searchChannelName.text = item.uploaderName
|
||||||
channelName.text = item.uploaderName
|
root.setOnClickListener {
|
||||||
v.setOnClickListener {
|
val bundle = Bundle()
|
||||||
var bundle = Bundle()
|
|
||||||
bundle.putString("videoId", item.url!!.replace("/watch?v=", ""))
|
bundle.putString("videoId", item.url!!.replace("/watch?v=", ""))
|
||||||
var frag = PlayerFragment()
|
val frag = PlayerFragment()
|
||||||
frag.arguments = bundle
|
frag.arguments = bundle
|
||||||
val activity = v.context as AppCompatActivity
|
val activity = root.context as AppCompatActivity
|
||||||
activity.supportFragmentManager.beginTransaction()
|
activity.supportFragmentManager.beginTransaction()
|
||||||
.remove(PlayerFragment())
|
.remove(PlayerFragment())
|
||||||
.commit()
|
.commit()
|
||||||
@ -108,72 +107,76 @@ class SearchViewHolder(
|
|||||||
.replace(R.id.container, frag)
|
.replace(R.id.container, frag)
|
||||||
.commitNow()
|
.commitNow()
|
||||||
}
|
}
|
||||||
v.setOnLongClickListener {
|
root.setOnLongClickListener {
|
||||||
val videoId = item.url!!.replace("/watch?v=", "")
|
val videoId = item.url!!.replace("/watch?v=", "")
|
||||||
VideoOptionsDialog(videoId, v.context)
|
VideoOptionsDialog(videoId, root.context)
|
||||||
.show(childFragmentManager, VideoOptionsDialog.TAG)
|
.show(childFragmentManager, VideoOptionsDialog.TAG)
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
channelImage.setOnClickListener {
|
searchChannelImage.setOnClickListener {
|
||||||
val activity = v.context as MainActivity
|
val activity = root.context as MainActivity
|
||||||
val bundle = bundleOf("channel_id" to item.uploaderUrl)
|
val bundle = bundleOf("channel_id" to item.uploaderUrl)
|
||||||
activity.navController.navigate(R.id.channelFragment, bundle)
|
activity.navController.navigate(R.id.channelFragment, bundle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun bindChannel(item: SearchItem) {
|
private fun bindChannel(item: SearchItem, binding: ChannelSearchRowBinding) {
|
||||||
val channelImage = v.findViewById<ImageView>(R.id.search_channel_image)
|
binding.apply {
|
||||||
Picasso.get().load(item.thumbnail).fit().centerCrop().into(channelImage)
|
Picasso.get().load(item.thumbnail).fit().centerCrop().into(searchChannelImage)
|
||||||
val channelName = v.findViewById<TextView>(R.id.search_channel_name)
|
searchChannelName.text = item.name
|
||||||
channelName.text = item.name
|
searchViews.text = root.context.getString(
|
||||||
val channelViews = v.findViewById<TextView>(R.id.search_views)
|
|
||||||
channelViews.text = v.context.getString(
|
|
||||||
R.string.subscribers,
|
R.string.subscribers,
|
||||||
item.subscribers.formatShort()
|
item.subscribers.formatShort()
|
||||||
) + " • " + v.context.getString(R.string.videoCount, item.videos.toString())
|
) + " • " + root.context.getString(R.string.videoCount, item.videos.toString())
|
||||||
v.setOnClickListener {
|
root.setOnClickListener {
|
||||||
val activity = v.context as MainActivity
|
val activity = root.context as MainActivity
|
||||||
val bundle = bundleOf("channel_id" to item.url)
|
val bundle = bundleOf("channel_id" to item.url)
|
||||||
activity.navController.navigate(R.id.channelFragment, bundle)
|
activity.navController.navigate(R.id.channelFragment, bundle)
|
||||||
}
|
}
|
||||||
// todo sub button
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun bindPlaylist(item: SearchItem) {
|
private fun bindPlaylist(item: SearchItem, binding: PlaylistSearchRowBinding) {
|
||||||
val playlistImage = v.findViewById<ImageView>(R.id.search_thumbnail)
|
binding.apply {
|
||||||
Picasso.get().load(item.thumbnail).fit().centerCrop().into(playlistImage)
|
Picasso.get().load(item.thumbnail).fit().centerCrop().into(searchThumbnail)
|
||||||
val playlistNumber = v.findViewById<TextView>(R.id.search_playlist_number)
|
if (item.videos?.toInt() != -1) searchPlaylistNumber.text = item.videos.toString()
|
||||||
if (item.videos?.toInt() != -1) playlistNumber.text = item.videos.toString()
|
searchDescription.text = item.name
|
||||||
val playlistName = v.findViewById<TextView>(R.id.search_description)
|
searchName.text = item.uploaderName
|
||||||
playlistName.text = item.name
|
|
||||||
val playlistChannelName = v.findViewById<TextView>(R.id.search_name)
|
|
||||||
playlistChannelName.text = item.uploaderName
|
|
||||||
val playlistVideosNumber = v.findViewById<TextView>(R.id.search_playlist_videos)
|
|
||||||
if (item.videos?.toInt() != -1) {
|
if (item.videos?.toInt() != -1) {
|
||||||
playlistVideosNumber.text =
|
searchPlaylistNumber.text =
|
||||||
v.context.getString(R.string.videoCount, item.videos.toString())
|
root.context.getString(R.string.videoCount, item.videos.toString())
|
||||||
}
|
}
|
||||||
v.setOnClickListener {
|
root.setOnClickListener {
|
||||||
// playlist clicked
|
// playlist clicked
|
||||||
val activity = v.context as MainActivity
|
val activity = root.context as MainActivity
|
||||||
val bundle = bundleOf("playlist_id" to item.url)
|
val bundle = bundleOf("playlist_id" to item.url)
|
||||||
activity.navController.navigate(R.id.playlistFragment, bundle)
|
activity.navController.navigate(R.id.playlistFragment, bundle)
|
||||||
}
|
}
|
||||||
v.setOnLongClickListener {
|
root.setOnLongClickListener {
|
||||||
val playlistId = item.url!!.replace("/playlist?list=", "")
|
val playlistId = item.url!!.replace("/playlist?list=", "")
|
||||||
PlaylistOptionsDialog(playlistId, v.context)
|
PlaylistOptionsDialog(playlistId, root.context)
|
||||||
.show(childFragmentManager, "PlaylistOptionsDialog")
|
.show(childFragmentManager, "PlaylistOptionsDialog")
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
fun bind(searchItem: SearchItem) {
|
}
|
||||||
when {
|
|
||||||
searchItem.url!!.startsWith("/watch", false) -> bindWatch(searchItem)
|
class SearchViewHolder : RecyclerView.ViewHolder {
|
||||||
searchItem.url!!.startsWith("/channel", false) -> bindChannel(searchItem)
|
var videoRowBinding: VideoSearchRowBinding? = null
|
||||||
searchItem.url!!.startsWith("/playlist", false) -> bindPlaylist(searchItem)
|
var channelRowBinding: ChannelSearchRowBinding? = null
|
||||||
else -> {
|
var playlistRowBinding: PlaylistSearchRowBinding? = null
|
||||||
}
|
|
||||||
}
|
constructor(binding: VideoSearchRowBinding) : super(binding.root) {
|
||||||
|
videoRowBinding = binding
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(binding: ChannelSearchRowBinding) : super(binding.root) {
|
||||||
|
channelRowBinding = binding
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(binding: PlaylistSearchRowBinding) : super(binding.root) {
|
||||||
|
playlistRowBinding = binding
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user