DiffUtil areContentsTheSame() always returns true, after the contents are updated of the List in RecycleViewAdapter

349 views Asked by At

This is my RecyclerView Adapter Class. Clicking an item from the list UI should change of the particular item like background black to white. After clicking an item I am changing the status of the item 0 to 1 from the UI class, based on that status UI should change of the item. But inside diffUtil areContentTheSame() retures true.

class TimeSlotsAdapter(val adapterItemOnClick: (TimeSlots) -> Unit):RecyclerView.Adapter<TimeSlotsAdapter.TimeSlotsViewHolder>() {

private lateinit var binding: RvTimeSlotsBinding
private var timeSlots: List<TimeSlots> = ArrayList<TimeSlots>()


inner class TimeSlotsViewHolder : RecyclerView.ViewHolder {
    constructor(rvBinding: RvTimeSlotsBinding) : super(rvBinding.root) {
    }

    fun setItem(timeSlots: TimeSlots) {
        binding.timeSlotView.setOnClickListener { adapterItemOnClick(timeSlots) }
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TimeSlotsViewHolder {
    binding =
        RvTimeSlotsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    return TimeSlotsViewHolder(binding)
}

override fun onBindViewHolder(holder: TimeSlotsViewHolder, position: Int) {
    timeSlots[position]?.let { currentSlot ->
        with(holder) {

            if (currentSlot.status == "1") {
                binding.timeSlotView.background =
                    ContextCompat.getDrawable(
                        binding.timeSlotView.context,
                        R.drawable.time_slot_item_bg
                    )
                binding.timeSlotTv.setTextColor(
                    ContextCompat
                        .getColor(binding.timeSlotTv.context, R.color.black)
                )
                binding.timeSlotTv.text = "1 " + currentSlot.time

                Timber.e("${currentSlot.time} ${currentSlot.status}")
            } else {
                binding.timeSlotView.background =
                    ContextCompat.getDrawable(
                        binding.timeSlotView.context,
                        R.drawable.black_bg
                    )
                binding.timeSlotTv.setTextColor(
                    ContextCompat
                        .getColor(binding.timeSlotTv.context, R.color.white)
                )
                binding.timeSlotTv.text = "0 " + currentSlot.time
                Timber.e("${currentSlot.time} ${currentSlot.status}")
            }
            setItem(currentSlot)
        }
    }
}

override fun getItemCount(): Int = timeSlots.size

fun submitSlots(timeSlotList: List<TimeSlots>) {
    val oldList = timeSlots
    val diffResult: DiffUtil.DiffResult = DiffUtil.calculateDiff(
        TimeSlotsDiffCallBack(
            oldList,
            timeSlotList
        )
    )
    timeSlots = timeSlotList
    diffResult.dispatchUpdatesTo(this)
}

class TimeSlotsDiffCallBack(
    var oldTimeSlotsList: List<TimeSlots>,
    var newTimeSlotsList: List<TimeSlots>
) : DiffUtil.Callback() {

    override fun getOldListSize(): Int = oldTimeSlotsList.size

    override fun getNewListSize(): Int = newTimeSlotsList.size

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return (oldTimeSlotsList[oldItemPosition].time
                == newTimeSlotsList[newItemPosition].time)
    }

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        val oldTimeSlots = oldTimeSlotsList[oldItemPosition].status
        val newTimeSlots = newTimeSlotsList[newItemPosition].status
        Timber.e("old: $oldTimeSlots new: $newTimeSlots")
        return oldTimeSlots == newTimeSlots
    }
}

}

This is the UI class for submitting adapter list. Below the doClickTimeSlot() I am updating the status of the items.

class TimeSlotBottomSheetFragment : BottomSheetDialogFragment(){
private val obj: List<TimeSlots> = mutableListOf<TimeSlots>(
    TimeSlots("09:30", "0"),
    TimeSlots("10:30", "0"),
    TimeSlots("11:30", "0"),
    TimeSlots("12:30", "0"),
    TimeSlots("14:30", "0"),
    TimeSlots("15:30", "0"),
    TimeSlots("16:30", "0"),
    TimeSlots("17:30", "0"),
    TimeSlots("18:30", "0"),
    TimeSlots("19:30", "0"),
    TimeSlots("20:30", "0"),
    TimeSlots("21:30", "0")
)

private fun doClickTimeSlot(timeSlots: TimeSlots) {
    val newList: ArrayList<TimeSlots> = ArrayList<TimeSlots>()
    newList.addAll(obj)
    for (i in newList.indices) {
        if (newList[i].time == timeSlots.time) {
            newList[i].status = "1"
            Timber.e("" + newList[i].time + " " + newList[i].status)
        } else {
            newList[i].status = "0"
            Timber.e("" + newList[i].time + " " + newList[i].status)
        }
    }
    adapter.submitSlots(newList)
}
}
0

There are 0 answers