18034727885
2026-04-27 3a4c1bbb2cf4fb1cdda8a5061b0674f30e7456b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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<QueryTagBean>()
    private val stateList = ArrayList<QueryTagBean>()
    private val typeList = ArrayList<QueryTagBean>()
 
    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<CommonOptionsBean>) {
 
        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()
            }
        }
    }
 
 
}