package com.lukouguoji.module_base.ktx

import androidx.recyclerview.widget.RecyclerView
import com.lukouguoji.module_base.base.CommonAdapter
import com.lukouguoji.module_base.interfaces.IOnItemClickListener

/**
 * @author：孟凡华
 * @date：2022/11/11
 * @desc：RecyclerView拓展
 */

/**
 * 获取自定义适配器
 */
fun RecyclerView.commonAdapter(): CommonAdapter? {
    if (adapter == null) {
        return null
    }
    return if (this.adapter is CommonAdapter) this.adapter as CommonAdapter else null
}

/**
 * 刷新数据
 */
fun RecyclerView.refresh(items: List<Any>?) {
    post {
        adapter?.let {
            if (it is CommonAdapter) {
                it.refresh(items)
            }
        }
    }
}

/**
 * 加载更多数据
 */
fun RecyclerView.loadMore(items: List<Any>?) {
    post {
        adapter?.let {
            if (it is CommonAdapter) {
                it.loadMore(items)
            }
        }
    }
}

/**
 * 获取条目数据
 */
fun RecyclerView.getItem(position: Int): Any? {
    return commonAdapter()?.getItem(position)
}

/**
 * 添加条目点击事件
 */
fun RecyclerView.addOnItemClickListener(listener: IOnItemClickListener) {
    post {
        commonAdapter()?.addOnItemClickListener(listener)
    }
}