2021-05-18 13:43:32 +00:00
|
|
|
package com.lagradost.cloudstream3.ui.result
|
|
|
|
|
2021-06-26 19:32:50 +00:00
|
|
|
import android.annotation.SuppressLint
|
2021-05-18 13:43:32 +00:00
|
|
|
import android.app.Activity
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.ImageView
|
|
|
|
import android.widget.LinearLayout
|
|
|
|
import android.widget.TextView
|
2021-06-26 19:32:50 +00:00
|
|
|
import androidx.annotation.LayoutRes
|
|
|
|
import androidx.core.widget.ContentLoadingProgressBar
|
2021-05-18 13:43:32 +00:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-06-26 19:32:50 +00:00
|
|
|
import com.bumptech.glide.Glide
|
|
|
|
import com.bumptech.glide.load.model.GlideUrl
|
2021-06-10 15:15:14 +00:00
|
|
|
import com.google.android.gms.cast.framework.CastContext
|
|
|
|
import com.google.android.gms.cast.framework.CastState
|
2021-05-18 13:43:32 +00:00
|
|
|
import com.lagradost.cloudstream3.*
|
2021-06-10 15:15:14 +00:00
|
|
|
import com.lagradost.cloudstream3.UIHelper.isCastApiAvailable
|
2021-05-18 13:43:32 +00:00
|
|
|
import kotlinx.android.synthetic.main.result_episode.view.*
|
2021-06-26 19:32:50 +00:00
|
|
|
import kotlinx.android.synthetic.main.result_episode.view.episode_holder
|
|
|
|
import kotlinx.android.synthetic.main.result_episode.view.episode_text
|
|
|
|
import kotlinx.android.synthetic.main.result_episode_large.view.*
|
2021-05-18 13:43:32 +00:00
|
|
|
|
2021-06-10 15:15:14 +00:00
|
|
|
const val ACTION_RELOAD_EPISODE = 4
|
|
|
|
const val ACTION_CHROME_CAST_EPISODE = 2
|
2021-06-29 23:14:48 +00:00
|
|
|
const val ACTION_DOWNLOAD_EPISODE = 3
|
|
|
|
const val ACTION_PLAY_EPISODE_IN_PLAYER = 1
|
2021-05-20 20:56:21 +00:00
|
|
|
|
|
|
|
data class EpisodeClickEvent(val action: Int, val data: ResultEpisode)
|
2021-05-18 13:43:32 +00:00
|
|
|
|
|
|
|
class EpisodeAdapter(
|
2021-05-20 15:22:28 +00:00
|
|
|
private var activity: Activity,
|
2021-06-15 16:07:20 +00:00
|
|
|
var cardList: List<ResultEpisode>,
|
2021-06-10 15:15:14 +00:00
|
|
|
private val resView: RecyclerView,
|
|
|
|
private val clickCallback: (EpisodeClickEvent) -> Unit,
|
2021-05-18 13:43:32 +00:00
|
|
|
) :
|
|
|
|
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
2021-06-26 19:32:50 +00:00
|
|
|
@LayoutRes
|
|
|
|
private var layout: Int = 0
|
|
|
|
fun updateLayout() {
|
|
|
|
layout = if (cardList.filter { it.poster != null }.size >= cardList.size / 2)
|
|
|
|
R.layout.result_episode_large
|
|
|
|
else R.layout.result_episode
|
|
|
|
}
|
2021-05-18 13:43:32 +00:00
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
2021-06-26 19:32:50 +00:00
|
|
|
/*val layout = if (cardList.filter { it.poster != null }.size >= cardList.size / 2)
|
|
|
|
R.layout.result_episode_large
|
|
|
|
else R.layout.result_episode*/
|
|
|
|
|
2021-05-18 13:43:32 +00:00
|
|
|
return CardViewHolder(
|
2021-06-26 19:32:50 +00:00
|
|
|
LayoutInflater.from(parent.context).inflate(layout, parent, false),
|
2021-05-18 13:43:32 +00:00
|
|
|
activity,
|
2021-05-20 15:22:28 +00:00
|
|
|
resView,
|
|
|
|
clickCallback
|
2021-05-18 13:43:32 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
|
|
|
when (holder) {
|
|
|
|
is CardViewHolder -> {
|
|
|
|
holder.bind(cardList[position])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItemCount(): Int {
|
|
|
|
return cardList.size
|
|
|
|
}
|
|
|
|
|
|
|
|
class CardViewHolder
|
2021-05-20 20:56:21 +00:00
|
|
|
constructor(
|
|
|
|
itemView: View,
|
2021-06-06 18:06:01 +00:00
|
|
|
val activity: Activity,
|
2021-05-20 20:56:21 +00:00
|
|
|
resView: RecyclerView,
|
2021-06-06 18:06:01 +00:00
|
|
|
private val clickCallback: (EpisodeClickEvent) -> Unit,
|
2021-05-20 20:56:21 +00:00
|
|
|
) : RecyclerView.ViewHolder(itemView) {
|
2021-06-26 19:32:50 +00:00
|
|
|
private val episodeViewPrecentage: View? = itemView.episode_view_procentage
|
|
|
|
private val episodeViewPercentageOff: View? = itemView.episode_view_procentage_off
|
2021-06-06 18:06:01 +00:00
|
|
|
private val episodeText: TextView = itemView.episode_text
|
2021-06-26 19:32:50 +00:00
|
|
|
private val episodeRating: TextView? = itemView.episode_rating
|
|
|
|
private val episodeDescript: TextView? = itemView.episode_descript
|
|
|
|
private val episodeProgress: ContentLoadingProgressBar? = itemView.episode_progress
|
|
|
|
private val episodePoster: ImageView? = itemView.episode_poster
|
|
|
|
|
|
|
|
// val episodeExtra: ImageView = itemView.episode_extra
|
|
|
|
// private val episodePlay: ImageView = itemView.episode_play
|
2021-06-06 18:06:01 +00:00
|
|
|
private val episodeHolder = itemView.episode_holder
|
2021-05-18 13:43:32 +00:00
|
|
|
|
2021-06-26 19:32:50 +00:00
|
|
|
@SuppressLint("SetTextI18n")
|
2021-05-18 13:43:32 +00:00
|
|
|
fun bind(card: ResultEpisode) {
|
2021-06-26 22:15:19 +00:00
|
|
|
val name = if (card.name == null) "Episode ${card.episode}" else "${card.episode}. ${card.name}"
|
|
|
|
episodeText.text = name
|
2021-05-18 13:43:32 +00:00
|
|
|
|
|
|
|
fun setWidth(v: View, procentage: Float) {
|
|
|
|
val param = LinearLayout.LayoutParams(
|
|
|
|
v.layoutParams.width,
|
|
|
|
v.layoutParams.height,
|
|
|
|
procentage
|
|
|
|
)
|
|
|
|
v.layoutParams = param
|
|
|
|
}
|
2021-06-10 15:15:14 +00:00
|
|
|
|
|
|
|
val watchProgress = card.getWatchProgress()
|
2021-06-26 19:32:50 +00:00
|
|
|
if (episodeViewPrecentage != null && episodeViewPercentageOff != null) {
|
|
|
|
setWidth(episodeViewPrecentage, watchProgress)
|
|
|
|
setWidth(episodeViewPercentageOff, 1 - watchProgress)
|
|
|
|
}
|
|
|
|
|
|
|
|
episodeProgress?.progress = (watchProgress * 50).toInt()
|
|
|
|
episodeProgress?.visibility = if (watchProgress > 0.0f) View.VISIBLE else View.GONE
|
|
|
|
|
|
|
|
if (card.poster != null) {
|
|
|
|
episodePoster?.visibility = View.VISIBLE
|
|
|
|
if (episodePoster != null) {
|
|
|
|
val glideUrl =
|
|
|
|
GlideUrl(card.poster)
|
|
|
|
activity.let {
|
|
|
|
Glide.with(it)
|
|
|
|
.load(glideUrl)
|
|
|
|
.into(episodePoster)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
episodePoster?.visibility = View.GONE
|
|
|
|
}
|
|
|
|
|
|
|
|
if (card.rating != null) {
|
2021-06-26 22:15:19 +00:00
|
|
|
episodeRating?.text = "Rated: %.1f".format(card.rating.toFloat() / 10f).replace(",", ".")
|
2021-06-26 19:32:50 +00:00
|
|
|
} else {
|
|
|
|
episodeRating?.text = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if (card.descript != null) {
|
|
|
|
episodeDescript?.visibility = View.VISIBLE
|
|
|
|
episodeDescript?.text = card.descript
|
|
|
|
} else {
|
|
|
|
episodeDescript?.visibility = View.GONE
|
|
|
|
}
|
2021-05-18 13:43:32 +00:00
|
|
|
|
2021-06-06 18:06:01 +00:00
|
|
|
episodeHolder.setOnClickListener {
|
2021-06-10 15:15:14 +00:00
|
|
|
if (activity.isCastApiAvailable()) {
|
|
|
|
val castContext = CastContext.getSharedInstance(activity)
|
2021-06-10 19:43:05 +00:00
|
|
|
|
2021-06-10 15:15:14 +00:00
|
|
|
if (castContext.castState == CastState.CONNECTED) {
|
|
|
|
clickCallback.invoke(EpisodeClickEvent(ACTION_CHROME_CAST_EPISODE, card))
|
|
|
|
} else {
|
2021-06-29 23:14:48 +00:00
|
|
|
// clickCallback.invoke(EpisodeClickEvent(ACTION_PLAY_EPISODE_IN_PLAYER, card))
|
2021-07-03 20:59:46 +00:00
|
|
|
clickCallback.invoke(EpisodeClickEvent(ACTION_DOWNLOAD_EPISODE, card))
|
2021-06-10 15:15:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-06-29 23:14:48 +00:00
|
|
|
// clickCallback.invoke(EpisodeClickEvent(ACTION_PLAY_EPISODE_IN_PLAYER, card))
|
2021-07-04 00:59:51 +00:00
|
|
|
clickCallback.invoke(EpisodeClickEvent(ACTION_DOWNLOAD_EPISODE, card)) //TODO REDO TO MAIN
|
2021-06-10 15:15:14 +00:00
|
|
|
}
|
2021-05-18 13:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|