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

81 lines
3.3 KiB
Kotlin
Raw Normal View History

2022-02-01 21:22:06 +05:30
package com.github.libretube.adapters
import android.os.Bundle
2022-02-15 02:17:50 +05:30
import android.text.format.DateUtils
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
2022-02-06 10:08:50 +05:30
import androidx.constraintlayout.motion.widget.MotionLayout
2022-02-05 00:25:05 +05:30
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.RecyclerView
2022-02-05 00:25:05 +05:30
import com.github.libretube.MainActivity
2022-02-01 21:22:06 +05:30
import com.github.libretube.PlayerFragment
import com.github.libretube.R
2022-02-15 02:26:32 +05:30
import com.github.libretube.formatShort
2022-05-10 22:05:51 +05:30
import com.github.libretube.obj.StreamItem
import com.squareup.picasso.Picasso
2021-12-18 16:34:14 +05:30
class TrendingAdapter(private val videoFeed: List<StreamItem>): RecyclerView.Adapter<CustomViewHolder>() {
override fun getItemCount(): Int {
return videoFeed.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val cell = layoutInflater.inflate(R.layout.trending_row,parent,false)
return CustomViewHolder(cell)
}
override fun onBindViewHolder(holder: CustomViewHolder, 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!!)
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!!)
val channelImage = holder.v.findViewById<ImageView>(R.id.channel_image)
channelImage.setOnClickListener{
2022-02-05 00:25:05 +05:30
val activity = holder.v.context as MainActivity
val bundle = bundleOf("channel_id" to trending.uploaderUrl)
activity.navController.navigate(R.id.channel, bundle)
2022-02-06 10:08:50 +05:30
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){
}
}
2022-02-27 23:21:17 +05:30
if (trending.thumbnail!!.isEmpty()) {
} else{
Picasso.get().load(trending.thumbnail).into(thumbnailImage)
}
if (trending.uploaderAvatar!!.isEmpty()) {
} else{
Picasso.get().load(trending.uploaderAvatar).into(channelImage)
}
holder.v.setOnClickListener{
var bundle = Bundle()
2021-12-18 16:34:14 +05:30
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 CustomViewHolder(val v: View): RecyclerView.ViewHolder(v){
init {
}
2022-01-19 22:22:32 +05:30
}