2022-02-01 21:22:06 +05:30
|
|
|
package com.github.libretube.adapters
|
2021-12-11 19:27:16 +05:30
|
|
|
|
2021-12-12 17:38:23 +05:30
|
|
|
import android.os.Bundle
|
2022-02-15 02:17:50 +05:30
|
|
|
import android.text.format.DateUtils
|
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
|
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
|
2022-05-06 19:17:02 +05:30
|
|
|
import androidx.fragment.app.FragmentManager
|
2021-12-11 19:27:16 +05:30
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2022-05-27 07:11:42 +05:30
|
|
|
import com.github.libretube.MainActivity
|
2022-02-01 21:22:06 +05:30
|
|
|
import com.github.libretube.R
|
2022-06-03 00:40:16 +05:30
|
|
|
import com.github.libretube.dialogs.VideoOptionsDialog
|
|
|
|
import com.github.libretube.fragments.PlayerFragment
|
2022-05-10 22:05:51 +05:30
|
|
|
import com.github.libretube.obj.StreamItem
|
2022-06-10 18:33:48 +05:30
|
|
|
import com.github.libretube.util.formatShort
|
2022-05-27 07:11:42 +05:30
|
|
|
import com.squareup.picasso.Picasso
|
2021-12-11 19:27:16 +05:30
|
|
|
|
2022-05-06 19:17:02 +05:30
|
|
|
class TrendingAdapter(
|
|
|
|
private val videoFeed: List<StreamItem>,
|
|
|
|
private val childFragmentManager: FragmentManager
|
2022-06-04 22:47:36 +05:30
|
|
|
) : RecyclerView.Adapter<TrendingViewHolder>() {
|
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
|
|
|
}
|
|
|
|
|
2022-06-04 22:47:36 +05:30
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TrendingViewHolder {
|
2021-12-11 19:27:16 +05:30
|
|
|
val layoutInflater = LayoutInflater.from(parent.context)
|
2022-05-20 03:52:10 +05:30
|
|
|
val cell = layoutInflater.inflate(R.layout.trending_row, parent, false)
|
2022-06-04 22:47:36 +05:30
|
|
|
return TrendingViewHolder(cell)
|
2021-12-11 19:27:16 +05:30
|
|
|
}
|
|
|
|
|
2022-06-04 22:47:36 +05:30
|
|
|
override fun onBindViewHolder(holder: TrendingViewHolder, 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
|
2022-05-21 13:32:04 +05:30
|
|
|
holder.v.findViewById<TextView>(R.id.textView_channel).text =
|
|
|
|
trending.uploaderName + " • " +
|
|
|
|
trending.views.formatShort() + " • " +
|
|
|
|
DateUtils.getRelativeTimeSpanString(trending.uploaded!!)
|
2021-12-11 19:27:16 +05:30
|
|
|
val thumbnailImage = holder.v.findViewById<ImageView>(R.id.thumbnail)
|
2022-06-08 11:13:37 +05:30
|
|
|
val thumbnailDuration = holder.v.findViewById<TextView>(R.id.thumbnail_duration)
|
2022-06-08 14:37:47 +05:30
|
|
|
if (trending.duration != -1L) {
|
|
|
|
thumbnailDuration.text = DateUtils.formatElapsedTime(trending.duration!!)
|
2022-06-08 11:13:37 +05:30
|
|
|
} else {
|
|
|
|
thumbnailDuration.text = holder.v.context.getString(R.string.live)
|
|
|
|
thumbnailDuration.setBackgroundColor(R.attr.colorPrimaryDark)
|
|
|
|
}
|
2021-12-11 19:27:16 +05:30
|
|
|
val channelImage = holder.v.findViewById<ImageView>(R.id.channel_image)
|
2022-05-20 03:52:10 +05:30
|
|
|
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()
|
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
} catch (e: Exception) {
|
2022-02-06 10:08:50 +05:30
|
|
|
}
|
2021-12-12 17:38:23 +05:30
|
|
|
}
|
2022-02-27 23:21:17 +05:30
|
|
|
if (trending.thumbnail!!.isEmpty()) {
|
2022-05-20 03:52:10 +05:30
|
|
|
} else {
|
2022-02-27 23:21:17 +05:30
|
|
|
Picasso.get().load(trending.thumbnail).into(thumbnailImage)
|
|
|
|
}
|
|
|
|
if (trending.uploaderAvatar!!.isEmpty()) {
|
2022-05-20 03:52:10 +05:30
|
|
|
} else {
|
2022-02-27 23:21:17 +05:30
|
|
|
Picasso.get().load(trending.uploaderAvatar).into(channelImage)
|
|
|
|
}
|
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
holder.v.setOnClickListener {
|
2021-12-12 17:38:23 +05:30
|
|
|
var bundle = Bundle()
|
2022-05-20 03:52:10 +05:30
|
|
|
bundle.putString("videoId", trending.url!!.replace("/watch?v=", ""))
|
2021-12-12 17:38:23 +05:30
|
|
|
var frag = PlayerFragment()
|
|
|
|
frag.arguments = bundle
|
|
|
|
val activity = holder.v.context as AppCompatActivity
|
|
|
|
activity.supportFragmentManager.beginTransaction()
|
2021-12-13 01:47:55 +05:30
|
|
|
.remove(PlayerFragment())
|
|
|
|
.commit()
|
|
|
|
activity.supportFragmentManager.beginTransaction()
|
|
|
|
.replace(R.id.container, frag)
|
2021-12-12 17:38:23 +05:30
|
|
|
.commitNow()
|
|
|
|
}
|
2022-05-06 19:17:02 +05:30
|
|
|
holder.v.setOnLongClickListener {
|
2022-05-07 01:45:00 +05:30
|
|
|
val videoId = trending.url!!.replace("/watch?v=", "")
|
2022-05-27 22:52:45 +05:30
|
|
|
VideoOptionsDialog(videoId, holder.v.context)
|
|
|
|
.show(childFragmentManager, VideoOptionsDialog.TAG)
|
2022-05-06 19:17:02 +05:30
|
|
|
true
|
|
|
|
}
|
2021-12-11 19:27:16 +05:30
|
|
|
}
|
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
|
2022-06-04 22:47:36 +05:30
|
|
|
class TrendingViewHolder(val v: View) : RecyclerView.ViewHolder(v) {
|
2021-12-12 17:38:23 +05:30
|
|
|
init {
|
|
|
|
}
|
2022-05-06 01:29:38 +05:30
|
|
|
}
|