-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9db7ab0
commit 35a7a3b
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/android/newsapp/shared/NewsArticleComparator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.android.newsapp.shared | ||
|
||
import androidx.recyclerview.widget.DiffUtil | ||
import com.android.newsapp.data.NewsArticle | ||
|
||
class NewsArticleComparator : DiffUtil.ItemCallback<NewsArticle>() { | ||
override fun areItemsTheSame(oldItem: NewsArticle, newItem: NewsArticle) = | ||
oldItem.url == newItem.url | ||
|
||
override fun areContentsTheSame(oldItem: NewsArticle, newItem: NewsArticle) = | ||
oldItem == newItem | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/android/newsapp/shared/NewsArticleListAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<NewsArticle, NewsArticleViewHolder>(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) | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/android/newsapp/shared/NewsArticleViewHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
) | ||
} | ||
} | ||
|
||
|
||
} |