Skip to content

Commit

Permalink
Added parent in argument for create
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaishnav Anil committed Oct 31, 2023
1 parent 7d4400d commit 6ffe53c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion adapt/src/main/java/io/github/vshnv/adapt/AdaptAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AdaptAdapter<T : Any>(private val viewTypeMapper: ((T, Int) -> Int)?, priv
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AdaptViewHolder<T> {
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()
val viewSource = binderItem.creator(parent)
return AdaptViewHolder<T>(viewSource.view) { position, data ->
binderItem.bindView?.let { bind ->
bind(position, data, viewSource)
Expand Down
6 changes: 4 additions & 2 deletions adapt/src/main/java/io/github/vshnv/adapt/AdaptScope.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.github.vshnv.adapt

import android.view.ViewGroup

interface AdaptScope<T: Any> {
fun itemEquals(checkEquality: (data: T, otherData: T) -> Boolean)
fun contentEquals(checkContentEquality: (data: T, otherData: T) -> Boolean)
fun defineViewTypes(mapToViewType: (data: T, position: Int) -> Int)
fun <V> create(createView: () -> ViewSource<V>): Bindable<T, V>
fun <V> create(viewType: Int, createView: () -> ViewSource<V>): Bindable<T, V>
fun <V> create(createView: (parent: ViewGroup) -> ViewSource<V>): Bindable<T, V>
fun <V> create(viewType: Int, createView: (parent: ViewGroup) -> ViewSource<V>): Bindable<T, V>
}
30 changes: 17 additions & 13 deletions adapt/src/main/java/io/github/vshnv/adapt/CollectingAdaptScope.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.github.vshnv.adapt

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

internal class CollectingAdaptScope<T: Any>: AdaptScope<T> {
private var itemEquals: (T, T) -> Boolean = {a, b -> a == b}
private var itemContentEquals: (T, T) -> Boolean = {a, b -> a == b}
private var viewTypeMapper: ((T, Int) -> Int)? = null
private var defaultBinder: CollectingBindable<T, *>? = null
private var defaultBinder: CollectingBindable<T, *>? = null
private val viewBinders: MutableMap<Int, CollectingBindable<T, *>> = mutableMapOf()

override fun defineViewTypes(mapToViewType: (T, Int) -> Int) {
Expand All @@ -21,18 +22,6 @@ internal class CollectingAdaptScope<T: Any>: AdaptScope<T> {
itemContentEquals = checkContentEquality
}

override fun <V> create(createView: () -> ViewSource<V>): Bindable<T, V> {
return CollectingBindable<T, V> { createView() }.apply {
defaultBinder = this
}
}

override fun <V> create(viewType: Int, createView: () -> ViewSource<V>): Bindable<T, V> {
return CollectingBindable<T, V> { createView() }.apply {
viewBinders[viewType] = this
}
}

internal fun buildAdapter(): AdaptAdapter<T> {
return AdaptAdapter<T>(
viewTypeMapper,
Expand All @@ -42,4 +31,19 @@ internal class CollectingAdaptScope<T: Any>: AdaptScope<T> {
itemContentEquals
)
}

override fun <V> create(createView: (parent: ViewGroup) -> ViewSource<V>): Bindable<T, V> {
return CollectingBindable<T, V>(createView).apply {
defaultBinder = this
}
}

override fun <V> create(
viewType: Int,
createView: (parent: ViewGroup) -> ViewSource<V>
): Bindable<T, V> {
return CollectingBindable<T, V>(createView).apply {
viewBinders[viewType] = this
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.github.vshnv.adapt

import android.view.View
import android.view.ViewGroup
import java.lang.RuntimeException

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

Expand Down

0 comments on commit 6ffe53c

Please sign in to comment.