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

83 lines
3.5 KiB
Kotlin
Raw Normal View History

2022-02-12 20:00:36 +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-12 20:00:36 +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.constraintlayout.motion.widget.MotionLayout
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.MainActivity
import com.squareup.picasso.Picasso
import com.github.libretube.PlayerFragment
import com.github.libretube.R
import com.github.libretube.obj.StreamItem
2022-02-15 02:26:32 +05:30
import com.github.libretube.formatShort
2022-02-12 20:00:36 +05:30
class SubscriptionAdapter(private val videoFeed: List<StreamItem>): RecyclerView.Adapter<SubscriptionViewHolder>() {
//private var limitedVideoFeed: MutableList<String> = [""].toMutableList()
2022-02-14 09:54:46 +05:30
var i = 0
2022-02-12 20:00:36 +05:30
override fun getItemCount(): Int {
return i
}
fun updateItems(){
//limitedVideoFeed.add("")
i += 10
2022-02-13 17:23:04 +05:30
if(i>videoFeed.size)
i=videoFeed.size
2022-02-12 20:00:36 +05:30
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubscriptionViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val cell = layoutInflater.inflate(R.layout.trending_row,parent,false)
return SubscriptionViewHolder(cell)
}
override fun onBindViewHolder(holder: SubscriptionViewHolder, position: Int) {
val trending = videoFeed[position]
holder.v.findViewById<TextView>(R.id.textView_title).text = trending.title
2022-02-15 02:26:32 +05:30
holder.v.findViewById<TextView>(R.id.textView_channel).text = trending.uploaderName +""+ trending.views.formatShort()+""+ DateUtils.getRelativeTimeSpanString(trending.uploaded!!)
2022-02-12 20:00:36 +05:30
val thumbnailImage = holder.v.findViewById<ImageView>(R.id.thumbnail)
2022-02-15 02:17:50 +05:30
holder.v.findViewById<TextView>(R.id.thumbnail_duration).text = DateUtils.formatElapsedTime(trending.duration!!)
2022-02-12 20:00:36 +05:30
val channelImage = holder.v.findViewById<ImageView>(R.id.channel_image)
channelImage.setOnClickListener{
val activity = holder.v.context as MainActivity
val bundle = bundleOf("channel_id" to trending.uploaderUrl)
activity.navController.navigate(R.id.channel, bundle)
try {
val mainMotionLayout = activity.findViewById<MotionLayout>(R.id.mainMotionLayout)
if (mainMotionLayout.progress == 0.toFloat()) {
mainMotionLayout.transitionToEnd()
activity.findViewById<MotionLayout>(R.id.playerMotionLayout).transitionToEnd()
}
}catch (e: Exception){
}
}
Picasso.get().load(trending.thumbnail).into(thumbnailImage)
Picasso.get().load(trending.uploaderAvatar).into(channelImage)
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()
}
}
}
class SubscriptionViewHolder(val v: View): RecyclerView.ViewHolder(v){
init {
}
}