2022-02-05 15:00:29 +05:30
|
|
|
package com.github.libretube.adapters
|
|
|
|
|
|
|
|
import android.os.Bundle
|
2022-02-15 02:17:50 +05:30
|
|
|
import android.text.format.DateUtils
|
2022-02-05 15:00:29 +05:30
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2022-06-14 00:49:19 +05:30
|
|
|
import androidx.fragment.app.FragmentManager
|
2022-02-05 15:00:29 +05:30
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import com.github.libretube.R
|
2022-07-01 15:07:14 +05:30
|
|
|
import com.github.libretube.databinding.VideoChannelRowBinding
|
2022-06-14 00:49:19 +05:30
|
|
|
import com.github.libretube.dialogs.VideoOptionsDialog
|
2022-06-03 00:40:16 +05:30
|
|
|
import com.github.libretube.fragments.PlayerFragment
|
2022-05-10 22:05:51 +05:30
|
|
|
import com.github.libretube.obj.StreamItem
|
2022-06-10 18:33:48 +05:30
|
|
|
import com.github.libretube.util.formatShort
|
2022-05-10 22:05:51 +05:30
|
|
|
import com.squareup.picasso.Picasso
|
2022-02-05 15:00:29 +05:30
|
|
|
|
2022-06-14 00:49:19 +05:30
|
|
|
class ChannelAdapter(
|
|
|
|
private val videoFeed: MutableList<StreamItem>,
|
|
|
|
private val childFragmentManager: FragmentManager
|
|
|
|
) :
|
2022-05-21 13:32:04 +05:30
|
|
|
RecyclerView.Adapter<ChannelViewHolder>() {
|
2022-07-01 15:07:14 +05:30
|
|
|
private lateinit var binding: VideoChannelRowBinding
|
|
|
|
|
2022-02-05 15:00:29 +05:30
|
|
|
override fun getItemCount(): Int {
|
|
|
|
return videoFeed.size
|
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
fun updateItems(newItems: List<StreamItem>) {
|
2022-02-05 19:39:50 +05:30
|
|
|
videoFeed.addAll(newItems)
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
2022-02-05 15:00:29 +05:30
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChannelViewHolder {
|
|
|
|
val layoutInflater = LayoutInflater.from(parent.context)
|
2022-07-01 15:07:14 +05:30
|
|
|
binding = VideoChannelRowBinding.inflate(layoutInflater, parent, false)
|
|
|
|
return ChannelViewHolder(binding.root)
|
2022-02-05 15:00:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: ChannelViewHolder, position: Int) {
|
|
|
|
val trending = videoFeed[position]
|
2022-07-01 15:07:14 +05:30
|
|
|
binding.apply {
|
|
|
|
channelDescription.text = trending.title
|
|
|
|
channelViews.text =
|
|
|
|
trending.views.formatShort() + " • " +
|
2022-07-01 15:41:30 +05:30
|
|
|
DateUtils.getRelativeTimeSpanString(trending.uploaded!!)
|
2022-07-01 15:07:14 +05:30
|
|
|
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
|
|
|
|
}
|
2022-06-14 00:49:19 +05:30
|
|
|
}
|
2022-02-05 15:00:29 +05:30
|
|
|
}
|
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
class ChannelViewHolder(val v: View) : RecyclerView.ViewHolder(v) {
|
2022-02-05 15:00:29 +05:30
|
|
|
init {
|
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
}
|