diff --git a/app/src/main/java/com/android/newsapp/shared/NewsArticleComparator.kt b/app/src/main/java/com/android/newsapp/shared/NewsArticleComparator.kt new file mode 100644 index 0000000..41b8bb2 --- /dev/null +++ b/app/src/main/java/com/android/newsapp/shared/NewsArticleComparator.kt @@ -0,0 +1,12 @@ +package com.android.newsapp.shared + +import androidx.recyclerview.widget.DiffUtil +import com.android.newsapp.data.NewsArticle + +class NewsArticleComparator : DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: NewsArticle, newItem: NewsArticle) = + oldItem.url == newItem.url + + override fun areContentsTheSame(oldItem: NewsArticle, newItem: NewsArticle) = + oldItem == newItem +} \ No newline at end of file diff --git a/app/src/main/java/com/android/newsapp/shared/NewsArticleListAdapter.kt b/app/src/main/java/com/android/newsapp/shared/NewsArticleListAdapter.kt new file mode 100644 index 0000000..1f5db10 --- /dev/null +++ b/app/src/main/java/com/android/newsapp/shared/NewsArticleListAdapter.kt @@ -0,0 +1,23 @@ +package com.android.newsapp.shared + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.ListAdapter +import com.android.newsapp.data.NewsArticle +import com.android.newsapp.databinding.ItemNewsArticleBinding + +class NewsArticleListAdapter : + ListAdapter(NewsArticleComparator()) { + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsArticleViewHolder { + val binding = + ItemNewsArticleBinding.inflate(LayoutInflater.from(parent.context), parent, false) + return NewsArticleViewHolder(binding) + } + + override fun onBindViewHolder(holder: NewsArticleViewHolder, position: Int) { + val currentItem = getItem(position) + if (currentItem != null) { + holder.bind(currentItem) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/android/newsapp/shared/NewsArticleViewHolder.kt b/app/src/main/java/com/android/newsapp/shared/NewsArticleViewHolder.kt new file mode 100644 index 0000000..55fd003 --- /dev/null +++ b/app/src/main/java/com/android/newsapp/shared/NewsArticleViewHolder.kt @@ -0,0 +1,32 @@ +package com.android.newsapp.shared + +import androidx.recyclerview.widget.RecyclerView +import com.android.newsapp.R +import com.android.newsapp.data.NewsArticle +import com.android.newsapp.databinding.ItemNewsArticleBinding +import com.bumptech.glide.Glide + +class NewsArticleViewHolder( + private val binding: ItemNewsArticleBinding +) : RecyclerView.ViewHolder(binding.root) { + + fun bind(article: NewsArticle) { + binding.apply { + Glide.with(itemView) + .load(article.thumbnailUrl) + .error(R.drawable.image_placeholder) + .into(imageView) + + textViewTitle.text = article.title ?: "" + + imageViewBookmark.setImageResource( + when { + article.isBookmarked -> R.drawable.ic_bookmark_selected + else -> R.drawable.ic_bookmark_unselected + } + ) + } + } + + +} \ No newline at end of file