package com.example.smart.ui.projectevent.frag import android.content.Context import android.text.TextUtils import androidx.lifecycle.viewModelScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.example.basecore.base.BaseContextViewModel import com.example.basecore.net.http.Result import com.example.basecore.widget.LoadingLayout import com.example.smart.reqeust.RetrofitClient import com.example.smart.ui.projectevent.adapter.CAdapter import com.example.smart.ui.exclude.adapter.HAdapter import com.example.smart.ui.exclude.adapter.TreeBaseAdapter import com.example.smart.ui.exclude.adapter.VAdapter import com.example.smart.ui.projectevent.bean.ProjectEventBean import com.example.smart.ui.projectevent.bean.SysProblemBean import com.example.smart.ui.projectevent.bean.SystemTreeBean import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch class SystemViewModel : BaseContextViewModel() { private lateinit var adapterV: VAdapter private lateinit var adapterH: HAdapter private lateinit var adapterC: CAdapter private lateinit var taskId: String private val vList = ArrayList() private val hList = ArrayList() private val cList = ArrayList() private var loadView: LoadingLayout? = null fun bindData( context: Context, id :String, list: ArrayList, vListView: RecyclerView, hListView: RecyclerView, cListView: RecyclerView ) { vList.addAll(list) this.taskId = id adapterV = VAdapter(context, vList) vListView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) vListView.adapter = adapterV adapterV.setTreeClickEvent(object : TreeBaseAdapter.OnTreeClickEvent { override fun onClicked(pos: Int, path: String) { val childSixTrees = vList[adapterV.getSelectItem()].children[pos].children adapterH.notifyHChanged(childSixTrees) if (!childSixTrees.isNullOrEmpty()) { callSystemPro(childSixTrees[0].path) } } }) adapterH = HAdapter(context, hList) adapterH.setTreeClickEvent(object : TreeBaseAdapter.OnTreeClickEvent { override fun onClicked(pos: Int, path: String) { callSystemPro(path) } }) hListView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) hListView.adapter = adapterH val childFourTree = vList[adapterV.getSelectItem()].children[0] //一般来说横向层级不会为空 if (vList.isEmpty() || childFourTree.children.isNullOrEmpty()) { val tree = SystemTreeBean() tree.isFilledSelf = true hList.add(tree) } else { adapterH.notifyHChanged(childFourTree.children) if (!childFourTree.children.isNullOrEmpty()) { callSystemPro(childFourTree.children[0].path) } } adapterC = CAdapter(context, cList) cListView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) cListView.adapter = adapterC } fun bindId(from: String, projectId: String, planId: String) { adapterC.bindId(from, projectId, planId) } /** * 具体问题列表 */ private fun callSystemPro(path: String) { loadView?.loadingData() if (TextUtils.isEmpty(path) || TextUtils.isEmpty(taskId)) { loadView?.loadEmpty() return } viewModelScope.launch(Dispatchers.Main) { val result = safeCall { callList(RetrofitClient.api.callSystemTreeList(path, taskId)) } if (result is Result.Success) { if (result.data.isEmpty()) { loadView?.loadEmpty() adapterC.notifyHChanged(result.data) return@launch } loadView?.loadNull() adapterC.notifyHChanged(result.data) } else if (result is Result.Error) { loadView?.loadEmpty() } } } private fun generateNullProblemBean(): SysProblemBean { val bean = SysProblemBean() bean.isNullContent = true return bean } fun bindLoading(loadView: LoadingLayout) { this.loadView = loadView } override fun onCleared() { super.onCleared() this.loadView = null } }