@@ -13,48 +13,23 @@ import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
1313import com.bumptech.glide.request.RequestOptions
1414
1515class FavoriteShowsAdapter (
16- private val favoriteShows : MutableList <FavoriteShow >,
17- private val callback : Callback
16+ private val favoriteShows : MutableList <FavoriteShow >
1817) : RecyclerView.Adapter<FavoriteShowsAdapter.FavoriteShowHolder>() {
1918
2019 override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): FavoriteShowHolder {
2120 val layoutInflater = LayoutInflater .from(parent.context)
2221 val showListItemBinding = ShowListItemBinding .inflate(layoutInflater, parent, false )
2322 val holder = FavoriteShowHolder (showListItemBinding)
24- holder.binding.favorite.setOnClickListener { onFavouriteIconClicked(holder.absoluteAdapterPosition) }
2523 holder.binding.showFavoriteIcon = false
2624 return holder
2725 }
2826
29- private fun onFavouriteIconClicked (position : Int ) {
30- if (position != RecyclerView .NO_POSITION ) {
31- val show = favoriteShows[position]
32- val updatedShow = show.copy(isFavorite = ! show.isFavorite)
33- favoriteShows[position] = updatedShow
34- notifyItemChanged(position)
35- callback.onFavoriteClicked(show)
36- }
37- }
38-
3927 override fun onBindViewHolder (holder : FavoriteShowHolder , position : Int ) {
4028 val favoriteShow = favoriteShows[position]
4129 Glide .with (holder.itemView.context).load(favoriteShow.imageUrl)
4230 .apply (RequestOptions .placeholderOf(R .color.grey))
4331 .transition(DrawableTransitionOptions .withCrossFade())
4432 .into(holder.binding.showImage)
45- configureFavoriteIcon(holder.binding.favorite, favoriteShow.isFavorite)
46- }
47-
48- private fun configureFavoriteIcon (favoriteIcon : ImageView , favorite : Boolean ) {
49- if (favorite) {
50- val favoriteDrawable = AppCompatResources
51- .getDrawable(favoriteIcon.context, R .drawable.favorite)
52- favoriteIcon.setImageDrawable(favoriteDrawable)
53- } else {
54- val unFavoriteDrawable = AppCompatResources
55- .getDrawable(favoriteIcon.context, R .drawable.favorite_border)
56- favoriteIcon.setImageDrawable(unFavoriteDrawable)
57- }
5833 }
5934
6035 override fun getItemCount (): Int {
@@ -63,8 +38,4 @@ class FavoriteShowsAdapter(
6338
6439 class FavoriteShowHolder (val binding : ShowListItemBinding ) :
6540 RecyclerView .ViewHolder (binding.root)
66-
67- interface Callback {
68- fun onFavoriteClicked (show : FavoriteShow )
69- }
7041}
0 commit comments