Skip to content

Commit

Permalink
feat: recycler generator with one view holder
Browse files Browse the repository at this point in the history
refactor: removed image decoder for coroutines
  • Loading branch information
CraZyLegenD committed Dec 19, 2020
1 parent e9e71ba commit 5951676
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 51 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.crazylegend.recyclerview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -52,23 +51,6 @@ abstract class AbstractViewBindingAdapter<T, VH : RecyclerView.ViewHolder, VB :
return holder
}

/**
* Sets an on click listener for a view, but ensures the action cannot be triggered more often than [coolDown] milliseconds.
*/
private inline fun View.setOnClickListenerCooldown(coolDown: Long = 1000L, crossinline action: (view: View) -> Unit) {
setOnClickListener(object : View.OnClickListener {
var lastTime = 0L
override fun onClick(v: View) {
val now = System.currentTimeMillis()
if (now - lastTime > coolDown) {
action(v)
lastTime = now
}
}
})
}


@Suppress("UNCHECKED_CAST")
private fun setViewHolder(binding: ViewBinding): VH = viewHolder(binding as VB)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.crazylegend.recyclerview

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.crazylegend.recyclerview.clickListeners.forItemClickListener


/**
* Created by crazy on 4/5/20 to long live and prosper !
* Takes leverage of not providing that damn layout res id
*
* USAGE:
* class TestViewBindingAdapter : AbstractViewBindingAdapter<TestModel, TestViewHolderShimmer, CustomizableCardViewBinding>(
::TestViewHolderShimmer, CustomizableCardViewBinding::inflate
)
*
*/
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 }
) :
ListAdapter<T, AbstractViewBindingHolderAdapter.AbstractViewHolder<VB>>(GenericDiffUtil(areItemsTheSameCallback, areContentsTheSameCallback)) {

var forItemClickListener: forItemClickListener<T>? = null
var onLongClickListener: forItemClickListener<T>? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AbstractViewHolder<VB> {
val binding = bindingInflater.invoke(LayoutInflater.from(parent.context), parent, false)
val holder = AbstractViewHolder(binding)

holder.itemView.setOnClickListenerCooldown {
if (holder.adapterPosition != RecyclerView.NO_POSITION)
forItemClickListener?.forItem(holder.adapterPosition, getItem(holder.adapterPosition), it)
}
holder.itemView.setOnLongClickListener {
if (holder.adapterPosition != RecyclerView.NO_POSITION)
onLongClickListener?.forItem(holder.adapterPosition, getItem(holder.adapterPosition), it)
true
}
return holder
}

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

abstract fun bindItems(item: T, position: Int, itemCount: Int, binding: VB)

class AbstractViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root)
}
Original file line number Diff line number Diff line change
Expand Up @@ -663,4 +663,21 @@ fun RecyclerView.smoothSnapToPosition(position: Int, performClick: Boolean = tru
}
smoothScroller.targetPosition = position
layoutManager?.startSmoothScroll(smoothScroller)
}


/**
* Sets an on click listener for a view, but ensures the action cannot be triggered more often than [coolDown] milliseconds.
*/
internal inline fun View.setOnClickListenerCooldown(coolDown: Long = 1000L, crossinline action: (view: View) -> Unit) {
setOnClickListener(object : View.OnClickListener {
var lastTime = 0L
override fun onClick(v: View) {
val now = System.currentTimeMillis()
if (now - lastTime > coolDown) {
action(v)
lastTime = now
}
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.crazylegend.recyclerview

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding


/**
* Created by crazy on 3/9/20 to long live and prosper !
*/


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 },
crossinline binder: (item: T, position: Int, itemCount: Int, binding: VB) -> Unit): AbstractViewBindingHolderAdapter<T, VB> {

return object : AbstractViewBindingHolderAdapter<T, VB>(bindingInflater, areItemsTheSameCallback, areContentsTheSameCallback) {
override fun bindItems(item: T, position: Int, itemCount: Int, binding: VB) {
binder(item, position, itemCount, binding)
}
}
}

inline fun <reified T, VB : ViewBinding> RecyclerView.generateVerticalAdapterWithHolder(
noinline bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB,
noinline areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
noinline areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
crossinline binder: (item: T, position: Int, itemCount: Int, binding: VB) -> Unit,
hasFixedSize: Boolean = false, reverseLayout: Boolean = false): AbstractViewBindingHolderAdapter<T, VB> {

val adapter = generateRecyclerWithHolder(bindingInflater, areItemsTheSameCallback, areContentsTheSameCallback, binder)
initRecyclerViewAdapter(adapter, RecyclerView.VERTICAL, hasFixedSize, reverseLayout)
return adapter
}

inline fun <reified T, VB : ViewBinding> RecyclerView.generateHorizontalAdapterWithHolder(
noinline bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB,
noinline areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
noinline areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null },
crossinline binder: (item: T, position: Int, itemCount: Int, binding: VB) -> Unit,
hasFixedSize: Boolean = false, reverseLayout: Boolean = false): AbstractViewBindingHolderAdapter<T, VB> {

val adapter = generateRecyclerWithHolder(bindingInflater, areItemsTheSameCallback, areContentsTheSameCallback, binder)
initRecyclerViewAdapter(adapter, RecyclerView.HORIZONTAL, hasFixedSize, reverseLayout)
return adapter
}


Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ fun ArrayMap<String, RequestBody>.addImageByteStringsToRetrofit(byteList: List<B
}
}
}

///

val generateRetrofitImageKeyName
Expand Down

0 comments on commit 5951676

Please sign in to comment.