-
-
Save programmerr47/6377eb644927165475334980190a45d7 to your computer and use it in GitHub Desktop.
Number of base generic delegates to construct custom delegates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias ExtensionItemDelegate<T> = ItemDelegate<T, ExtensionViewHolder<T>> | |
class LayoutItemDelegate<I>( | |
private val type: Class<out I>, | |
@LayoutRes private val layoutId: Int | |
) : ExtensionItemDelegate<I> { | |
override fun itemType() = type | |
override fun createVewHolder(parent: ViewGroup) = ExtensionViewHolder<I>(parent.inflate(layoutId)) | |
override fun bindView(position: Int, item: I, holder: ExtensionViewHolder<I>) { | |
holder.holdItem = item | |
} | |
override fun bindView(position: Int, item: I, holder: ExtensionViewHolder<I>, payloads: List<Any>) { | |
holder.holdItem = item | |
} | |
} | |
class CreateItemDelegate<I>( | |
private val origin: ExtensionItemDelegate<I>, | |
private val createBlock: ExtensionViewHolder<I>.() -> Unit | |
) : ExtensionItemDelegate<I> by origin { | |
override fun createVewHolder(parent: ViewGroup) = origin.createVewHolder(parent).apply(createBlock) | |
} | |
class BindItemDelegate<I>( | |
private val origin: ExtensionItemDelegate<I>, | |
private val bindBlock: ExtensionViewHolder<I>.(I) -> Unit | |
) : ExtensionItemDelegate<I> by origin { | |
override fun bindView(position: Int, item: I, holder: ExtensionViewHolder<I>) { | |
origin.bindView(position, item, holder) | |
holder.holdItem?.let { holder.bindBlock(it) } | |
} | |
} | |
class BindPayloadsItemDelegate<I>( | |
private val origin: ExtensionItemDelegate<I>, | |
private val bindBlock: ExtensionViewHolder<I>.(I, List<Any>) -> Unit | |
) : ExtensionItemDelegate<I> by origin { | |
override fun bindView(position: Int, item: I, holder: ExtensionViewHolder<I>, payloads: List<Any>) { | |
origin.bindView(position, item, holder, payloads) | |
holder.holdItem?.let { holder.bindBlock(it, payloads) } | |
} | |
} | |
class ClickableItemDelegate<I>( | |
private val origin: ExtensionItemDelegate<I>, | |
private val onClick: (I) -> Unit | |
) : ExtensionItemDelegate<I> by origin { | |
override fun createVewHolder(parent: ViewGroup) = | |
origin.createVewHolder(parent).apply { | |
itemView bindClick onClick | |
} | |
} | |
class ClickableWithPosItemDelegate<I>( | |
private val origin: ExtensionItemDelegate<I>, | |
private val clickListener: (I, Int) -> Unit | |
) : ExtensionItemDelegate<I> by origin { | |
override fun createVewHolder(parent: ViewGroup) = origin.createVewHolder(parent).apply { | |
itemView.setSafeOnClickListener { holdItem?.let { clickListener(it, adapterPosition) } } | |
} | |
} | |
inline fun <reified T> itemDelegate(@LayoutRes layoutId: Int) = LayoutItemDelegate(T::class.java, layoutId) | |
fun <T> ExtensionItemDelegate<T>.create(block: ExtensionViewHolder<T>.() -> Unit) = CreateItemDelegate(this, block) | |
fun <T> ExtensionItemDelegate<T>.bind(block: ExtensionViewHolder<T>.(item: T) -> Unit) = BindItemDelegate(this, block) | |
fun <T> ExtensionItemDelegate<T>.bindPayloads(block: ExtensionViewHolder<T>.(item: T, payloads: List<Any>) -> Unit) = BindPayloadsItemDelegate(this, block) | |
fun <T> ExtensionItemDelegate<T>.click(clickListener: (T) -> Unit) = ClickableItemDelegate(this, clickListener) | |
fun <T> ExtensionItemDelegate<T>.clickWithPos(clickListener: (T, Int) -> Unit) = ClickableWithPosItemDelegate(this, clickListener) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment