Skip to content

Instantly share code, notes, and snippets.

@DivS-15
Created May 22, 2022 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DivS-15/299af9cb43661eac62ec006901a27d3e to your computer and use it in GitHub Desktop.
Save DivS-15/299af9cb43661eac62ec006901a27d3e to your computer and use it in GitHub Desktop.
class VideoAdapter :
PagingDataAdapter<VideoWithChannelModel, VideoAdapter.VideoViewHolder>(DiffCallback) {
companion object DiffCallback : DiffUtil.ItemCallback<VideoWithChannelModel>() {
override fun areItemsTheSame(
oldItem: VideoWithChannelModel,
newItem: VideoWithChannelModel
): Boolean {
return oldItem.item.id == newItem.item.id
}
override fun areContentsTheSame(
oldItem: VideoWithChannelModel,
newItem: VideoWithChannelModel
): Boolean {
return oldItem == newItem
}
}
inner class VideoViewHolder(
private val binding: VideoListItemBinding
) : RecyclerView.ViewHolder(binding.root) {
@SuppressLint("SetTextI18n")
fun bind(video: VideoWithChannelModel) {
binding.apply {
val imgUrl = video.channelUrl.toUri().buildUpon().scheme("https").build()
channelThumbnailImage.load(imgUrl) {
transformations(CircleCropTransformation())
}
video.item.apply {
videoTitle.text = this.snippet.title
channelTitle.text = snippet.channelTitle
viewCount.text = statistics.viewCount
val imgUrl = snippet.thumbnails?.high?.url
val dateTime = getDateTimeDifference(snippet.publishedAt.toString())
if (dateTime.days.toInt() != 0) {
timePublished.text = "${dateTime.days.toInt()} days ago"
} else if (dateTime.days.toInt() == 0) {
timePublished.text = "${dateTime.hours.toInt()} hours ago"
}
if (dateTime.hours.toInt() == 0) {
timePublished.text = "${dateTime.minutes.toInt()} minutes ago"
}
val imgUri = imgUrl?.toUri()?.buildUpon()?.scheme("https")?.build()
videoThumbnailImage.load(imgUri)
}
}
}
}
override fun onBindViewHolder(holder: VideoViewHolder, position: Int) {
val currentItem = getItem(position)
currentItem?.let {
holder.bind(it)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoViewHolder {
val binding =
VideoListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return VideoViewHolder(binding)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment