Skip to content

Commit

Permalink
refactor: LifecycleExtensions and binding adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
FunkyMuse committed Jul 14, 2021
1 parent a5f83e7 commit e647514
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fun LifecycleRegistry.firstThreeStages() {

fun Fragment.repeatingJobOnStarted(block: suspend CoroutineScope.() -> Unit) {
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED, block)
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED, block)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class AbstractViewBindingAdapter<T, VH : RecyclerView.ViewHolder, VB :
private val bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB,
areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
private val onCreateBinding: (binding:VB) -> Unit = {}
private val onCreateBinding: (holder: VH) -> Unit = {}

) :
ListAdapter<T, VH>(GenericDiffUtil(areItemsTheSameCallback, areContentsTheSameCallback)) {
Expand All @@ -40,7 +40,7 @@ abstract class AbstractViewBindingAdapter<T, VH : RecyclerView.ViewHolder, VB :
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val binding = bindingInflater.invoke(LayoutInflater.from(parent.context), parent, false)
val holder = setViewHolder(binding)
onCreateBinding(binding)
onCreateBinding(holder)

holder.itemView.setOnClickListenerCooldown {
if (holder.bindingAdapterPosition != RecyclerView.NO_POSITION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class AbstractViewBindingAdapterSingleSelection<T, VH : RecyclerView.Vi
private val bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB,
areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
private val onCreateBinding: (binding:VB) -> Unit = {}
private val onCreateBinding: (holder: VH) -> Unit = {}

) :
ListAdapter<T, VH>(GenericDiffUtil(areItemsTheSameCallback, areContentsTheSameCallback)) {
Expand All @@ -40,7 +40,7 @@ abstract class AbstractViewBindingAdapterSingleSelection<T, VH : RecyclerView.Vi
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val binding = bindingInflater.invoke(LayoutInflater.from(parent.context), parent, false)
val holder = setViewHolder(binding)
onCreateBinding(binding)
onCreateBinding(holder)

holder.itemView.setOnClickListenerCooldown {
if (holder.bindingAdapterPosition != RecyclerView.NO_POSITION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@ import androidx.viewbinding.ViewBinding
abstract class AbstractViewBindingCleanAdapter<T, VB : ViewBinding>(
private val bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB,
areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
private val onCreateBinding: (binding: VB, position:Int, item:T) -> Unit = {_,_,_->}
areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null }
) : ListAdapter<T, AbstractViewBindingCleanAdapter.AbstractViewHolder<VB>>(GenericDiffUtil(areItemsTheSameCallback, areContentsTheSameCallback)) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AbstractViewHolder<VB> {
val binding = bindingInflater.invoke(LayoutInflater.from(parent.context), parent, false)
val holder = AbstractViewHolder(binding)
//attach click listeners etc...
onCreateBinding(binding, holder.bindingAdapterPosition, getItem(holder.bindingAdapterPosition))
onCreateBinding(holder)
return holder
}

override fun onBindViewHolder(holder: AbstractViewHolder<VB>, position: Int) {
val item = getItem(holder.bindingAdapterPosition)
bindItems(item, position, itemCount, holder.binding, holder.itemView.context)
}

abstract fun bindItems(item: T, position: Int, itemCount: Int, binding: VB, context: Context)
abstract fun onCreateBinding(holder: AbstractViewHolder<VB>)

class AbstractViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class AbstractViewBindingHolderAdapter<T, VB : ViewBinding>(
private val bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB,
areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
private val onCreateBinding: (binding: VB, position:Int) -> Unit = {_,_->}
private val onCreateBinding: (holder: AbstractViewHolder<VB>) -> Unit = {}
) :
ListAdapter<T, AbstractViewBindingHolderAdapter.AbstractViewHolder<VB>>(GenericDiffUtil(areItemsTheSameCallback, areContentsTheSameCallback)) {

Expand All @@ -33,7 +33,7 @@ abstract class AbstractViewBindingHolderAdapter<T, VB : ViewBinding>(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AbstractViewHolder<VB> {
val binding = bindingInflater.invoke(LayoutInflater.from(parent.context), parent, false)
val holder = AbstractViewHolder(binding)
onCreateBinding(binding, holder.bindingAdapterPosition)
onCreateBinding(holder)

holder.itemView.setOnClickListenerCooldown {
if (holder.bindingAdapterPosition != RecyclerView.NO_POSITION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ inline fun <reified T, VB : ViewBinding> generateRecyclerWithHolder(
noinline bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB,
noinline areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
noinline areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
noinline onCreateBinding: (binding:VB, position:Int) -> Unit = {_,_->},
crossinline binder: (item: T, position: Int, itemCount: Int, binding: VB, context: Context) -> Unit): AbstractViewBindingHolderAdapter<T, VB> {
noinline onCreateBinding: (holder: AbstractViewBindingHolderAdapter.AbstractViewHolder<VB>) -> Unit = {},
crossinline binder: (item: T, position: Int, itemCount: Int, binding: VB, context: Context) -> Unit): AbstractViewBindingHolderAdapter<T, VB> {

return object : AbstractViewBindingHolderAdapter<T, VB>(bindingInflater, areItemsTheSameCallback, areContentsTheSameCallback, onCreateBinding) {
override fun bindItems(item: T, position: Int, itemCount: Int, binding: VB, context: Context) {
Expand All @@ -30,7 +30,7 @@ inline fun <reified T, VB : ViewBinding> RecyclerView.generateVerticalAdapterWit
noinline bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB,
noinline areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
noinline areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
noinline onCreateBinding: (binding:VB, position:Int) -> Unit = {_,_->},
noinline onCreateBinding: (holder: AbstractViewBindingHolderAdapter.AbstractViewHolder<VB>) -> Unit = {},
crossinline binder: (item: T, position: Int, itemCount: Int, binding: VB, context: Context) -> Unit,
hasFixedSize: Boolean = false, reverseLayout: Boolean = false): AbstractViewBindingHolderAdapter<T, VB> {

Expand All @@ -43,7 +43,7 @@ inline fun <reified T, VB : ViewBinding> RecyclerView.generateHorizontalAdapterW
noinline bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB,
noinline areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
noinline areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
noinline onCreateBinding: (binding:VB, position:Int) -> Unit = {_,_->},
noinline onCreateBinding: (holder: AbstractViewBindingHolderAdapter.AbstractViewHolder<VB>) -> Unit = {},
crossinline binder: (item: T, position: Int, itemCount: Int, binding: VB, context: Context) -> Unit,
hasFixedSize: Boolean = false, reverseLayout: Boolean = false): AbstractViewBindingHolderAdapter<T, VB> {

Expand Down

0 comments on commit e647514

Please sign in to comment.