罗明文
2024-06-16 9673bcd57c6100ad9fdfbee728ef078104511fc1
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
package com.dollearn.student.ui.home
 
import android.Manifest
import android.view.animation.AnimationUtils
import androidx.recyclerview.widget.LinearLayoutManager
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.SpanBuilder
import cn.sinata.xldutils.utils.myToast
import cn.sinata.xldutils.visible
import com.amap.api.location.AMapLocationListener
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.Match
import com.dollearn.student.network.request
import com.dollearn.student.ui.TransparentStatusBarActivity
import com.dollearn.student.ui.home.adapter.FilterAdapter
import com.dollearn.student.ui.home.adapter.MatchAdapter
import com.dollearn.student.ui.home.adapter.OpenCityAdapter
import com.dollearn.student.utils.AMapKit
import com.dollearn.student.utils.extention.clickDelay
import com.dollearn.student.utils.extention.getInitial
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.android.synthetic.main.activity_match.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.textColorResource
 
class MatchActivity : TransparentStatusBarActivity() {
    override fun setContentView() = R.layout.activity_match
 
    private val datas = arrayListOf<Match>()
    private val adapter = MatchAdapter(datas)
 
    private val index = arrayListOf(
        "A", "B", "C", "D", "E", "F", "G", "H", "I",
        "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
    )
    //筛选条件
    private val cityList = arrayListOf<CommonData>() //展示数据源(搜索后过滤)
    private val cityAdapter = OpenCityAdapter(cityList)
    var cityCode:String? = null //城市
    var condition:Int? = null //1=全部用户,2=仅限年度会员参与,3=仅限运动营成员参与
    var heatSort:String? = null //销量排序(asc=正序,desc=降序)
    var search:String? = null //搜索
 
 
    private val filters = arrayListOf("全部用户参与","仅限年度会员参与","仅限运动营成员参与") //筛选选项
    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 ->
            filterAdapter.checkedIndex = position
            filterAdapter.notifyDataSetChanged()
            cb_condition.isChecked = false
            condition = position+1
            cb_condition.text = if (position == 0) "全部用户" else if (position == 1) "仅限会员" else "仅限运动营成员"
            refreshLayout.autoRefresh()
        }
        cityAdapter.setOnItemClickListener { view, position ->
            cb_city.isChecked = false
            cityCode = cityList[position].code
            cb_city.text = cityList[position].name
            refreshLayout.autoRefresh()
        }
        tv_current.setOnClickListener {
            if (WeparkApplication.cityCode.isNotEmpty()){
                cb_city.isChecked = false
                cityCode = WeparkApplication.cityCode
                cb_city.text = WeparkApplication.cityName
                refreshLayout.autoRefresh()
            }
        }
        tv_refresh.setOnClickListener {
            val subscribe =
                RxPermissions(this).request(Manifest.permission.ACCESS_FINE_LOCATION)
                    .subscribe {
                        if (it){
                            AMapKit.initLocation(this, AMapLocationListener {
                                WeparkApplication.lat = it.latitude
                                WeparkApplication.lon = it.longitude
                                WeparkApplication.province = it.province
                                WeparkApplication.provinceCode = "${it.adCode.substring(0,3)}000"
                                WeparkApplication.cityName = it.city
                                WeparkApplication.cityCode = "${it.adCode.substring(0,4)}00"
                                tv_current.text = it.city
                            })
                        }else
                            myToast("没有定位权限")
                    }
        }
        adapter.setOnItemClickListener { _, position ->
            startActivity<MatchDetailActivity>("id" to datas[position].id)
        }
        cb_city.setOnCheckedChangeListener { _, isChecked ->
            if (!isChecked)
                closeFilter()
            else{
                if (cb_condition.isChecked)
                    cb_condition.isChecked = false
                bg_filter.visible()
                cl_city.visible()
                val loadAnimation = AnimationUtils.loadAnimation(this, R.anim.popup_in_from_top)
                cl_city.startAnimation(loadAnimation)
            }
        }
        cb_condition.setOnCheckedChangeListener { _, isChecked ->
            if (!isChecked)
                closeFilter()
            else{
                if (cb_city.isChecked)
                    cb_city.isChecked = false
                bg_filter.visible()
                rv_condition.visible()
                val loadAnimation = AnimationUtils.loadAnimation(this, R.anim.popup_in_from_top)
                rv_condition.startAnimation(loadAnimation)
            }
        }
        tv_heat.clickDelay {
            if (cb_city.isChecked)
                cb_city.isChecked = false
            if (cb_condition.isChecked)
                cb_condition.isChecked = false
            tv_heat.textColorResource = R.color.colorPrimary
            if (heatSort == null||heatSort == "asc")
                heatSort = "desc"
            else
                heatSort = "asc"
            tv_heat.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,if (heatSort == "asc")R.mipmap.sort_asc else R.mipmap.sort_desc,0)
            refreshLayout.autoRefresh()
        }
        bg_filter.setOnClickListener {
            if (cb_condition.isChecked)
                cb_condition.isChecked = false
            else if (cb_city.isChecked)
                cb_city.isChecked = false
        }
        tv_record.setOnClickListener {
            startActivity<MyMatchActivity>()
        }
    }
 
    override fun initView() {
        if (WeparkApplication.cityName.isNotEmpty()){
            cb_city.text = WeparkApplication.cityName
            cityCode = WeparkApplication.cityCode
        }
        rv_course.layoutManager = LinearLayoutManager(this)
        rv_course.adapter = adapter
        refreshLayout.setOnRefreshListener {
            getData()
        }
        getData()
        //筛选
        rv_city.layoutManager = LinearLayoutManager(this)
        rv_city.adapter = cityAdapter
        side_bar.setOnSelectIndexItemListener { index ->
            (0 until cityList.size).forEach {
                if (index == cityList[it].name?.getInitial()) {
                    val manager = rv_city.layoutManager as LinearLayoutManager
                    manager.scrollToPositionWithOffset(it, 0)
                    return@setOnSelectIndexItemListener
                }
            }
        }
        rv_condition.layoutManager = LinearLayoutManager(this)
        rv_condition.adapter = filterAdapter
        filterAdapter.checkedIndex = -1
        tv_current.text = SpanBuilder("当前城市:${WeparkApplication.cityName}").color(this,0,5,R.color.textColor).build()
        //获取城市筛选项数据
        getCity()
    }
 
    private fun getData(){
        HttpManager.queryCompetitionList(cityCode,condition,heatSort, search).request(this,success = {_,data->
            refreshLayout.finishRefresh()
            datas.clear()
            datas.addAll(data?: arrayListOf())
            adapter.notifyDataSetChanged()
        }){_,_->
            refreshLayout.finishRefresh(false)
        }
    }
 
    private fun getCity(){
        HttpManager.queryAllCity().request(this){_,data->
            data?.let {
                cityList.clear()
                index.forEach { index->
                    it.filter { it.name.isNotEmpty() }.forEach {
                        if (index == it.name.getInitial()){
                            cityList.add(it)
                        }
                    }
                }
                adapter.notifyDataSetChanged()
            }
        }
    }
 
 
    private fun closeFilter(){
        bg_filter.gone()
        rv_condition.gone()
        cl_city.gone()
    }
}