diff options
author | Allan Wang <me@allanwang.ca> | 2019-10-20 01:51:18 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2019-10-20 01:51:18 -0700 |
commit | 302d3351e9866b71b1e525c0e5ba883fe022ba5c (patch) | |
tree | 3578ac0cb463cb1b9224f9168250279a8b989652 | |
parent | 8e73e46e9ebbec9ac452cd670538736ad0dacb03 (diff) | |
download | kau-302d3351e9866b71b1e525c0e5ba883fe022ba5c.tar.gz kau-302d3351e9866b71b1e525c0e5ba883fe022ba5c.tar.bz2 kau-302d3351e9866b71b1e525c0e5ba883fe022ba5c.zip |
Update binding definitions
-rw-r--r-- | fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt | 91 |
1 files changed, 41 insertions, 50 deletions
diff --git a/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt b/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt index d1ae65f..d40be3d 100644 --- a/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt +++ b/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt @@ -31,65 +31,48 @@ interface VhModel { fun vh(): GenericItem } +/** + * Layout container. Should be implemented in a [BindingItem] companion. + */ interface BindingLayout<Binding : ViewBinding> { val layoutRes: Int - fun createBinding(context: Context, parent: ViewGroup?): Binding - fun Binding.bindView(holder: ViewHolder, payloads: MutableList<Any>) - fun Binding.unbindView(holder: ViewHolder) - - class ViewHolder(itemView: View, internal val layoutRes: Int) : - RecyclerView.ViewHolder(itemView) { - - /** - * Retrieves a binding. - * - * It is assumed that the binding is set on view holder creation, - * and that its type matches the supplied generic. - */ - fun <T> getBinding(): T = getBinding(itemView) - } - - companion object { - fun setBinding(view: View, binding: Any) { - view.setTag(R.id.kau_view_binding_model, binding) - } - - @Suppress("UNCHECKED_CAST") - fun <T> getBinding(view: View): T = view.getTag(R.id.kau_view_binding_model) as T - } } abstract class BindingItem<Binding : ViewBinding>(open val data: Any?) : - AbstractItem<BindingLayout.ViewHolder>(), + AbstractItem<BindingItem.ViewHolder>(), BindingLayout<Binding> { override val type: Int get() = layoutRes + abstract fun createBinding(context: Context, parent: ViewGroup?): Binding + override fun createView(ctx: Context, parent: ViewGroup?): View { val binding = createBinding(ctx, parent) - BindingLayout.setBinding(binding.root, binding) + setBinding(binding.root, binding) return binding.root } - final override fun bindView(holder: BindingLayout.ViewHolder, payloads: MutableList<Any>) { + final override fun bindView(holder: ViewHolder, payloads: MutableList<Any>) { super.bindView(holder, payloads) val binding = holder.getBinding<Binding>() binding.bindView(holder, payloads) } - final override fun unbindView(holder: BindingLayout.ViewHolder) { + abstract fun Binding.bindView(holder: ViewHolder, payloads: MutableList<Any>) + + final override fun unbindView(holder: ViewHolder) { super.unbindView(holder) val binding = holder.getBinding<Binding>() binding.unbindView(holder) } - override fun Binding.unbindView(holder: BindingLayout.ViewHolder) {} + open fun Binding.unbindView(holder: ViewHolder) {} - final override fun getViewHolder(v: View): BindingLayout.ViewHolder = - BindingLayout.ViewHolder(v, layoutRes) + final override fun getViewHolder(v: View): ViewHolder = + ViewHolder(v, layoutRes) - override fun failedToRecycle(holder: BindingLayout.ViewHolder): Boolean { + override fun failedToRecycle(holder: ViewHolder): Boolean { KL.e { "Failed to recycle" } return super.failedToRecycle(holder) } @@ -101,36 +84,44 @@ abstract class BindingItem<Binding : ViewBinding>(open val data: Any?) : } override fun hashCode(): Int = data.hashCode() + + class ViewHolder(itemView: View, internal val layoutRes: Int) : + RecyclerView.ViewHolder(itemView) { + + /** + * Retrieves a binding. + * + * It is assumed that the binding is set prior to this call, + * and that its type matches the supplied generic. + */ + fun <T> getBinding(): T = getBinding(itemView) + } + + companion object { + fun setBinding(view: View, binding: Any) { + view.setTag(R.id.kau_view_binding_model, binding) + } + + @Suppress("UNCHECKED_CAST") + fun <T> getBinding(view: View): T = view.getTag(R.id.kau_view_binding_model) as T + } } abstract class BindingClickEventHook<Binding : ViewBinding, Item : BindingItem<Binding>>(val identifier: BindingLayout<Binding>) : ClickEventHook<Item>() { private fun RecyclerView.ViewHolder.binding(): Binding? { - val holder = this as? BindingLayout.ViewHolder ?: return null + val holder = this as? BindingItem.ViewHolder ?: return null if (holder.layoutRes != identifier.layoutRes) { return null } return getBinding() } - /** - * All bound views must set the view root, which will be used to find the binding. - * We avoid attaching the binding directly - */ - private fun View.setRoot(root: View) { - setTag(R.id.kau_view_binding_root, root) - } - - private fun View.findBinding(): Binding { - val root = getTag(R.id.kau_view_binding_root) as View - return BindingLayout.getBinding(root) - } - final override fun onBind(viewHolder: RecyclerView.ViewHolder): View? { val binding = viewHolder.binding() ?: return super.onBind(viewHolder) val view = binding.onBind(viewHolder) ?: return super.onBind(viewHolder) - view.setRoot(binding.root) + BindingItem.setBinding(view, binding) return view } @@ -139,7 +130,7 @@ abstract class BindingClickEventHook<Binding : ViewBinding, Item : BindingItem<B final override fun onBindMany(viewHolder: RecyclerView.ViewHolder): List<View>? { val binding = viewHolder.binding() ?: return super.onBindMany(viewHolder) val views = binding.onBindMany(viewHolder) ?: return super.onBindMany(viewHolder) - views.forEach { it.setRoot(binding.root) } + views.forEach { BindingItem.setBinding(it, binding) } return views } @@ -147,8 +138,8 @@ abstract class BindingClickEventHook<Binding : ViewBinding, Item : BindingItem<B super.onBindMany(viewHolder) final override fun onClick(v: View, position: Int, fastAdapter: FastAdapter<Item>, item: Item) { - v.findBinding().onClick(v, position, fastAdapter, item) + BindingItem.getBinding<Binding>(v).onClick(v, position, fastAdapter, item) } abstract fun Binding.onClick(v: View, position: Int, fastAdapter: FastAdapter<Item>, item: Item) -} +}
\ No newline at end of file |