package ru.myitschool.work.ui.list



import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import ru.myitschool.work.R
import ru.myitschool.work.api.list.ListInfo

class ListAdapter : ListAdapter<ListInfo, ListViewHolder>(DiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_recycler_view_entrancedata, parent, false)
        return ListViewHolder(view)
    }

    override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
        val item = getItem(position)
        holder.bind(item)
    }

    class DiffCallback : DiffUtil.ItemCallback<ListInfo>() {
        override fun areItemsTheSame(oldItem: ListInfo, newItem: ListInfo): Boolean {
            return oldItem.time == newItem.time && oldItem.value == newItem.value
        }

        override fun areContentsTheSame(oldItem: ListInfo, newItem: ListInfo): Boolean {
            return oldItem == newItem
        }
    }
}