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

65 lines
2.5 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.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
2022-06-10 18:32:51 +05:30
import com.github.libretube.util.formatShort
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
import com.squareup.picasso.Picasso
2022-02-05 15:00:29 +05:30
2022-05-21 13:32:04 +05:30
class ChannelAdapter(private val videoFeed: MutableList<StreamItem>) :
RecyclerView.Adapter<ChannelViewHolder>() {
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-05-20 03:52:10 +05:30
val cell = layoutInflater.inflate(R.layout.video_channel_row, parent, false)
2022-02-05 15:00:29 +05:30
return ChannelViewHolder(cell)
}
override fun onBindViewHolder(holder: ChannelViewHolder, position: Int) {
val trending = videoFeed[position]
holder.v.findViewById<TextView>(R.id.channel_description).text = trending.title
2022-05-21 13:32:04 +05:30
holder.v.findViewById<TextView>(R.id.channel_views).text =
trending.views.formatShort() + "" +
DateUtils.getRelativeTimeSpanString(trending.uploaded!!)
holder.v.findViewById<TextView>(R.id.channel_duration).text =
DateUtils.formatElapsedTime(trending.duration!!)
2022-02-05 15:00:29 +05:30
val thumbnailImage = holder.v.findViewById<ImageView>(R.id.channel_thumbnail)
Picasso.get().load(trending.thumbnail).into(thumbnailImage)
2022-05-20 03:52:10 +05:30
holder.v.setOnClickListener {
2022-02-05 15:00:29 +05:30
var bundle = Bundle()
2022-05-20 03:52:10 +05:30
bundle.putString("videoId", trending.url!!.replace("/watch?v=", ""))
2022-02-05 15:00:29 +05:30
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()
}
}
}
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
}