aboutsummaryrefslogtreecommitdiff
path: root/fastadapter-databinding/src/main/kotlin/ca/allanwang
diff options
context:
space:
mode:
Diffstat (limited to 'fastadapter-databinding/src/main/kotlin/ca/allanwang')
-rw-r--r--fastadapter-databinding/src/main/kotlin/ca/allanwang/fastadapter/databinding/BindingItem.kt122
-rw-r--r--fastadapter-databinding/src/main/kotlin/ca/allanwang/fastadapter/databinding/FastBindingAdapter.kt45
2 files changed, 167 insertions, 0 deletions
diff --git a/fastadapter-databinding/src/main/kotlin/ca/allanwang/fastadapter/databinding/BindingItem.kt b/fastadapter-databinding/src/main/kotlin/ca/allanwang/fastadapter/databinding/BindingItem.kt
new file mode 100644
index 0000000..e52838b
--- /dev/null
+++ b/fastadapter-databinding/src/main/kotlin/ca/allanwang/fastadapter/databinding/BindingItem.kt
@@ -0,0 +1,122 @@
+package ca.allanwang.fastadapter.databinding
+
+
+import android.content.Context
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import android.widget.ImageView
+import android.widget.TextView
+import androidx.databinding.DataBindingUtil
+import androidx.databinding.ViewDataBinding
+import androidx.recyclerview.widget.RecyclerView
+import ca.allanwang.gitdroid.logger.L
+import ca.allanwang.gitdroid.views.BR
+import com.bumptech.glide.Glide
+import com.mikepenz.fastadapter.FastAdapter
+import com.mikepenz.fastadapter.items.AbstractItem
+import com.mikepenz.fastadapter.listeners.ClickEventHook
+
+typealias GenericBindingItem = BindingItem<*>
+
+abstract class BindingItem<Binding : ViewDataBinding>(open val data: Any?) : AbstractItem<BindingItem.ViewHolder>(),
+ BindingLayout<Binding> {
+
+ override val type: Int
+ get() = layoutRes
+
+ override fun createView(ctx: Context, parent: ViewGroup?): View {
+ val binding: ViewDataBinding = DataBindingUtil.inflate(
+ LayoutInflater.from(ctx),
+ layoutRes, parent,
+ false,
+ null
+ )
+ return binding.root
+ }
+
+ final override fun bindView(holder: ViewHolder, payloads: MutableList<Any>) {
+ super.bindView(holder, payloads)
+ val binding = DataBindingUtil.getBinding<Binding>(holder.itemView) ?: return
+ binding.bindView(holder, payloads)
+ binding.executePendingBindings()
+ }
+
+ open fun Binding.bindView(holder: ViewHolder, payloads: MutableList<Any>) {
+ setVariable(BR.model, data)
+ }
+
+ final override fun unbindView(holder: ViewHolder) {
+ super.unbindView(holder)
+ val binding = DataBindingUtil.getBinding<Binding>(holder.itemView) ?: return
+ binding.unbindView(holder)
+ binding.unbind()
+ }
+
+ open fun Binding.unbindView(holder: ViewHolder) {}
+
+ final override fun getViewHolder(v: View): ViewHolder = ViewHolder(v, layoutRes)
+
+ override fun failedToRecycle(holder: ViewHolder): Boolean {
+ L.e { "Failed to recycle" }
+ return super.failedToRecycle(holder)
+ }
+
+ companion object {
+ @JvmStatic
+ protected fun unbindGlide(vararg imageView: ImageView) {
+ if (imageView.isEmpty()) {
+ return
+ }
+ val manager = Glide.with(imageView.first().context)
+ imageView.forEach { manager.clear(it) }
+ }
+
+ @JvmStatic
+ protected fun unbind(vararg imageView: ImageView) {
+ imageView.forEach { it.setImageDrawable(null) }
+ }
+
+ @JvmStatic
+ protected fun unbind(vararg textView: TextView) {
+ textView.forEach { it.text = null }
+ }
+ }
+
+ class ViewHolder(itemView: View, internal val layoutRes: Int) : RecyclerView.ViewHolder(itemView)
+
+}
+
+interface BindingLayout<Binding : ViewDataBinding> {
+ val layoutRes: Int
+}
+
+
+abstract class BindingClickEventHook<Binding : ViewDataBinding, Item : BindingItem<Binding>>(val identifier: BindingLayout<Binding>) :
+ ClickEventHook<Item>() {
+
+ private fun RecyclerView.ViewHolder.binding(): Binding? {
+ val holder = this as? BindingItem.ViewHolder ?: return null
+ if (holder.layoutRes != identifier.layoutRes) {
+ return null
+ }
+ return DataBindingUtil.getBinding(itemView)
+ }
+
+ final override fun onBind(viewHolder: RecyclerView.ViewHolder): View? =
+ viewHolder.binding()?.onBind(viewHolder) ?: super.onBind(viewHolder)
+
+ open fun Binding.onBind(viewHolder: RecyclerView.ViewHolder): View? = super.onBind(viewHolder)
+
+ final override fun onBindMany(viewHolder: RecyclerView.ViewHolder): List<View>? =
+ viewHolder.binding()?.onBindMany(viewHolder) ?: super.onBindMany(viewHolder)
+
+ open fun Binding.onBindMany(viewHolder: RecyclerView.ViewHolder): List<View>? = super.onBindMany(viewHolder)
+
+ final override fun onClick(v: View, position: Int, fastAdapter: FastAdapter<Item>, item: Item) {
+ val binding: Binding = DataBindingUtil.findBinding(v) ?: return
+ binding.onClick(v, position, fastAdapter, item)
+ }
+
+ abstract fun Binding.onClick(v: View, position: Int, fastAdapter: FastAdapter<Item>, item: Item)
+} \ No newline at end of file
diff --git a/fastadapter-databinding/src/main/kotlin/ca/allanwang/fastadapter/databinding/FastBindingAdapter.kt b/fastadapter-databinding/src/main/kotlin/ca/allanwang/fastadapter/databinding/FastBindingAdapter.kt
new file mode 100644
index 0000000..2270b5d
--- /dev/null
+++ b/fastadapter-databinding/src/main/kotlin/ca/allanwang/fastadapter/databinding/FastBindingAdapter.kt
@@ -0,0 +1,45 @@
+package ca.allanwang.fastadapter.databinding
+
+import com.mikepenz.fastadapter.FastAdapter
+import com.mikepenz.fastadapter.IAdapter
+import com.mikepenz.fastadapter.IItemAdapter
+import com.mikepenz.fastadapter.adapters.ItemAdapter
+import com.mikepenz.fastadapter.diff.FastAdapterDiffUtil
+
+class FastBindingAdapter private constructor(private val adapter: ItemAdapter<GenericBindingItem>) :
+ FastAdapter<GenericBindingItem>(),
+ IItemAdapter<GenericBindingItem, GenericBindingItem> by adapter {
+
+ constructor() : this(ItemAdapter())
+
+ var lastClearTime: Long = -1
+
+ init {
+ super.addAdapter(0, adapter)
+ }
+
+ override fun clear(): FastBindingAdapter {
+ if (itemCount != 0) {
+ adapter.clear()
+ lastClearTime = System.currentTimeMillis()
+ }
+ return this
+ }
+
+ override fun <A : IAdapter<GenericBindingItem>> addAdapter(
+ index: Int,
+ adapter: A
+ ): FastAdapter<GenericBindingItem> {
+ throw IllegalArgumentException("FastBindingAdapter only allows one adapter")
+ }
+
+ fun setWithDiff(items: List<GenericBindingItem>, detectMoves: Boolean = true) {
+ FastAdapterDiffUtil.set(
+ adapter,
+ items,
+ null,
+ detectMoves
+ )
+ }
+
+} \ No newline at end of file