lmw
2024-06-16 03172fc2d9a7717f4a9d8de1c5eca3158a550b30
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package com.dollearn.student.ui.home
 
import android.Manifest
import android.view.animation.AnimationUtils
import androidx.core.os.bundleOf
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.utils.showAllowingStateLoss
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.dialog.ChooseTimeDialog
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.CommonData
import com.dollearn.student.network.entity.Place
import com.dollearn.student.network.request
import com.dollearn.student.ui.TransparentStatusBarActivity
import com.dollearn.student.ui.home.adapter.FieldAdapter
import com.dollearn.student.ui.home.adapter.FilterAdapter
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.dollearn.student.utils.interfaces.StringCallback
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.android.synthetic.main.activity_field.*
import org.jetbrains.anko.startActivity
 
class FieldActivity : TransparentStatusBarActivity() {
    override fun setContentView() = R.layout.activity_field
 
    private val datas = arrayListOf<Place>()
    private val adapter = FieldAdapter(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 var page = 1
    var cityCode:String? = null //城市
    var shop :String? = null //店铺id
    var siteTypeId:Int? = null //1=全部用户,2=仅限年度会员参与,3=仅限运动营成员参与
    var startTime:String? = null //开始时间
    var endTime:String? = null //结束时间
    var search:String? = null //搜索
 
    private val typeList = arrayListOf(CommonData(name = "全部"))
 
    private val storeList = arrayListOf<CommonData>()
 
    private val cityList = arrayListOf<CommonData>()
    private val cityAdapter = OpenCityAdapter(cityList)
 
    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
                siteTypeId = if (position == 0) null else typeList[position].id
                cb_type.text = if (position == 0) "场地类型" else typeList[position].name
            }else{
                cb_shop.isChecked = false
                shop = if (position == 0) null else storeList[position].id.toString()
            }
            refreshLayout.autoRefresh()
        }
        cityAdapter.setOnItemClickListener { view, position ->
            cb_city.isChecked = false
            cityCode = cityList[position].code
            cb_city.text = cityList[position].name
            shop = null
            refreshLayout.autoRefresh()
            getShop()
        }
        tv_current.setOnClickListener {
            if (WeparkApplication.cityCode.isNotEmpty()){
                cb_city.isChecked = false
                cityCode = WeparkApplication.cityCode
                cb_city.text = WeparkApplication.cityName
                shop = null
                refreshLayout.autoRefresh()
                getShop()
            }
        }
        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<PlaceDetailActivity>("id" to datas[position].id)
        }
        cb_city.setOnCheckedChangeListener { _, isChecked ->
            if (!isChecked)
                closeFilter()
            else{
                if (cb_time.isChecked)
                    cb_time.isChecked = false
                else if (cb_shop.isChecked)
                    cb_shop.isChecked = false
                else if (cb_type.isChecked)
                    cb_type.isChecked = false
                bg_filter.visible()
                cl_city.visible()
                val loadAnimation = AnimationUtils.loadAnimation(this, R.anim.popup_in_from_top)
                cl_city.startAnimation(loadAnimation)
            }
        }
        cb_time.setOnCheckedChangeListener { buttonView, isChecked ->
            if (!isChecked)
                closeFilter()
            else{
                if (cb_city.isChecked)
                    cb_city.isChecked = false
                else if (cb_shop.isChecked)
                    cb_shop.isChecked = false
                else if (cb_type.isChecked)
                    cb_type.isChecked = false
                bg_filter.visible()
                cl_time.visible()
                val loadAnimation = AnimationUtils.loadAnimation(this, R.anim.popup_in_from_top)
                cl_time.startAnimation(loadAnimation)
            }
        }
 
        tv_time_start.clickDelay {
            val chooseTimeDialog = ChooseTimeDialog()
            chooseTimeDialog.callback = object :StringCallback{
                override fun onResult(rst: String) {
                    tv_time_start.text = rst
                }
            }
            chooseTimeDialog.showAllowingStateLoss(supportFragmentManager,"start")
        }
 
        tv_time_end.clickDelay {
            val start = tv_time_start.text.toString()
            if (start.isNullOrEmpty())
                myToast("请选择开始时间")
            else{
                val chooseTimeDialog = ChooseTimeDialog()
                chooseTimeDialog.arguments = bundleOf("startTime" to start)
                chooseTimeDialog.callback = object :StringCallback{
                    override fun onResult(rst: String) {
                        tv_time_end.text = rst
                        startTime = start
                        endTime = rst
                        cb_time.isChecked = false
                        refreshLayout.autoRefresh()
                    }
                }
                chooseTimeDialog.showAllowingStateLoss(supportFragmentManager,"end")
            }
        }
 
        cb_type.setOnCheckedChangeListener { buttonView, isChecked ->
            if (!isChecked)
                closeFilter()
            else{
                if (cb_city.isChecked)
                    cb_city.isChecked = false
                else if (cb_shop.isChecked)
                    cb_shop.isChecked = false
                else if (cb_time.isChecked)
                    cb_time.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(siteTypeId?:0)
                filterAdapter.notifyDataSetChanged()
            }
        }
 
        cb_shop.setOnCheckedChangeListener { _, isChecked ->
            if (!isChecked)
                closeFilter()
            else{
                if (cb_city.isChecked)
                    cb_city.isChecked = false
                else if (cb_type.isChecked)
                    cb_type.isChecked = false
                else if (cb_time.isChecked)
                    cb_time.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?:"0").toInt())
                filterAdapter.notifyDataSetChanged()
            }
        }
 
        bg_filter.setOnClickListener {
            if (cb_time.isChecked)
                cb_time.isChecked = false
            else if (cb_city.isChecked)
                cb_city.isChecked = false
            else if (cb_shop.isChecked)
                cb_shop.isChecked = false
            else if (cb_type.isChecked)
                cb_type.isChecked = false
        }
        tv_record.setOnClickListener {
            startActivity<MyPlaceActivity>()
        }
    }
 
    override fun initView() {
        shop = WeparkApplication.storeId
        rv_course.layoutManager = LinearLayoutManager(this)
        rv_course.adapter = adapter
        refreshLayout.setOnRefreshListener {
            page = 1
            getData()
        }
        refreshLayout.setOnLoadMoreListener {
            page ++
            getData()
        }
        getData()
        //筛选
        rv_city.layoutManager = LinearLayoutManager(this)
        rv_city.adapter = cityAdapter
        side_bar.setOnSelectIndexItemListener { index ->
            (0 until datas.size).forEach {
                if (index == datas[it].name.getInitial()) {
                    val manager = rv_city.layoutManager as LinearLayoutManager
                    manager.scrollToPositionWithOffset(it, 0)
                    return@setOnSelectIndexItemListener
                }
            }
        }
        rv_filter.layoutManager = LinearLayoutManager(this)
        rv_filter.adapter = filterAdapter
        tv_current.text = SpanBuilder("当前城市:${WeparkApplication.cityName}").color(this,0,5,R.color.textColor).build()
        //筛选项数据
        getCity()
        getTypes()
        getShop()
    }
 
    private fun getData(){
        HttpManager.querySiteList(page,cityCode,startTime,endTime,siteTypeId,shop,search).request(this,success = {_,data->
            if (page == 1)
                datas.clear()
            datas.addAll(data?: arrayListOf())
            adapter.notifyDataSetChanged()
            if (datas.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 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 getTypes(){
        HttpManager.querySiteType().request(this){_,data->
            typeList.addAll(data?: arrayListOf())
        }
    }
 
    private fun getShop(){
        HttpManager.queryStoreLists(WeparkApplication.lat,WeparkApplication.lon,cityCode).request(this){_,data->
            storeList.clear()
            storeList.add(CommonData(name = "全部"))
            storeList.addAll(data?: arrayListOf())
        }
    }
 
    private fun closeFilter(){
        bg_filter.gone()
        cl_time.gone()
        rv_filter.gone()
        cl_city.gone()
    }
}