package com.example.smart.ui.creatplan
|
|
import android.text.TextUtils
|
import android.view.LayoutInflater
|
import androidx.activity.viewModels
|
import com.example.basecore.BaseBindingActivity
|
import com.example.smart.bean.CommonOptionsBean
|
import com.example.smart.databinding.ActivityCreatePlanBinding
|
import com.example.smart.ui.creatplan.bean.CreatePlanParam
|
import com.example.smart.ui.creatplan.bean.PlanEventBean
|
import com.example.smart.ui.creatplan.event.PlanCreateEvent
|
import com.example.smart.widget.common.ICommonDataEvent
|
import com.example.smart.widget.dialog.CommonListPopup
|
import com.example.basecore.util.showCenterToast
|
import com.example.smart.bean.EntityRef
|
import com.example.smart.enumlist.PlanType
|
import kotlinx.coroutines.flow.flow
|
|
/**
|
* 新建计划
|
*/
|
class CreatePlanActivity : BaseBindingActivity() {
|
|
private val binding by lazy { ActivityCreatePlanBinding.inflate(LayoutInflater.from(mContext)) }
|
|
private val viewModel: CreatePlanViewModel by viewModels()
|
|
private var isSelectedEdit = false
|
|
private var lastEntrustSearchResultList = ArrayList<CommonOptionsBean>()
|
|
override fun getRootView() = binding.root
|
|
override fun initView() {
|
transStatusBar()
|
requestOptions()
|
addListener()
|
bindEventObserver()
|
}
|
|
private fun bindEventObserver() {
|
//流程修改,新建计划时不再新建项目
|
PlanCreateEvent.observe(this) {
|
if (it.code == PlanCreateEvent.CODE_PROJECT_SUCCESS) {
|
createPlan()
|
}
|
}
|
}
|
|
private fun addListener() {
|
binding.apply {
|
|
/**
|
* 设备体系类型重新选择
|
*/
|
systemWidget.setDataEvent(object : ICommonDataEvent {
|
override fun onClickView(containerId: String) {
|
mathWidget.clearTxt()
|
gradeWidget.clearTxt()
|
requestSystemChildOptions(containerId)
|
}
|
})
|
|
//设备体系模型
|
equipmentWidget.setDataEvent(object : ICommonDataEvent {
|
override fun onClickView(containerId: String) {
|
facilityWidget.clearTxt()
|
facilityMathWidget.clearTxt()
|
viewModel.callDeviceSystemChildOptions(containerId)
|
}
|
})
|
|
// entrustWidget.getEdtView().textChangeFlow() // 构建输入框文字变化流
|
// .filter {
|
// val isSelected = isSelectedEdit
|
// if (isSelectedEdit) {
|
// isSelectedEdit = false
|
// }
|
// it.isNotEmpty() && !isSelected
|
// }
|
// .debounce(300) // 300ms防抖
|
// .flatMapLatest {
|
// searchFlow(it.toString())
|
// } // 新搜索覆盖旧搜索
|
// .flowOn(Dispatchers.IO) // 让搜索在异步线程中执行
|
//// .onEach { updateUi(it) } // 获取搜索结果并更新界面
|
// .launchIn(viewModel.viewModelScope) // 在主线程收集搜索结果
|
|
stepTv.setOnClickListener {
|
// var isClickNameRight = false
|
// for (data in lastEntrustSearchResultList) {
|
// if (data.name == binding.entrustWidget.getContent()) {
|
// isClickNameRight = true
|
// break
|
// }
|
// }
|
// if (!isClickNameRight) {
|
// showCenterToast("请输入正确的委托方")
|
// return@setOnClickListener
|
// }
|
if (!checkData()) {
|
return@setOnClickListener
|
}
|
createPlan()
|
}
|
}
|
}
|
|
private fun searchFlow(key: String) = flow { emit(search(key)) }
|
|
private fun search(key: String) {
|
viewModel.searchEntrustName(key) {}
|
}
|
|
//
|
private fun requestOptions() {
|
val list = ArrayList<CommonOptionsBean>()
|
//计划类型
|
PlanType.values().forEach {
|
list.add(CommonOptionsBean(it.name, it.desc))
|
}
|
binding.planWidget.bindData(list)
|
|
//综合体系模型
|
viewModel.requestSystemOptions {
|
showCenterToast(it)
|
}
|
|
//设备体系模型
|
viewModel.requestDeviceSystemOptions()
|
|
//委托方
|
viewModel.requestEntrustOption()
|
}
|
|
/**
|
* 综合体系模型子模型
|
* 综合打分专家&综合模糊专家
|
*/
|
private fun requestSystemChildOptions(id: String) {
|
viewModel.requestSystemChildOptions(id) {
|
showCenterToast(it)
|
}
|
}
|
|
private var popupWindow: CommonListPopup? = null
|
override fun bindDataObserver() {
|
viewModel.run {
|
createResult.observe(this@CreatePlanActivity) {
|
PlanCreateEvent.setResult(PlanEventBean(PlanCreateEvent.CODE_PLAN_SUCCESS))
|
finish()
|
}
|
entrustOptionsBean.observe(this@CreatePlanActivity) {
|
if (it.isEmpty()) {
|
return@observe
|
}
|
binding.entrustWidget.bindData(it)
|
}
|
|
/**
|
* 搜索
|
*/
|
// searchResult.observe(this@CreatePlanActivity) {
|
// if (it.isEmpty()) {
|
// val bean = CommonOptionsBean("", "无数据")
|
// it.add(bean)
|
// } else {
|
// lastEntrustSearchResultList.clear()
|
// lastEntrustSearchResultList.addAll(it)
|
// }
|
//
|
// popupWindow?.dismiss()
|
// popupWindow =
|
// CommonListPopup(mContext, it, object : CommonListPopup.ItemClickListener {
|
// override fun onItemClick(bean: CommonOptionsBean) {
|
// if (bean.code.isNotEmpty()) {
|
// isSelectedEdit = true
|
// binding.entrustWidget.updateData(bean)
|
// }
|
// }
|
// })
|
// popupWindow!!.contentView.measure(
|
// View.MeasureSpec.UNSPECIFIED,
|
// View.MeasureSpec.UNSPECIFIED
|
// )
|
// popupWindow!!.showAsDropDown(
|
// binding.entrustWidget.getEdtView(),
|
// dp2px(14),
|
// dp2px(14),
|
// Gravity.RIGHT or Gravity.BOTTOM
|
// )
|
// }
|
|
//综合
|
systemOptionsBean.observe(this@CreatePlanActivity) {
|
if (it.isEmpty()) {
|
return@observe
|
}
|
binding.systemWidget.bindData(it)
|
|
requestSystemChildOptions(it.first().code)
|
}
|
|
//综合体系模型子模型 数学
|
systemScoreOptionsBean.observe(this@CreatePlanActivity) {
|
if (it.isEmpty()) {
|
return@observe
|
}
|
binding.mathWidget.bindData(it)
|
|
}
|
//综合体系模型子模型 模糊
|
systemFuzzyOptionsBean.observe(this@CreatePlanActivity) {
|
if (it.isEmpty()) {
|
return@observe
|
}
|
binding.gradeWidget.bindData(it)
|
}
|
|
//设备体系模型
|
deviceOptionBeans.observe(this@CreatePlanActivity) {
|
if (it.isEmpty()) {
|
return@observe
|
}
|
binding.equipmentWidget.bindData(it)
|
viewModel.callDeviceSystemChildOptions(it.first().code)
|
}
|
|
//设备体系模型 数学
|
deviceScoreOptionsBean.observe(this@CreatePlanActivity) {
|
if (it.isEmpty()) {
|
return@observe
|
}
|
binding.facilityMathWidget.bindData(it)
|
}
|
|
//设备体系模型 模糊
|
deviceFuzzyOptionsBean.observe(this@CreatePlanActivity) {
|
if (it.isEmpty()) {
|
return@observe
|
}
|
binding.facilityWidget.bindData(it)
|
}
|
}
|
|
}
|
|
/**
|
* 新建计划
|
*/
|
private fun createPlan() {
|
if (!checkData()) {
|
return
|
}
|
val param = CreatePlanParam()
|
|
binding.apply {
|
param.name = nameWidget.getContent()
|
param.type = planWidget.getContainerId()
|
|
param.client = EntityRef(entrustWidget.getContainerId().toInt())
|
param.systemModel = EntityRef(systemWidget.getContainerId().toInt())
|
//生成其他成员变量赋值
|
param.systemFuzzyExpert = EntityRef(mathWidget.getContainerId().toInt())
|
param.systemScoreExpert = EntityRef(gradeWidget.getContainerId().toInt())
|
param.deviceModel = EntityRef(equipmentWidget.getContainerId().toInt())
|
param.deviceScoreExpert = EntityRef(facilityMathWidget.getContainerId().toInt())
|
param.deviceFuzzyExpert = EntityRef(facilityWidget.getContainerId().toInt())
|
|
}
|
|
viewModel.createItem(param) {
|
showCenterToast(it)
|
}
|
|
}
|
|
private fun checkData(): Boolean {
|
binding.run {
|
if (!checkEmptyData(nameWidget.getPreTxt(), nameWidget.getContent())) {
|
return false
|
}
|
if (!checkEmptyData(entrustWidget.getPreTxt(), entrustWidget.getContent())) {
|
return false
|
}
|
|
if (!checkEmptyData(planWidget.getPreTxt(), planWidget.getContent())) {
|
return false
|
}
|
if (!checkEmptyData(systemWidget.getPreTxt(), systemWidget.getContent())) {
|
return false
|
}
|
if (!checkEmptyData(mathWidget.getPreTxt(), mathWidget.getContent())) {
|
return false
|
}
|
if (!checkEmptyData(gradeWidget.getPreTxt(), gradeWidget.getContent())) {
|
return false
|
}
|
if (!checkEmptyData(equipmentWidget.getPreTxt(), equipmentWidget.getContent())) {
|
return false
|
}
|
if (!checkEmptyData(facilityMathWidget.getPreTxt(), facilityMathWidget.getContent())) {
|
return false
|
}
|
|
if (!checkEmptyData(facilityWidget.getPreTxt(), facilityWidget.getContent())) {
|
return false
|
}
|
|
}
|
|
return true
|
}
|
|
private fun checkEmptyData(name: String, content: String): Boolean {
|
if (TextUtils.isEmpty(content)) {
|
showCenterToast("请输入$name")
|
return false
|
}
|
|
return true
|
}
|
}
|