teapod/app/src/main/java/org/mosad/teapod/util/adapter/EpisodeItemAdapter.kt

97 lines
3.8 KiB
Kotlin
Raw Normal View History

package org.mosad.teapod.util.adapter
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import org.mosad.teapod.R
2020-11-25 22:35:55 +01:00
import org.mosad.teapod.databinding.ItemEpisodeBinding
import org.mosad.teapod.parser.crunchyroll.Episode
import org.mosad.teapod.parser.crunchyroll.PlayheadsMap
2021-07-25 19:15:31 +02:00
import org.mosad.teapod.util.tmdb.TMDBTVEpisode
class EpisodeItemAdapter(
private val episodes: List<Episode>,
private val tmdbEpisodes: List<TMDBTVEpisode>?,
private val playheads: PlayheadsMap
) : RecyclerView.Adapter<EpisodeItemAdapter.EpisodeViewHolder>() {
2021-12-20 22:14:58 +01:00
var onImageClick: ((seasonId: String, episodeId: String) -> Unit)? = null
2020-11-21 19:40:55 +01:00
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EpisodeViewHolder {
2020-11-25 22:35:55 +01:00
return EpisodeViewHolder(ItemEpisodeBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
2020-11-21 19:40:55 +01:00
override fun onBindViewHolder(holder: EpisodeViewHolder, position: Int) {
2020-11-25 22:35:55 +01:00
val context = holder.binding.root.context
val ep = episodes[position]
2021-12-29 20:51:53 +01:00
val titleText = if (ep.episodeNumber != null) {
// for tv shows add ep prefix and episode number
if (ep.isDubbed) {
context.getString(R.string.component_episode_title, ep.episode, ep.title)
} else {
context.getString(R.string.component_episode_title_sub, ep.episode, ep.title)
}
} else {
2021-12-29 20:51:53 +01:00
ep.title
}
2020-11-25 22:35:55 +01:00
holder.binding.textEpisodeTitle.text = titleText
2021-12-20 22:14:58 +01:00
holder.binding.textEpisodeDesc.text = if (ep.description.isNotEmpty()) {
ep.description
} else if (tmdbEpisodes != null && position < tmdbEpisodes.size){
tmdbEpisodes[position].overview
} else {
""
}
// TODO is isNotEmpty() needed? also in PlayerEpisodeItemAdapter
2021-12-20 22:14:58 +01:00
if (ep.images.thumbnail[0][0].source.isNotEmpty()) {
Glide.with(context).load(ep.images.thumbnail[0][0].source)
.apply(RequestOptions.placeholderOf(ColorDrawable(Color.DKGRAY)))
.apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(10, 0)))
2020-11-25 22:35:55 +01:00
.into(holder.binding.imageEpisode)
}
// add watched progress
val playheadProgress = playheads[ep.id]?.let { playhead ->
((playhead.playhead.toFloat() / (ep.durationMs / 1000)) * 100).toInt()
} ?: 0
holder.binding.progressPlayhead.setProgressCompat(playheadProgress, false)
holder.binding.progressPlayhead.visibility = if (playheadProgress <= 0)
View.GONE else View.VISIBLE
// add watched icon to episode, if the episode id is present in playheads and fullyWatched
val watchedImage: Drawable? = if (playheads[ep.id]?.fullyWatched == true) {
ContextCompat.getDrawable(context, R.drawable.ic_baseline_check_circle_24)
} else {
null
}
holder.binding.imageWatched.setImageDrawable(watchedImage)
}
override fun getItemCount(): Int {
return episodes.size
}
2021-12-20 22:14:58 +01:00
inner class EpisodeViewHolder(val binding: ItemEpisodeBinding) :
RecyclerView.ViewHolder(binding.root) {
init {
2021-12-20 22:14:58 +01:00
// on image click return the episode id and index (within the adapter)
2020-11-25 22:35:55 +01:00
binding.imageEpisode.setOnClickListener {
2021-12-20 22:14:58 +01:00
onImageClick?.invoke(
episodes[bindingAdapterPosition].seasonId,
episodes[bindingAdapterPosition].id
2021-12-20 22:14:58 +01:00
)
2020-10-16 10:05:11 +02:00
}
}
}
}