Skip to content

Commit

Permalink
Merge pull request #29 from Rere-kt/bugfix/viewholder-binding-without…
Browse files Browse the repository at this point in the history
…-payloads

Fixed method onBindViewHolder with payloads
  • Loading branch information
dionep authored Aug 19, 2022
2 parents ed1a7aa + 31ad0c5 commit 487224f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
20 changes: 12 additions & 8 deletions rekukler/src/main/java/com/rerekt/rekukler/MultibindingsAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ open class MultiBindingAdapter(
): RecyclerView.ViewHolder =
checkNotNull(
value = bindersSet.getOrNull(viewType)?.createViewHolder(parent),
lazyMessage = { "Unnable to find ViewBinder for viewType $viewType" }
lazyMessage = { "Unable to find ViewBinder for viewType $viewType" }
)

override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
Expand All @@ -52,19 +52,23 @@ open class MultiBindingAdapter(
position: Int,
payloads: MutableList<Any>
) {
getBinder(position).bindViewHolder(
viewHolder = viewHolder as RekuklerViewHolder<*, *>,
position = position,
item = items[position],
payloads = payloads
)
if (payloads.isEmpty()) {
super.onBindViewHolder(viewHolder, position, payloads)
} else {
getBinder(position).bindViewHolder(
viewHolder = viewHolder as RekuklerViewHolder<*, *>,
position = position,
item = items[position],
payloads = payloads
)
}
}

private fun getBinder(position: Int): ViewBinder<*, *> {
val item = items[position]
return checkNotNull(
value = bindersSet.find { it.isForItem(item) },
lazyMessage = { "Unnable to find ViewBinder for ${item::class}" }
lazyMessage = { "Unable to find ViewBinder for ${item::class}" }
)
}

Expand Down
15 changes: 7 additions & 8 deletions rekukler/src/main/java/com/rerekt/rekukler/ViewBinder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.rerekt.rekukler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

Expand Down Expand Up @@ -75,13 +74,13 @@ open class RekuklerViewHolder<Type: Any, Binding: ViewBinding>(
}

inline fun <reified Type: Any, Binding: ViewBinding> viewBinder(
noinline isForItem: (item: Any) -> Boolean = { it is Type },
noinline areItemsSame: (Type, Type) -> Boolean = { old, new -> old == new },
noinline areContentsSame: (Type, Type) -> Boolean = { old, new -> old == new },
noinline getChangePayload: (Type, Type) -> List<BasePayload<Type>> = { old, new -> listOf() },
noinline binder: (layoutInflater: LayoutInflater, parent: ViewGroup) -> Binding,
noinline layoutInflater: (parent: ViewGroup) -> LayoutInflater = { parent -> LayoutInflater.from(parent.context) },
noinline holderBinder: HolderBinder<Type, Binding>.(Type) -> Unit = {},
noinline isForItem: (item: Any) -> Boolean = { it is Type },
noinline areItemsSame: (Type, Type) -> Boolean = { old, new -> old == new },
noinline areContentsSame: (Type, Type) -> Boolean = { old, new -> old == new },
noinline getChangePayload: (Type, Type) -> List<BasePayload<Type>> = { old, new -> listOf() },
noinline binder: (layoutInflater: LayoutInflater, parent: ViewGroup) -> Binding,
noinline layoutInflater: (parent: ViewGroup) -> LayoutInflater = { parent -> LayoutInflater.from(parent.context) },
noinline holderBinder: HolderBinder<Type, Binding>.(Type) -> Unit = {},
) = ViewBinder(
binder = binder,
isForItem = isForItem,
Expand Down

0 comments on commit 487224f

Please sign in to comment.