Skip to content

Instantly share code, notes, and snippets.

@ravisorathiya
Last active May 8, 2024 07:04
Show Gist options
  • Save ravisorathiya/9457edb4a8a3fd2e4ad83721601cce31 to your computer and use it in GitHub Desktop.
Save ravisorathiya/9457edb4a8a3fd2e4ad83721601cce31 to your computer and use it in GitHub Desktop.
BaseAdapter.kt
class EasyAdapter<T : Any> private constructor(
private val layoutResId: Int,
areContentsTheSame: (T, T) -> Boolean = { old, new -> old == new },
areItemsTheSame: (T, T) -> Boolean = { old, new -> old == new },
private val onBind: (View, T) -> Unit,
private val onViewCreated: (View, position: () -> Pair<Int, T>) -> Unit,
) : ListAdapter<T, RecyclerView.ViewHolder>(
GenericDiffCallback(
areContentsTheSame,
areItemsTheSame,
)
) {
companion object {
fun <T : Any> create(
@LayoutRes layoutResId: Int,
): Builder<T> {
return Builder(layoutResId)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(layoutResId, parent, false)
return ViewHolder(itemView)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val item = getItem(position)
onBind(holder.itemView, item)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
init {
try {
onViewCreated(itemView, ::item)
} catch (e: Exception) {
Timber.e(e.stackTraceToString())
}
}
fun item(): Pair<Int, T> {
val absoluteAdapterPosition = absoluteAdapterPosition
return Pair(absoluteAdapterPosition, getItem(absoluteAdapterPosition))
}
}
private class GenericDiffCallback<T : Any>(
private val areContentsSame: (T, T) -> Boolean,
private val areItemsSame: (T, T) -> Boolean,
) : DiffUtil.ItemCallback<T>() {
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean {
return areContentsSame(oldItem, newItem)
}
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean {
return areItemsSame(oldItem, newItem)
}
}
class Builder<T : Any>(
private val layoutResId: Int,
) {
private var areContentsTheSame: (T, T) -> Boolean = { old, new -> old == new }
private var areItemsTheSame: (T, T) -> Boolean = { old, new -> old == new }
private var onBind: (View, T) -> Unit = { _, _ -> }
private var onViewCreated: (View, position: () -> Pair<Int, T>) -> Unit = { _, _ -> }
fun setAreContentsTheSame(areContentsTheSame: (T, T) -> Boolean): Builder<T> {
this.areContentsTheSame = areContentsTheSame
return this
}
fun setAreItemsTheSame(areItemsTheSame: (T, T) -> Boolean): Builder<T> {
this.areItemsTheSame = areItemsTheSame
return this
}
fun setOnBind(onBind: (View, T) -> Unit): Builder<T> {
this.onBind = onBind
return this
}
fun setOnViewCreated(onViewCreated: (View, position: () -> Pair<Int, T>) -> Unit): Builder<T> {
this.onViewCreated = onViewCreated
return this
}
fun build(): EasyAdapter<T> {
return EasyAdapter(
layoutResId = layoutResId,
areContentsTheSame = areContentsTheSame,
areItemsTheSame = areItemsTheSame,
onBind = onBind,
onViewCreated = onViewCreated,
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment