Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add payload changes to adapters #1

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ class CartItemAdapter(
}

override fun onBindViewHolder(holder: CartItemViewHolder, position: Int) {
onBindViewHolder(holder, position, emptyList())
}

override fun onBindViewHolder(
holder: CartItemViewHolder, position: Int, payloads: List<Any>
) {
val item = getItem(position)
holder.bind(item)

val payloadList = payloads.map { it as CartItemPayload }
holder.bind(item, payloadList)
}

class DiffCallback : DiffUtil.ItemCallback<CartMenuItem>() {
Expand All @@ -31,6 +39,15 @@ class CartItemAdapter(

override fun areContentsTheSame(oldItem: CartMenuItem, newItem: CartMenuItem): Boolean =
oldItem == newItem

override fun getChangePayload(
oldItem: CartMenuItem,
newItem: CartMenuItem
): CartItemPayload {
return CartItemPayload(
countChanged = newItem.count != oldItem.count
)
}
}
}

Expand All @@ -39,20 +56,38 @@ class CartItemViewHolder(
private val interactionListener: CartItemInteractionListener?
) : RecyclerView.ViewHolder(binding.root) {

fun bind(cartMenuItem: CartMenuItem) {
with(binding) {
name.text = cartMenuItem.name
price.text = itemView.context.getString(R.string.price, cartMenuItem.price)
count.text = cartMenuItem.count.toString()

buttonRemove.isEnabled = cartMenuItem.count > 0
private var item: CartMenuItem? = null

init {
with(binding) {
buttonRemove.setOnClickListener {
interactionListener?.onRemove(cartMenuItem, bindingAdapterPosition)
item?.let {
interactionListener?.onRemove(it, bindingAdapterPosition)
}
}
buttonAdd.setOnClickListener {
interactionListener?.onAdd(cartMenuItem, bindingAdapterPosition)
item?.let {
interactionListener?.onAdd(it, bindingAdapterPosition)
}
}
}
}

fun bind(cartMenuItem: CartMenuItem, payloads: List<CartItemPayload>) {
item = cartMenuItem

with(binding) {
name.text = cartMenuItem.name
price.text = itemView.context.getString(R.string.price, cartMenuItem.price)

if (payloads.isEmpty() || payloads.any { it.countChanged }) {
count.text = cartMenuItem.count.toString()
buttonRemove.isEnabled = cartMenuItem.count > 0
}
}
}
}

data class CartItemPayload(
val countChanged: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ class MenuItemAdapter(
}

override fun onBindViewHolder(holder: CartMenuItemViewHolder, position: Int) {
onBindViewHolder(holder, position, emptyList())
}

override fun onBindViewHolder(
holder: CartMenuItemViewHolder, position: Int, payloads: List<Any>
) {
val item = getItem(position)
holder.bind(item)

val payloadList = payloads.map { it as CartMenuItemPayload }
holder.bind(item, payloadList)
}

class DiffCallback : DiffUtil.ItemCallback<CartMenuItem>() {
Expand All @@ -32,6 +40,15 @@ class MenuItemAdapter(

override fun areContentsTheSame(oldItem: CartMenuItem, newItem: CartMenuItem): Boolean =
oldItem == newItem

override fun getChangePayload(
oldItem: CartMenuItem,
newItem: CartMenuItem
): CartMenuItemPayload {
return CartMenuItemPayload(
countChanged = newItem.count != oldItem.count
)
}
}
}

Expand All @@ -40,28 +57,47 @@ class CartMenuItemViewHolder(
private val interactionListener: MenuItemInteractionListener? = null
) : RecyclerView.ViewHolder(binding.root) {

fun bind(cartMenuItem: CartMenuItem) {
itemView.setOnClickListener {
interactionListener?.onClick(cartMenuItem, bindingAdapterPosition)
private var item: CartMenuItem? = null

init {
with(binding) {
itemView.setOnClickListener {
item?.let {
interactionListener?.onClick(it, bindingAdapterPosition)
}
}
buttonRemove.setOnClickListener {
item?.let {
interactionListener?.onRemove(it, bindingAdapterPosition)
}
}
buttonAdd.setOnClickListener {
item?.let {
interactionListener?.onAdd(it, bindingAdapterPosition)
}
}
}
}

fun bind(cartMenuItem: CartMenuItem, payloads: List<CartMenuItemPayload>) {
item = cartMenuItem

with(binding) {
image.load(cartMenuItem.imageUrl) {
crossfade(true)
}

name.text = cartMenuItem.name
price.text = itemView.context.getString(R.string.price, cartMenuItem.price)
count.text = cartMenuItem.count.toString()

buttonRemove.isEnabled = cartMenuItem.count > 0

buttonRemove.setOnClickListener {
interactionListener?.onRemove(cartMenuItem, bindingAdapterPosition)
}
buttonAdd.setOnClickListener {
interactionListener?.onAdd(cartMenuItem, bindingAdapterPosition)
if (payloads.isEmpty() || payloads.any { it.countChanged }) {
count.text = cartMenuItem.count.toString()
buttonRemove.isEnabled = cartMenuItem.count > 0
}
}
}
}

data class CartMenuItemPayload(
val countChanged: Boolean = false
)