package com.ux.video.file.filerecovery.base



import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.viewbinding.ViewBinding

/**
 * 高性能 RecyclerView 通用适配器基类，支持 DiffUtil + ViewBinding
 */
abstract class DiffBaseAdapter<K, T : ViewBinding>(
    protected val mContext: Context,
    diffCallback: DiffUtil.ItemCallback<K>
) : ListAdapter<K, DiffBaseAdapter.VHolder<T>>(diffCallback) {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VHolder<T> {
        return VHolder(getViewBinding(parent))
    }

    override fun onBindViewHolder(holder: VHolder<T>, position: Int) {
        bindItem(holder, getItem(position))
    }

    /**
     * 更新数据（自动触发 DiffUtil 计算）
     */
    fun submitData(items: List<K>?) {
        submitList(items ?: emptyList())
    }

    protected abstract fun getViewBinding(parent: ViewGroup): T
    protected abstract fun bindItem(holder: VHolder<T>, item: K)

    class VHolder<V : ViewBinding>(val vb: V) : androidx.recyclerview.widget.RecyclerView.ViewHolder(vb.root)
}
