package com.xty.base.fragment

import android.view.LayoutInflater
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.xty.base.adapter.BaseAdapter
import com.xty.base.vm.BaseVm
import com.xty.common.R

abstract  class BaseFragList<V:BaseVm> :BaseVmFrag<V>() {
    var page=1
    private var recyclerView:RecyclerView?=null
    override fun initView() {
        super.initView()
        initAdapter()
        refresh()
    }
    abstract fun initAdapter()
    abstract fun refresh()


    //刷新问题
    fun setRecycleRefresh(view: RecyclerView, refresh: SmartRefreshLayout, isLoadMore:Boolean=false){
        recyclerView=view
        refresh.setOnRefreshListener {
            page=1
            refresh()
            refresh.finishRefresh(1000)
        }
        refresh.setEnableLoadMore(isLoadMore)
        refresh.setOnLoadMoreListener {
            page++
            refresh()
            refresh.finishLoadMore(1000)
        }
    }

    fun <T> setDate(adapter: BaseAdapter<T>, list: MutableList<T>){
        if(page==1){
            if(list.isNullOrEmpty()){
                adapter.setNewInstance(null)
                adapter.setEmptyView(getNoneView())
            }else{
                adapter.setNewInstance(list)
            }
        }else {
            if (list.isNullOrEmpty()) {
            }else{
                adapter.addData(list)
            }
        }
    }


    private fun getNoneView()= LayoutInflater.from(requireContext()).inflate(R.layout.view_none_data,recyclerView,false)


}