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
|
2019-02-24 17:34:12 +01:00
|
|
|
import org.libre.agosto.p2play.*
|
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
|
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
|
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
|
|
|
|
Picasso.get().load("https://"+ManagerSingleton.url+myDataset[position].thumbUrl).into(holder.thumb)
|
|
|
|
holder.thumb.setOnClickListener {
|
2019-02-10 18:41:56 +01:00
|
|
|
val intent = Intent(holder.context, ReproductorActivity::class.java)
|
2018-08-18 07:04:31 +02:00
|
|
|
intent.putExtra("video", myDataset[position] as Serializable)
|
2019-02-10 18:41:56 +01:00
|
|
|
holder.context.startActivity(intent)
|
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)
|
|
|
|
var timeText = holder.context.getString(R.string.timeSec_text)
|
2019-02-09 21:56:56 +01:00
|
|
|
var timeString = myDataset[position].duration.toString()
|
|
|
|
val seconds = myDataset[position].duration.toInt();
|
|
|
|
if(seconds > 60 && seconds < (60 * 60)){
|
2019-02-10 18:41:56 +01:00
|
|
|
timeText = holder.context.getString(R.string.timeMin_text)
|
2019-02-09 21:56:56 +01:00
|
|
|
timeString = (seconds / 60).toString() + ":" + (seconds % 60).toString()
|
|
|
|
}
|
|
|
|
else if(seconds > (60 * 60)){
|
2019-02-10 18:41:56 +01:00
|
|
|
timeText = holder.context.getString(R.string.timeHrs_text)
|
2019-02-09 21:56:56 +01:00
|
|
|
timeString = (seconds / 60 / 60).toString() + ":" + (seconds / 60 % 60).toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
holder.description.text = myDataset[position].username+" - "+myDataset[position].views+" "+viewsText+" - "+timeString+" "+timeText
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-18 07:04:31 +02:00
|
|
|
}
|