Skip to content

Commit

Permalink
Included bind based on position
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaishnav Anil committed Oct 31, 2023
1 parent e10c7ff commit 7d4400d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
8 changes: 4 additions & 4 deletions adapt/src/main/java/io/github/vshnv/adapt/AdaptAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class AdaptAdapter<T : Any>(private val viewTypeMapper: ((T, Int) -> Int)?, priv
val binderItem: CollectingBindable<T, *> = viewBinders[viewType] ?: defaultBinder
?: throw AssertionError("Adapt found ViewType with no bound view creator or any default view creator, Cannot proceed!")
val viewSource = binderItem.creator()
return AdaptViewHolder<T>(viewSource.view) { data ->
return AdaptViewHolder<T>(viewSource.view) { position, data ->
binderItem.bindView?.let { bind ->
bind(data, viewSource)
bind(position, data, viewSource)
}
}
}
Expand All @@ -44,7 +44,7 @@ class AdaptAdapter<T : Any>(private val viewTypeMapper: ((T, Int) -> Int)?, priv

override fun onBindViewHolder(holder: AdaptViewHolder<T>, position: Int) {
val data = getItem(position)
holder.bind(data)
holder.bind(position, data)
}

private fun getItem(position: Int): T {
Expand All @@ -62,6 +62,6 @@ class AdaptAdapter<T : Any>(private val viewTypeMapper: ((T, Int) -> Int)?, priv
mDiffer.submitList(data, callback)
}

class AdaptViewHolder<T>(view: View, val bind: (T) -> Unit): RecyclerView.ViewHolder(view)
class AdaptViewHolder<T>(view: View, val bind: (Int, T) -> Unit): RecyclerView.ViewHolder(view)

}
1 change: 1 addition & 0 deletions adapt/src/main/java/io/github/vshnv/adapt/Bindable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package io.github.vshnv.adapt

interface Bindable<T, V> {
fun bind(bindView: (T, V) -> Unit)
fun bind(bindView: (Int, T, V) -> Unit)
}
10 changes: 8 additions & 2 deletions adapt/src/main/java/io/github/vshnv/adapt/CollectingBindable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import android.view.View
import java.lang.RuntimeException

class CollectingBindable<T, V>(val creator: () -> ViewSource<V>): Bindable<T, V> {
var bindView: ((T, Any) -> Unit)? = null
var bindView: ((Int, T, Any) -> Unit)? = null
private set

override fun bind(bindView: (T, V) -> Unit) {
this.bindView = { a, b -> bindView(a, resolveSourceParam(b)) }
this.bindView = { _, a, b -> bindView(a, resolveSourceParam(b)) }
}

override fun bind(bindView: (Int, T, V) -> Unit) {
this.bindView = { i, a, b -> bindView(i, a, resolveSourceParam(b)) }
}

private fun resolveSourceParam(item: Any): V {
Expand All @@ -20,4 +24,6 @@ class CollectingBindable<T, V>(val creator: () -> ViewSource<V>): Bindable<T, V>
}
}
}


}

0 comments on commit 7d4400d

Please sign in to comment.