2021-12-11 19:27:16 +05:30
|
|
|
package xyz.btcland.libretube
|
|
|
|
|
2021-12-12 17:38:23 +05:30
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
2021-12-11 19:27:16 +05:30
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.ImageView
|
|
|
|
import android.widget.TextView
|
2021-12-12 17:38:23 +05:30
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2021-12-11 19:27:16 +05:30
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import com.squareup.picasso.Picasso
|
|
|
|
import java.math.BigDecimal
|
|
|
|
import java.math.RoundingMode
|
|
|
|
|
2021-12-12 17:38:23 +05:30
|
|
|
class TrendingAdapter(private val videoFeed: List<Video>): RecyclerView.Adapter<CustomViewHolder>() {
|
2021-12-11 19:27:16 +05:30
|
|
|
override fun getItemCount(): Int {
|
2021-12-12 17:38:23 +05:30
|
|
|
return videoFeed.size
|
2021-12-11 19:27:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-12-12 17:38:23 +05:30
|
|
|
val trending = videoFeed[position]
|
2021-12-11 19:27:16 +05:30
|
|
|
holder.v.findViewById<TextView>(R.id.textView_title).text = trending.title
|
|
|
|
holder.v.findViewById<TextView>(R.id.textView_channel).text = trending.uploaderName +" • "+ videoViews(trending.views)+" • "+trending.uploadedDate
|
|
|
|
val thumbnailImage = holder.v.findViewById<ImageView>(R.id.thumbnail)
|
|
|
|
val channelImage = holder.v.findViewById<ImageView>(R.id.channel_image)
|
2021-12-12 17:38:23 +05:30
|
|
|
channelImage.setOnClickListener{
|
|
|
|
println("channel clicked")
|
|
|
|
}
|
2021-12-11 19:27:16 +05:30
|
|
|
Picasso.get().load(trending.thumbnail).into(thumbnailImage)
|
|
|
|
Picasso.get().load(trending.uploaderAvatar).into(channelImage)
|
2021-12-12 17:38:23 +05:30
|
|
|
holder.v.setOnClickListener{
|
|
|
|
//val intent = Intent(holder.v.context, Player::class.java)
|
|
|
|
//intent.putExtra("videoId",trending.url.replace("/watch?v=",""))
|
|
|
|
//holder.v.context.startActivity(intent)
|
|
|
|
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()
|
|
|
|
.add(R.id.container, frag)
|
|
|
|
.commitNow()
|
|
|
|
}
|
2021-12-11 19:27:16 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
class CustomViewHolder(val v: View): RecyclerView.ViewHolder(v){
|
2021-12-12 17:38:23 +05:30
|
|
|
init {
|
|
|
|
}
|
2021-12-11 19:27:16 +05:30
|
|
|
}
|
|
|
|
fun videoViews(views: Int): String{
|
|
|
|
when {
|
|
|
|
views<1000 -> {
|
|
|
|
return views.toString()
|
|
|
|
}
|
|
|
|
views in 1000..999999 -> {
|
|
|
|
val decimal = BigDecimal(views/1000).setScale(0, RoundingMode.HALF_EVEN)
|
|
|
|
return decimal.toString()+"K"
|
|
|
|
}
|
|
|
|
views in 1000000..10000000 -> {
|
|
|
|
val decimal = BigDecimal(views/1000000).setScale(0, RoundingMode.HALF_EVEN)
|
|
|
|
return decimal.toString()+"M"
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
val decimal = BigDecimal(views/1000000).setScale(0, RoundingMode.HALF_EVEN)
|
|
|
|
return decimal.toString()+"M"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|