p2play-app-android/app/src/main/java/org/libre/agosto/p2play/adapters/VideosAdapter.kt

107 lines
4.1 KiB
Kotlin
Raw Normal View History

2018-08-18 07:04:31 +02:00
package org.libre.agosto.p2play.adapters
2019-02-10 18:41:56 +01:00
import android.content.Context
2018-08-18 07:04:31 +02:00
import android.content.Intent
2024-03-21 04:04:16 +01:00
import androidx.recyclerview.widget.RecyclerView
2018-08-18 07:04:31 +02:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.view_video.view.duration
2019-02-24 17:34:12 +01:00
import org.libre.agosto.p2play.*
import org.libre.agosto.p2play.helpers.mapSeconds
2018-08-18 07:04:31 +02:00
import org.libre.agosto.p2play.models.VideoModel
import java.io.Serializable
class VideosAdapter(private val myDataset: ArrayList<VideoModel>) :
RecyclerView.Adapter<VideosAdapter.ViewHolder>() {
2019-02-10 18:41:56 +01:00
2018-08-18 07:04:31 +02:00
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder.
// Each data item is just a string in this case that is shown in a TextView.
class ViewHolder(val view: View) : RecyclerView.ViewHolder(view){
val thumb: ImageView
val userImg: ImageView
val tittle: TextView
val description: TextView
2019-02-10 18:41:56 +01:00
val context: Context
val duration: TextView
2018-08-18 07:04:31 +02:00
init {
// Define click listener for the ViewHolder's View
tittle = view.findViewById(R.id.tittleTxt)
description = view.findViewById(R.id.descriptionTxt)
thumb = view.findViewById(R.id.thumb)
userImg = view.findViewById(R.id.userImg)
2019-02-10 18:41:56 +01:00
context = view.context
duration = view.duration
2018-08-18 07:04:31 +02:00
}
}
// Create new views (invoked by the layout manager)
2024-03-21 04:04:16 +01:00
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
2018-08-18 07:04:31 +02:00
// create a new view
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.view_video, parent, false) as View
// set the view's size, margins, paddings and layout parameters
return ViewHolder(view)
}
// Replace the contents of a view (invoked by the layout manager)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.tittle.text = myDataset[position].name
2024-03-26 01:53:01 +01:00
holder.tittle.setOnClickListener {
this.launchChannelActivity(myDataset[position] as Serializable, holder.context)
}
2018-08-18 07:04:31 +02:00
Picasso.get().load("https://"+ManagerSingleton.url+myDataset[position].thumbUrl).into(holder.thumb)
holder.thumb.setOnClickListener {
2024-03-26 01:53:01 +01:00
this.launchChannelActivity(myDataset[position] as Serializable, holder.context)
2018-08-18 07:04:31 +02:00
}
2019-02-24 17:34:12 +01:00
holder.userImg.setOnClickListener {
val intent = Intent(holder.context, ChannelActivity::class.java)
intent.putExtra("channel", myDataset[position].getAccount())
holder.context.startActivity(intent)
}
2018-08-18 07:04:31 +02:00
if(myDataset[position].userImageUrl!="")
2019-02-09 21:56:56 +01:00
Picasso.get().load("https://"+ManagerSingleton.url+myDataset[position].userImageUrl).into(holder.userImg)
else
Picasso.get().load(R.drawable.default_avatar).into(holder.userImg)
2018-08-18 07:04:31 +02:00
2019-02-10 18:41:56 +01:00
val viewsText = holder.context.getString(R.string.view_text)
2019-02-09 21:56:56 +01:00
val seconds = myDataset[position].duration.toInt();
val timeString = mapSeconds(seconds)
holder.duration.text = timeString
2019-02-09 21:56:56 +01:00
holder.description.text = myDataset[position].username+" - "+myDataset[position].views+" "+viewsText
2018-08-18 07:04:31 +02:00
}
// Return the size of your dataset (invoked by the layout manager)
override fun getItemCount() = myDataset.size
2019-02-09 21:56:56 +01:00
fun clearData(){
myDataset.clear()
notifyDataSetChanged()
}
fun addData(newItems: ArrayList<VideoModel>){
2019-02-12 18:45:24 +01:00
val lastPos = myDataset.size
2019-02-09 21:56:56 +01:00
myDataset.addAll(newItems)
notifyItemRangeInserted(lastPos, newItems.size)
}
2024-03-26 01:53:01 +01:00
private fun launchChannelActivity (data: Serializable, context: Context) {
val intent = Intent(context, ReproductorActivity::class.java)
intent.putExtra("video", data)
context.startActivity(intent)
}
2018-08-18 07:04:31 +02:00
}