2020-10-14 20:22:20 +02:00
package org.mosad.teapod.util.adapter
2020-10-12 09:54:32 +02:00
2021-01-11 22:12:21 +01:00
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
2020-10-12 09:54:32 +02:00
import android.view.LayoutInflater
import android.view.ViewGroup
2020-10-16 18:24:34 +02:00
import androidx.core.content.ContextCompat
2020-10-12 09:54:32 +02:00
import androidx.recyclerview.widget.RecyclerView
2020-10-13 15:56:07 +02:00
import com.bumptech.glide.Glide
2020-10-14 18:33:02 +02:00
import com.bumptech.glide.request.RequestOptions
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
2020-10-12 09:54:32 +02:00
import org.mosad.teapod.R
2020-11-25 22:35:55 +01:00
import org.mosad.teapod.databinding.ItemEpisodeBinding
2021-08-31 19:47:18 +02:00
import org.mosad.teapod.util.AoDEpisode
2021-07-25 19:15:31 +02:00
import org.mosad.teapod.util.tmdb.TMDBTVEpisode
2020-10-12 09:54:32 +02:00
2021-08-31 19:47:18 +02:00
class EpisodeItemAdapter ( private val episodes : List < AoDEpisode > , private val tmdbEpisodes : List < TMDBTVEpisode > ? ) : RecyclerView . Adapter < EpisodeItemAdapter . EpisodeViewHolder > ( ) {
2020-10-12 09:54:32 +02:00
2020-10-16 10:05:11 +02:00
var onImageClick : ( ( String , Int ) -> Unit ) ? = null
2020-10-12 09:54:32 +02:00
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-10-12 09:54:32 +02:00
}
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
2020-10-19 21:57:02 +02:00
val ep = episodes [ position ]
2020-10-16 18:24:34 +02:00
2020-12-26 14:39:35 +01:00
val titleText = if ( ep . hasDub ( ) ) {
2020-10-19 21:57:02 +02:00
context . getString ( R . string . component _episode _title , ep . number , ep . description )
2020-12-26 14:39:35 +01:00
} else {
context . getString ( R . string . component _episode _title _sub , ep . number , ep . description )
2020-10-19 21:57:02 +02:00
}
2020-11-25 22:35:55 +01:00
holder . binding . textEpisodeTitle . text = titleText
2021-07-11 12:56:21 +02:00
holder . binding . textEpisodeDesc . text = if ( ep . shortDesc . isNotEmpty ( ) ) {
ep . shortDesc
} else if ( tmdbEpisodes != null && position < tmdbEpisodes . size ) {
tmdbEpisodes [ position ] . overview
} else {
" "
}
2020-10-13 20:23:55 +02:00
2021-08-31 19:47:18 +02:00
if ( ep . imageURL . isNotEmpty ( ) ) {
Glide . with ( context ) . load ( ep . imageURL )
2021-01-11 22:12:21 +01:00
. apply ( RequestOptions . placeholderOf ( ColorDrawable ( Color . DKGRAY ) ) )
2020-10-14 18:33:02 +02:00
. apply ( RequestOptions . bitmapTransform ( RoundedCornersTransformation ( 10 , 0 ) ) )
2020-11-25 22:35:55 +01:00
. into ( holder . binding . imageEpisode )
2020-10-13 20:23:55 +02:00
}
2020-10-13 15:56:07 +02:00
2020-10-19 21:57:02 +02:00
if ( ep . watched ) {
2020-11-25 22:35:55 +01:00
holder . binding . imageWatched . setImageDrawable (
2020-10-16 18:24:34 +02:00
ContextCompat . getDrawable ( context , R . drawable . ic _baseline _check _circle _24 )
)
} else {
2020-11-25 22:35:55 +01:00
holder . binding . imageWatched . setImageDrawable ( null )
2020-10-13 15:56:07 +02:00
}
2020-10-12 09:54:32 +02:00
}
override fun getItemCount ( ) : Int {
2020-10-13 20:23:55 +02:00
return episodes . size
2020-10-12 09:54:32 +02:00
}
2020-10-13 23:47:48 +02:00
fun updateWatchedState ( watched : Boolean , position : Int ) {
2021-02-13 00:31:58 +01:00
// use getOrNull as there could be a index out of bound when running this in onResume()
episodes . getOrNull ( position ) ?. watched = watched
2020-10-13 23:47:48 +02:00
}
2020-11-25 22:35:55 +01:00
inner class EpisodeViewHolder ( val binding : ItemEpisodeBinding ) : RecyclerView . ViewHolder ( binding . root ) {
2020-10-12 09:54:32 +02:00
init {
2020-11-25 22:35:55 +01:00
binding . imageEpisode . setOnClickListener {
2020-10-16 10:05:11 +02:00
onImageClick ?. invoke ( episodes [ adapterPosition ] . title , adapterPosition )
}
2020-10-12 09:54:32 +02:00
}
}
}