package org.mosad.teapod.util.adapter import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide import org.mosad.teapod.R import org.mosad.teapod.databinding.ItemMediaBinding import org.mosad.teapod.parser.crunchyroll.ContinueWatchingItem class MediaEpisodeListAdapter(private val onClickListener: OnClickListener) : ListAdapter(DiffCallback) { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MediaViewHolder { return MediaViewHolder( ItemMediaBinding.inflate( LayoutInflater.from(parent.context), parent, false ) ) } override fun onBindViewHolder(holder: MediaViewHolder, position: Int) { val item = getItem(position) holder.binding.root.setOnClickListener { onClickListener.onClick(item) } holder.bind(item) } inner class MediaViewHolder(val binding: ItemMediaBinding) : RecyclerView.ViewHolder(binding.root) { fun bind(item: ContinueWatchingItem) { val metadata = item.panel.episodeMetadata binding.textTitle.text = binding.root.context.getString(R.string.season_episode_title, metadata.seasonNumber, metadata.episodeNumber, metadata.seriesTitle ) Glide.with(binding.imagePoster) .load(item.panel.images.thumbnail[0][0].source) .into(binding.imagePoster) // add watched progress val playheadProgress = ((item.playhead.toFloat() / (metadata.durationMs / 1000)) * 100) .toInt() binding.progressPlayhead.setProgressCompat(playheadProgress, false) binding.progressPlayhead.visibility = if (playheadProgress <= 0) View.GONE else View.VISIBLE } } companion object DiffCallback : DiffUtil.ItemCallback() { override fun areItemsTheSame(oldItem: ContinueWatchingItem, newItem: ContinueWatchingItem): Boolean { return oldItem.panel.id == newItem.panel.id } override fun areContentsTheSame(oldItem: ContinueWatchingItem, newItem: ContinueWatchingItem): Boolean { return oldItem == newItem } } class OnClickListener(val clickListener: (item: ContinueWatchingItem) -> Unit) { fun onClick(item: ContinueWatchingItem) = clickListener(item) } }