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()
|
}
|
}
|