Skip to content

Instantly share code, notes, and snippets.

@vicky7230
Last active July 5, 2018 06:24
Show Gist options
  • Save vicky7230/d5a8c45bca82a790c36837211bc0583c to your computer and use it in GitHub Desktop.
Save vicky7230/d5a8c45bca82a790c36837211bc0583c to your computer and use it in GitHub Desktop.
class TvAdapter(private val resultList: MutableList<Result>?) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val TYPE_LOADING = -1
private val TYPE_TV = 1
interface Callback {
fun onTvShowClick(id: Int)
}
private var callback: Callback? = null
fun setCallback(callback: Callback) {
this.callback = callback
}
fun addItems(resultList: MutableList<Result>?) {
val newResultList = ArrayList<Result>()
newResultList.addAll(this.resultList!!)
newResultList.addAll(resultList!!)
val diffResult =
DiffUtil.calculateDiff(
ResultDiffUtilCallback(
this.resultList,
newResultList
)
)
this.resultList.addAll(resultList)
diffResult.dispatchUpdatesTo(this)
}
fun clearItems() {
resultList?.clear()
notifyDataSetChanged()
}
fun addItem(recipe: Result?) {
resultList?.add(recipe!!)
notifyItemInserted(resultList!!.size - 1)
}
fun removeItem() {
resultList?.removeAt(resultList.size - 1)
notifyItemRemoved(resultList!!.size)
}
override fun getItemViewType(position: Int): Int {
return if (resultList?.get(position)?.type == "LOADING") TYPE_LOADING else TYPE_TV
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
TYPE_LOADING -> createLoadingViewHolder(parent)
else -> createTvViewHolder(parent)
}
}
private fun createTvViewHolder(parent: ViewGroup?): RecyclerView.ViewHolder {
val tvViewHolder = TvViewHolder(
LayoutInflater.from(parent?.context).inflate(
R.layout.tv_list_item,
parent,
false
)
)
tvViewHolder.itemView.tv_image_card.setOnClickListener({
val result = getItem(tvViewHolder.adapterPosition)
if (result != null) {
callback?.onTvShowClick(result.id ?: 0)
}
})
return tvViewHolder
}
private fun createLoadingViewHolder(parent: ViewGroup?): RecyclerView.ViewHolder {
return LoadingViewHolder(
LayoutInflater.from(parent?.context).inflate(
R.layout.tv_list_footer,
parent,
false
)
)
}
fun getItem(position: Int): Result? {
return if (position != RecyclerView.NO_POSITION)
resultList?.get(position)
else
null
}
override fun getItemCount(): Int {
return resultList?.size ?: 0
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (getItemViewType(position)) {
TYPE_TV -> (holder as TvViewHolder).onBind(resultList?.get(position))
TYPE_LOADING -> (holder as LoadingViewHolder).onBind()
}
}
class TvViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun onBind(result: Result?) {
GlideApp
.with(itemView.context)
.load("https://image.tmdb.org/t/p/w500" + result?.posterPath)
.transition(withCrossFade())
.fitCenter()
.into(itemView.tv_image)
itemView.tv_title.text = result?.originalName
itemView.rating_circle.text = result?.voteAverage.toString()
}
}
class LoadingViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun onBind() {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment