LibreTube/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt

73 lines
2.8 KiB
Kotlin
Raw Normal View History

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.ViewGroup
import androidx.appcompat.app.AppCompatActivity
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
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-07-16 01:07:44 +05:30
import com.github.libretube.util.ConnectionHelper
2022-06-10 18:33:48 +05:30
import com.github.libretube.util.formatShort
2022-02-05 15:00:29 +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
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-02 20:31:24 +05:30
val binding = VideoChannelRowBinding.inflate(layoutInflater, parent, false)
return ChannelViewHolder(binding)
2022-02-05 15:00:29 +05:30
}
override fun onBindViewHolder(holder: ChannelViewHolder, position: Int) {
val trending = videoFeed[position]
2022-07-02 20:31:24 +05:30
holder.binding.apply {
2022-07-01 15:07:14 +05:30
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!!)
2022-07-16 01:07:44 +05:30
ConnectionHelper.loadImage(trending.thumbnail, channelThumbnail)
2022-07-01 15:07:14 +05:30
root.setOnClickListener {
var bundle = Bundle()
bundle.putString("videoId", trending.url!!.replace("/watch?v=", ""))
var frag = PlayerFragment()
frag.arguments = bundle
2022-07-02 20:31:24 +05:30
val activity = root.context as AppCompatActivity
2022-07-01 15:07:14 +05:30
activity.supportFragmentManager.beginTransaction()
.remove(PlayerFragment())
.commit()
activity.supportFragmentManager.beginTransaction()
.replace(R.id.container, frag)
.commitNow()
}
root.setOnLongClickListener {
val videoId = trending.url!!.replace("/watch?v=", "")
2022-07-02 20:31:24 +05:30
VideoOptionsDialog(videoId, root.context)
2022-07-01 15:07:14 +05:30
.show(childFragmentManager, VideoOptionsDialog.TAG)
true
}
}
2022-02-05 15:00:29 +05:30
}
}
2022-05-21 13:32:04 +05:30
2022-07-02 20:31:24 +05:30
class ChannelViewHolder(val binding: VideoChannelRowBinding) : RecyclerView.ViewHolder(binding.root)