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.DollearnApplication
|
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.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 = DollearnApplication.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,
|
DollearnApplication.lat,
|
DollearnApplication.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(DollearnApplication.lat, DollearnApplication.lon)
|
.request(this) { _, data ->
|
storeList.addAll(data ?: arrayListOf())
|
}
|
}
|
|
private fun closeFilter() {
|
bg_filter.gone()
|
rv_filter.gone()
|
}
|
}
|