2022-04-10 20:15:13 +02:00
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
2023-02-19 14:21:46 +01:00
import org.mosad.teapod.parser.crunchyroll.UpNextAccountItem
2022-04-10 20:15:13 +02:00
2023-02-19 14:21:46 +01:00
class MediaEpisodeListAdapter ( private val onClickListener : OnClickListener , private val itemOffset : Int = 0 ) : ListAdapter < UpNextAccountItem , MediaEpisodeListAdapter . MediaViewHolder > ( DiffCallback ) {
2022-04-10 20:15:13 +02:00
override fun onCreateViewHolder ( parent : ViewGroup , viewType : Int ) : MediaViewHolder {
2022-12-04 13:51:29 +01:00
val binding = ItemMediaBinding . inflate (
LayoutInflater . from ( parent . context ) ,
parent ,
false
2022-04-10 20:15:13 +02:00
)
2022-12-04 13:51:29 +01:00
binding . root . layoutParams . apply {
2022-12-04 14:48:25 +01:00
width = ( parent . measuredWidth / parent . context . resources . getInteger ( R . integer . item _media _columns ) ) - itemOffset
2022-12-04 13:51:29 +01:00
}
return MediaViewHolder ( binding )
2022-04-10 20:15:13 +02:00
}
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 ) {
2023-02-19 14:21:46 +01:00
fun bind ( item : UpNextAccountItem ) {
2022-04-10 20:15:13 +02:00
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
}
}
2023-02-19 14:21:46 +01:00
companion object DiffCallback : DiffUtil . ItemCallback < UpNextAccountItem > ( ) {
override fun areItemsTheSame ( oldItem : UpNextAccountItem , newItem : UpNextAccountItem ) : Boolean {
2022-04-10 20:15:13 +02:00
return oldItem . panel . id == newItem . panel . id
}
2023-02-19 14:21:46 +01:00
override fun areContentsTheSame ( oldItem : UpNextAccountItem , newItem : UpNextAccountItem ) : Boolean {
2022-04-10 20:15:13 +02:00
return oldItem == newItem
}
}
2023-02-19 14:21:46 +01:00
class OnClickListener ( val clickListener : ( item : UpNextAccountItem ) -> Unit ) {
fun onClick ( item : UpNextAccountItem ) = clickListener ( item )
2022-04-10 20:15:13 +02:00
}
}