riku
2022-02-18 d59d55575d913646b7a90fca651904ab889c6723
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
package cn.flightfeather.thirdappmodule.module.common
 
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.inputmethod.EditorInfo
import cn.flightfeather.thirdappmodule.R
import cn.flightfeather.thirdappmodule.bean.entity.Subtask
import cn.flightfeather.thirdappmodule.module.base.BaseActivity
import cn.flightfeather.thirdappmodule.module.inspection.InspectionDetailActivity
import cn.flightfeather.thirdappmodule.util.Constant
import cn.flightfeather.thirdappmodule.util.DateUtil
import cn.flightfeather.thirdappmodule.util.Domain
import cn.flightfeather.thirdappmodule.view.recyclerview.BaseCustomViewHolder
import cn.flightfeather.thirdappmodule.view.recyclerview.MySection
import cn.flightfeather.thirdappmodule.view.recyclerview.RecyclerViewPanel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import kotlinx.android.synthetic.main.activity_search.*
import kotlinx.android.synthetic.main.layout_search_title.*
 
/**
 * @author riku
 * Date: 2022/2/17
 */
class SearchActivity : BaseActivity() {
 
    private lateinit var searViewModel: SearchViewModel
    private lateinit var recyclerViewPanel: RecyclerViewPanel<Subtask>
 
    override fun getLayoutId(): Int = R.layout.activity_search
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        searViewModel = ViewModelProviders.of(this).get(SearchViewModel::class.java)
 
        initClickListener()
        initRecyclerView()
    }
 
    private fun initClickListener() {
        // 回退键
        img_cancel.setOnClickListener { onBackPressed() }
        // 搜索清空文本按钮
        btn_clear.visibility = View.GONE
        btn_clear.setOnClickListener { edt_search.setText("") }
        // 搜索框
        autoShowKeyboard(edt_search)
        edt_search.apply {
            // “搜索”按钮监听事件
            setOnEditorActionListener { v, actionId, _ ->
                if (actionId == EditorInfo.IME_ACTION_SEARCH && !v.text.isNullOrBlank()) {
                    searViewModel.keyword = v.text.toString()
                    recyclerViewPanel.startRefresh()
                    hideKeyboard()
                }
                true
            }
            // 清空文字按钮的显示逻辑
            addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
                override fun afterTextChanged(s: Editable?) {
                    btn_clear.visibility=if (s.isNullOrBlank()) View.GONE else View.VISIBLE
                }
            })
        }
 
    }
 
    private fun initRecyclerView() {
        recyclerViewPanel = object : RecyclerViewPanel<Subtask>(searViewModel.subtaskResult, recycler_view, this) {
            override fun getItemLayoutId(): Int? = R.layout.item_task_list_2
 
            override fun onBindView(holder: BaseCustomViewHolder, item: MySection<Subtask>?) {
                item?.t?.let {s ->
                    val planStartTime = DateUtil.getYearMonthDayStr(s.planstarttime, true)
 
                    val names = s.executorrealtimes?.replace(Constant.CONNECTOR.toRegex(), Constant.CONNECTOR_FOR_VIEW)
                    holder.setText(R.id.tv_item_task_list_name, s.name)
                        .setText(R.id.tv_item_task_list_address, s.scenseaddress)
                        .setText(R.id.tv_item_task_list_time, planStartTime)
                        .setText(R.id.tv_task_staff, names)
                    when (s.status) {
                        Domain.TASK_STATUS_RUNNING -> holder.setImageResource(R.id.img_task_status, R.mipmap.ic_task_executing)
                        Domain.TASK_STATUS_FINISHED -> holder.setImageResource(R.id.img_task_status, R.mipmap.ic_task_complete)
                        else -> holder.setImageResource(R.id.img_task_status, R.mipmap.ic_task_ready_to_start)
                    }
                }
            }
 
            override fun onItemClick(adapter: BaseQuickAdapter<Any?, BaseViewHolder>, view: View, position: Int, dataList: List<Subtask>) {
                super.onItemClick(adapter, view, position, dataList)
                val intent = Intent(this@SearchActivity, InspectionDetailActivity::class.java)
                intent.putExtra("subTask", dataList[position])
                startActivity(intent)
            }
        }.also {
            it.init()
            it.startRefresh()
        }
    }
}