罗明文
2024-06-19 481723ce3c05d74fec53b8567b9c79d77bdcc155
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
package com.dollearn.student.ui.course
 
import android.view.animation.AnimationUtils
import androidx.recyclerview.widget.LinearLayoutManager
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.visible
import com.dollearn.student.R
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.CommonData
import com.dollearn.student.network.entity.Practice
import com.dollearn.student.network.request
import com.dollearn.student.ui.TransparentStatusBarActivity
import com.dollearn.student.ui.course.adapter.PracticeAdapter
import com.dollearn.student.ui.home.VideoDetailActivity
import com.dollearn.student.ui.home.adapter.FilterAdapter
import com.dollearn.student.utils.extention.clickDelay
import kotlinx.android.synthetic.main.activity_practice_list.*
import org.jetbrains.anko.startActivity
 
class PracticeListActivity:TransparentStatusBarActivity() {
    override fun setContentView() = R.layout.activity_practice_list
 
    private var typeId:Int? = null
    private var search:String? = null
    private var page = 1
 
    private val list = arrayListOf<Practice>()
    private val adapter = PracticeAdapter(list)
 
    private val typeList = arrayListOf(CommonData(name = "全部"))
    private val filters = arrayListOf("全部") //筛选选项
    private val filterAdapter = FilterAdapter(filters)
 
    private val stuId by lazy { intent.getStringExtra("stuId")?:"" }
 
    override fun initClick() {
        tv_search.clickDelay {
            val s = et_search.text.toString()
            search = if (s.isNullOrEmpty()) null else s
            refreshLayout.autoRefresh()
        }
 
        adapter.setOnItemClickListener { view, position ->
            startActivity<VideoDetailActivity>("id" to list[position].videoId,"courseId" to list[position].coursePackageId,
                "scId" to list[position].scId,"type" to 3)
        }
 
        cb_type.setOnCheckedChangeListener { buttonView, isChecked ->
            if (!isChecked)
                closeFilter()
            else{
                bg_filter.visible()
                rv_condition.visible()
                val loadAnimation = AnimationUtils.loadAnimation(this, R.anim.popup_in_from_top)
                rv_condition.startAnimation(loadAnimation)
            }
        }
 
        bg_filter.setOnClickListener {
            cb_type.isChecked = false
        }
 
        filterAdapter.setOnItemClickListener { view, position ->
            typeId = if (position == 0) null else typeList[position].id
            cb_type.text = if (position == 0) "布置运动营" else filters[position]
            filterAdapter.checkedIndex = position
            filterAdapter.notifyDataSetChanged()
            cb_type.isChecked = false
            refreshLayout.autoRefresh()
        }
    }
 
    override fun initView() {
        rv_condition.layoutManager = LinearLayoutManager(this)
        rv_condition.adapter = filterAdapter
        rv_video.layoutManager = LinearLayoutManager(this)
        rv_video.adapter = adapter
        refreshLayout.setEnableLoadMore(false)
        refreshLayout.setOnRefreshListener {
            page = 1
            getData()
        }
        refreshLayout.setOnLoadMoreListener {
            page ++
            getData()
        }
        getData()
        getTypes()
    }
 
    private fun getData(){
        HttpManager.afterSourceList(stuId,typeId,search).request(this,success = {_,data->
            if (page == 1)
                list.clear()
            list.addAll(data?: arrayListOf())
            adapter.notifyDataSetChanged()
            if (list.isEmpty())
                refreshLayout.finishRefreshWithNoMoreData()
            else if (data.isNullOrEmpty())
                refreshLayout.finishLoadMoreWithNoMoreData()
            else if (page == 1)
                refreshLayout.finishRefresh()
            else
                refreshLayout.finishLoadMore()
        }){_,_->
            if (page == 1)
                refreshLayout.finishRefresh(false)
            else
                refreshLayout.finishLoadMore(false)
            page--
        }
    }
 
    private fun getTypes(){
        HttpManager.queryArrangeCourseList().request(this){_,data->
            typeList.addAll(data?: arrayListOf())
            filters.addAll((data?: arrayListOf()).map { it.name })
            filterAdapter.notifyDataSetChanged()
        }
    }
 
    private fun closeFilter(){
        bg_filter.gone()
        rv_condition.gone()
    }
}