罗明文
2024-06-18 c9f6a2283ee7e5595c91c6d721726a89a3ab9ecd
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
package com.dollearn.student.ui.home
 
import android.app.Activity
import android.content.Intent
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.LinearLayoutManager
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.SPUtils
import cn.sinata.xldutils.utils.myToast
import cn.sinata.xldutils.view.SwipeRefreshRecyclerLayout
import cn.sinata.xldutils.visible
import com.dollearn.student.R
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.Course
import com.dollearn.student.network.request
import com.dollearn.student.ui.TransparentStatusBarActivity
import com.dollearn.student.ui.course.CourseDetailActivity
import com.dollearn.student.ui.home.adapter.CourseAdapter
import com.dollearn.student.utils.Const
import kotlinx.android.synthetic.main.activity_search.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.startActivityForResult
import org.jetbrains.anko.textColorResource
 
class SearchActivity : TransparentStatusBarActivity() {
    override fun setContentView() = R.layout.activity_search
    private val gradeId by lazy {
        SPUtils.instance().getInt(Const.User.GRADE, 0)
    }
    private var page = 1
    private var subjectId:String? = null
 
    private val subjects by lazy { //筛选学科项
        arrayListOf(tv_all,tv_chinese,tv_math,tv_english,tv_physics,tv_chemistry,
            tv_biology,tv_geography,tv_history,tv_politics,tv_other)
    }
    private val datas = arrayListOf<Course>()
    private val adapter = CourseAdapter(datas)
 
    override fun initClick() {
        iv_back.onClick {
            finish()
        }
        tv_subject.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked){
                bg.visible()
                cl_filter.visible()
            }else{
                bg.gone()
                cl_filter.gone()
            }
        }
        bg.onClick {
            tv_subject.isChecked = false
        }
        cl_filter.onClick {}
        adapter.setOnItemClickListener { _, position ->
            val course = datas[position]
 
                startActivityForResult<CourseDetailActivity>(1,"course" to course)
        }
 
        subjects.forEachIndexed{i,it->
            it.onClick {_->
                it.isChecked = !it.isChecked
            }
        }
 
        tv_reset.onClick {
            subjects.forEach {
                it.isChecked = false
            }
        }
 
        tv_sure.onClick {
            tv_subject.isChecked = false
            subjectId = if (tv_all.isChecked) "" else subjects.filter { it.isChecked }.joinToString(",") {
                (subjects.indexOf(it)).toString()
            }
            tv_subject.textColorResource = if (subjectId.isNullOrEmpty()) R.color.textColor else R.color.colorAccent
            mSwipeReLayout.isRefreshing = true
            page = 1
            search(et_search.text.toString())
        }
    }
 
    override fun initView() {
        titleBar.gone()
        et_search.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH){
                val key = et_search.text.toString()
                if (key.isNotEmpty()){
                    mSwipeReLayout.isRefreshing = true
                    page = 1
                    search(key)
                }else{
                    myToast("请输入搜索内容")
                }
                hideInputMethod()
            }
            return@setOnEditorActionListener false
        }
        mSwipeReLayout.setLayoutManager(LinearLayoutManager(this))
        mSwipeReLayout.setAdapter(adapter)
        mSwipeReLayout.setOnRefreshListener(object :SwipeRefreshRecyclerLayout.OnRefreshListener{
            override fun onRefresh() {
                page = 1
                search(et_search.text.toString())
            }
 
            override fun onLoadMore() {
                page++
                search(et_search.text.toString())
            }
        })
        mSwipeReLayout.isRefreshing = false
    }
 
    private fun search(key: String) {
        tv_subject.visible()
        HttpManager.querySearchCourseList(key,page,gradeId,subjectId).request(this,success = {_,data->
            mSwipeReLayout.isRefreshing = false
            data?.let {
                if (page == 1)
                    datas.clear()
                datas.addAll(it)
                if (datas.isEmpty())
                    mSwipeReLayout.setLoadMoreText("暂无数据")
                else if (it.isEmpty())
                    mSwipeReLayout.setLoadMoreText("没有更多")
                adapter.notifyDataSetChanged()
            }
        }){_,_->
            mSwipeReLayout.isRefreshing = false
        }
        adapter.notifyDataSetChanged()
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK){
            page = 1
            search(et_search.text.toString())
            setResult(Activity.RESULT_OK)
        }
    }
}