lmw
2024-06-17 f571288a24fcf10143dcc8015ffbbf38dbc0c614
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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.WeparkApplication
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.CommonData
import com.dollearn.student.network.entity.Course
import com.dollearn.student.network.request
import com.dollearn.student.ui.TransparentStatusBarActivity
import com.dollearn.student.ui.home.adapter.CourseAdapter
import com.dollearn.student.ui.home.adapter.FilterAdapter
import com.dollearn.student.utils.Const
import com.dollearn.student.utils.event.EmptyEvent
import com.dollearn.student.utils.extention.clickDelay
import kotlinx.android.synthetic.main.activity_course.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.textColorResource
 
class CourseActivity : TransparentStatusBarActivity() {
    override fun setContentView() = R.layout.activity_course
 
    private val datas = arrayListOf<Course>()
    private val adapter = CourseAdapter(datas)
 
    //筛选条件
    var shop = WeparkApplication.storeId //店铺id
    var type: Int? = null //运动营类型id
    var distanceSort: String? = null //距离排序(asc=正序,desc=降序)
    var salesSort: String? = null //销量排序(asc=正序,desc=降序)
    var search: String? = null //搜索
 
    private val typeList = arrayListOf(CommonData(name = "全部"))
    private val storeList = arrayListOf<CommonData>()
 
    private val filters = arrayListOf<String>() //筛选选项
    private val filterAdapter = FilterAdapter(filters)
 
    override fun initClick() {
        tv_search.clickDelay {
            val s = et_search.text.toString()
            search = if (s.isNullOrEmpty()) null else s
            refreshLayout.autoRefresh()
        }
        filterAdapter.setOnItemClickListener { view, position ->
            if (cb_type.isChecked) {
                cb_type.isChecked = false
                type = if (position == 0) null else typeList[position].id
                cb_type.text = if (position == 0) "运动营类型" else typeList[position].name
            } else {
                cb_shop.isChecked = false
                shop = storeList[position].id.toString()
            }
            refreshLayout.autoRefresh()
        }
        adapter.setOnItemClickListener { _, position ->
            startActivity<CourseDetailActivity>("id" to datas[position].id)
        }
        cb_type.setOnCheckedChangeListener { _, isChecked ->
            if (!isChecked)
                closeFilter()
            else {
                if (cb_shop.isChecked)
                    cb_shop.isChecked = false
                bg_filter.visible()
                rv_filter.visible()
                val loadAnimation = AnimationUtils.loadAnimation(this, R.anim.popup_in_from_top)
                rv_filter.startAnimation(loadAnimation)
                filters.clear()
                filters.addAll(typeList.map { it.name })
                filterAdapter.checkedIndex = typeList.map { it.id }.indexOf(type ?: 0)
                filterAdapter.notifyDataSetChanged()
            }
        }
        cb_shop.setOnCheckedChangeListener { _, isChecked ->
            if (!isChecked)
                closeFilter()
            else {
                if (cb_type.isChecked)
                    cb_type.isChecked = false
                bg_filter.visible()
                rv_filter.visible()
                val loadAnimation = AnimationUtils.loadAnimation(this, R.anim.popup_in_from_top)
                rv_filter.startAnimation(loadAnimation)
                filters.clear()
                filters.addAll(storeList.map { it.name })
                filterAdapter.checkedIndex = storeList.map { it.id }.indexOf(shop.toInt())
                filterAdapter.notifyDataSetChanged()
            }
        }
        tv_sale.clickDelay {
            if (cb_type.isChecked)
                cb_type.isChecked = false
            if (cb_shop.isChecked)
                cb_shop.isChecked = false
            distanceSort = null
            tv_distance.textColorResource = R.color.textColor
            tv_distance.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.mipmap.sort_none, 0)
            tv_sale.textColorResource = R.color.colorPrimary
            if (salesSort == null || salesSort == "asc")
                salesSort = "desc"
            else
                salesSort = "asc"
            tv_sale.setCompoundDrawablesRelativeWithIntrinsicBounds(
                0,
                0,
                if (salesSort == "asc") R.mipmap.sort_asc else R.mipmap.sort_desc,
                0
            )
            refreshLayout.autoRefresh()
        }
        tv_distance.clickDelay {
            if (cb_type.isChecked)
                cb_type.isChecked = false
            if (cb_shop.isChecked)
                cb_shop.isChecked = false
            salesSort = null
            tv_sale.textColorResource = R.color.textColor
            tv_sale.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.mipmap.sort_none, 0)
            tv_distance.textColorResource = R.color.colorPrimary
            if (distanceSort == null || distanceSort == "desc")
                distanceSort = "asc"
            else
                distanceSort = "desc"
            tv_distance.setCompoundDrawablesRelativeWithIntrinsicBounds(
                0,
                0,
                if (distanceSort == "asc") R.mipmap.sort_asc else R.mipmap.sort_desc,
                0
            )
            refreshLayout.autoRefresh()
        }
        bg_filter.setOnClickListener {
            if (cb_shop.isChecked)
                cb_shop.isChecked = false
            else if (cb_type.isChecked)
                cb_type.isChecked = false
        }
 
 
        mtvybmcourse.clickDelay {
            startActivity<MyCourseActivity>()
        }
    }
 
    override fun initView() {
        rv_course.layoutManager = LinearLayoutManager(this)
        rv_course.adapter = adapter
        refreshLayout.setOnRefreshListener {
            getData()
        }
        getData()
        //筛选
        rv_filter.layoutManager = LinearLayoutManager(this)
        rv_filter.adapter = filterAdapter
        //获取筛选项数据
        getType()
        getShop()
        EventBus.getDefault().register(this)
    }
 
    @Subscribe
    fun onSwitch(e: EmptyEvent) {
 
    }
 
    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
 
    private fun getData() {
        HttpManager.queryCourseList(
            shop,
            WeparkApplication.lat,
            WeparkApplication.lon,
            type,
            distanceSort,
            salesSort,
            search
        ).request(this, success = { _, data ->
            refreshLayout.finishRefresh()
            datas.clear()
            datas.addAll(data ?: arrayListOf())
            adapter.notifyDataSetChanged()
        }) { _, _ ->
            refreshLayout.finishRefresh(false)
        }
    }
 
    private fun getType() {
        HttpManager.queryCoursePackageType().request(this) { _, data ->
            typeList.addAll(data ?: arrayListOf())
        }
    }
 
    private fun getShop() {
        HttpManager.queryStoreLists(WeparkApplication.lat, WeparkApplication.lon)
            .request(this) { _, data ->
                storeList.addAll(data ?: arrayListOf())
            }
    }
 
    private fun closeFilter() {
        bg_filter.gone()
        rv_filter.gone()
    }
}