Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LastAdapter.Builder
class Builder<T : Any> internal constructor(private val list: List<T>, private val variable: Int) {
private val map: MutableMap<Class<*>, Int> = mutableMapOf()
private var handler: LayoutHandler? = null
private var onBind: OnBindListener? = null
private var onClick: OnClickListener? = null
private var onLongClick: OnLongClickListener? = null
inline fun <reified T : Any> map(@LayoutRes layout: Int) = map(T::class.java, layout)
fun map(clazz: Class<*>, @LayoutRes layout: Int) = apply { map.put(clazz, layout) }
fun layoutHandler(layoutHandler: LayoutHandler) = apply { handler = layoutHandler }
fun layout(f: (Any, Int) -> Int) = apply {
handler = object : LayoutHandler {
override fun getItemLayout(item: Any, position: Int) = f(item, position)
}
}
fun onBindListener(listener: OnBindListener) = apply { onBind = listener }
fun onBind(f: (Any, View, Int) -> Unit) = apply {
onBind = object : OnBindListener {
override fun onBind(item: Any, view: View, position: Int) = f(item, view, position)
}
}
fun onClickListener(listener: OnClickListener) = apply { onClick = listener }
fun onClick(f: (Any, View, Int) -> Unit) = apply {
onClick = object : OnClickListener {
override fun onClick(item: Any, view: View, position: Int) = f(item, view, position)
}
}
fun onLongClickListener(listener: OnLongClickListener) = apply { onLongClick = listener }
fun onLongClick(f: (Any, View, Int) -> Unit) = apply {
onLongClick = object : OnLongClickListener {
override fun onLongClick(item: Any, view: View, position: Int) = f(item, view, position)
}
}
fun into(recyclerView: RecyclerView) = build().apply { recyclerView.adapter = this }
fun build() = LastAdapter(list, variable, map, handler, onBind, onClick, onLongClick)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment