Skip to content

Instantly share code, notes, and snippets.

@nitrico
Created July 3, 2016 16:23
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 nitrico/90e15f235782e1d8b82b2567116a3a5f to your computer and use it in GitHub Desktop.
Save nitrico/90e15f235782e1d8b82b2567116a3a5f to your computer and use it in GitHub Desktop.
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