package com.example.smart.ui.query import android.text.Editable import android.text.TextUtils import android.text.TextWatcher import android.view.KeyEvent import android.view.LayoutInflater import android.view.inputmethod.EditorInfo import android.widget.TextView import android.widget.TextView.OnEditorActionListener import androidx.activity.viewModels import androidx.core.view.isVisible import com.example.basecore.BaseBindingActivity import com.example.basecore.util.sysbar.StatusBarUtils import com.example.basecore.widget.LoadingLayout import com.example.smart.databinding.ActivityQueryBinding import com.example.smart.ui.home.page.event.FEED_TAB_STATE import com.example.smart.ui.home.page.event.FEED_TAB_TYPE import com.example.smart.ui.query.bean.QueryKeyData import com.example.smart.ui.query.bean.QueryTagBean import com.example.smart.ui.query.event.QueryLiveData import com.example.basecore.util.toast import com.example.smart.bean.CommonOptionsBean import com.example.smart.enumlist.PlanState import com.example.smart.enumlist.PlanType import com.example.smart.ui.home.page.event.FEED_TAB_CLIENT import com.example.smart.ui.query.bean.QueryBean import com.example.smart.ui.query.bean.QueryInfoBean class QueryActivity : BaseBindingActivity() { private val binding by lazy { ActivityQueryBinding.inflate(LayoutInflater.from(mContext)) } private val viewModel: QueryViewModel by viewModels() /** * 关键字查询 */ private var queryKey: String = "" private var pageId: String? = "" override fun getRootView() = binding.root private var isLoading = false private val clientList = ArrayList() private val stateList = ArrayList() private val typeList = ArrayList() override fun initView() { window.enterTransition = android.transition.Fade() StatusBarUtils.transparentMode(this, true, isFullScreen = true) parserIntent() generateTab() binding.run { searchEnsure.setOnClickListener { onResult() } backImg.setOnClickListener { finish() } searchEdit.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged( s: CharSequence?, start: Int, count: Int, after: Int ) { } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { } override fun afterTextChanged(s: Editable?) { if (s != null && !TextUtils.isEmpty(s.toString())) { queryKey = s.toString() searchClear.isVisible = true } else { searchClear.isVisible = false } } }) searchEdit.isFocusable = true searchEdit.isFocusableInTouchMode = true searchEdit.requestFocus() /** * 搜索点击监听 */ searchEdit.setOnEditorActionListener(object : OnEditorActionListener { override fun onEditorAction( v: TextView?, actionId: Int, event: KeyEvent? ): Boolean { if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) { onResult() return true } return false } }) loadingLayout.bindEvent(object : LoadingLayout.LoadEvent { override fun onRetryLoad() { fetchOptions() } }) } } private fun onResult() { queryKey = binding.searchEdit.text.toString().trim() val queryTags = binding.queryTagContainer.getQueryTags() if (TextUtils.isEmpty(queryKey) && queryTags.isEmpty()) { toast("请输入查询内容") return } QueryLiveData.setQueryResult(QueryKeyData(pageId, queryKey, queryTags)) finish() } private fun parserIntent() { val uri = intent.data if (uri != null) { pageId = uri.getQueryParameter("page_id") } } override fun bindDataObserver() { super.bindDataObserver() fetchOptions() viewModel.queryData.observe(this) { buildClientIds(it) val clientInfo = QueryInfoBean("委托方", clientList) val state = QueryInfoBean("计划状态", stateList) val type = QueryInfoBean("计划类型", typeList) val queryBean = QueryBean(pageId, clientInfo, state, type) binding.queryTagContainer.buildTagLayout(queryBean) } } private fun generateTab() { val planTypes = PlanType.values() for (type in planTypes) { val tag = QueryTagBean(type.name, type.desc, FEED_TAB_TYPE) typeList.add(tag) } val states = PlanState.values() for (state in states) { val tag = QueryTagBean(state.name, state.desc, FEED_TAB_STATE) stateList.add(tag) } } private fun buildClientIds(list: List) { for (bean in list) { val tag = QueryTagBean(bean.code, bean.name, FEED_TAB_CLIENT) clientList.add(tag) } } private fun fetchOptions() { if (isLoading) { toast("正在重试中请稍后......") return } isLoading = true viewModel.optionsRequest { isLoading = false binding.loadingLayout.run { isVisible = true loadError() } } } }