Skip to content

Instantly share code, notes, and snippets.

@rohitjakhar
Created April 12, 2021 10:11
Show Gist options
  • Save rohitjakhar/bd233229037c3ba59984ccc6f0bc752e to your computer and use it in GitHub Desktop.
Save rohitjakhar/bd233229037c3ba59984ccc6f0bc752e to your computer and use it in GitHub Desktop.
package com.rohitjakhar.sagarkhurana.adapter
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.rohitjakhar.sagarkhurana.clicklistner.ItemClickListner
import com.rohitjakhar.sagarkhurana.databinding.DialogInputLayoutBinding
import com.rohitjakhar.sagarkhurana.databinding.SingleProductOrderRowBinding
import com.rohitjakhar.sagarkhurana.model.ProductModel
class AddOrder2Adapter(val itemClickListner: ItemClickListner) :
PagingDataAdapter<ProductModel, AddOrder2Adapter.AddOrderViewHolder>(Companion) {
companion object : DiffUtil.ItemCallback<ProductModel>() {
override fun areItemsTheSame(oldItem: ProductModel, newItem: ProductModel): Boolean {
return oldItem.productName == oldItem.productName
}
override fun areContentsTheSame(oldItem: ProductModel, newItem: ProductModel): Boolean {
return oldItem == newItem
}
}
override fun onBindViewHolder(holder: AddOrderViewHolder, position: Int) {
holder.bind(getItem(position) ?: return)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AddOrderViewHolder {
val mView =
SingleProductOrderRowBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return AddOrderViewHolder(mView)
}
inner class AddOrderViewHolder(val binding: SingleProductOrderRowBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(productModel: ProductModel) = with(binding) {
var counter: Int = 0
imgItemImage.load(productModel.productImage)
txtItemPrice.text = productModel.productPrice
txtItemName.text = productModel.productName
txtItemCount.text = counter.toString()
imgIncrease.setOnClickListener {
Log.d("clikkk", "clicks $counter")
counter += 1
txtItemCount.text = counter.toString()
if (counter >= 1) {
imgAddNote.isVisible = true
imgDecrease.isVisible = true
txtItemCount.isVisible = true
} else {
imgAddNote.isVisible = false
imgDecrease.isVisible = false
txtItemCount.isVisible = false
}
itemClickListner.OnCountClickListner(
productModel.productId,
counter.toString(),
productModel.productPrice.toInt()
)
}
imgDecrease.setOnClickListener {
if (counter > 1) {
counter -= 1
txtItemCount.text = counter.toString()
imgAddNote.isVisible = true
imgDecrease.isVisible = true
txtItemCount.isVisible = true
} else {
counter = 0
imgAddNote.isVisible = false
imgDecrease.isVisible = false
txtItemCount.isVisible = false
return@setOnClickListener
}
}
imgAddNote.setOnClickListener {
val context = it.context
val builder = AlertDialog.Builder(context)
builder.setTitle("Add Note ${productModel.productName}")
val mView = DialogInputLayoutBinding.inflate(
LayoutInflater.from(context)
)
builder.setView(mView.root)
builder.setPositiveButton(android.R.string.ok) { d, p ->
var isValid = true
if (mView.txtDialogeInputLayout.editText?.text.isNullOrEmpty()) {
mView.txtDialogeInputLayout.error = "Enter Note"
isValid = false
}
if (isValid) {
itemClickListner.OnNoteClickListner(mView.txtDialogeInputLayout.editText!!.text.toString())
d.dismiss()
}
}
builder.setNegativeButton(android.R.string.cancel){d, p ->
d.cancel()
}
builder.create()
builder.show()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment