| | |
| | | package com.dollearn.student.ui.home |
| | | |
| | | import android.Manifest |
| | | import android.view.animation.AnimationUtils |
| | | import android.annotation.SuppressLint |
| | | import android.os.Handler |
| | | import android.os.Looper |
| | | import android.os.Message |
| | | import android.view.ViewGroup |
| | | import androidx.core.os.bundleOf |
| | | import androidx.fragment.app.Fragment |
| | | import androidx.fragment.app.FragmentPagerAdapter |
| | | import androidx.recyclerview.widget.LinearLayoutManager |
| | | import androidx.viewpager.widget.ViewPager |
| | | 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.dialog.TipDialog |
| | | 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.entity.SortBean |
| | | import com.dollearn.student.network.entity.SubjectBean |
| | | 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 |
| | | import com.dollearn.student.ui.home.adapter.IndexAdapter |
| | | import com.dollearn.student.utils.Const |
| | | import com.dollearn.student.utils.event.EmptyEvent |
| | | import kotlinx.android.synthetic.main.activity_listen.* |
| | | import org.greenrobot.eventbus.EventBus |
| | | |
| | | class MatchActivity : TransparentStatusBarActivity() { |
| | | override fun setContentView() = R.layout.activity_match |
| | | class MatchActivity:TransparentStatusBarActivity() { |
| | | override fun setContentView() = R.layout.activity_listen |
| | | |
| | | private val datas = arrayListOf<Match>() |
| | | private val adapter = MatchAdapter(datas) |
| | | private val day by lazy { |
| | | intent.getIntExtra("day",0) |
| | | } |
| | | private val week by lazy { |
| | | intent.getIntExtra("week",0) |
| | | } |
| | | private val season by lazy { |
| | | intent.getIntExtra("season",0) |
| | | } |
| | | val data by lazy { intent.getParcelableExtra<SubjectBean>("data") } |
| | | val fragments = arrayListOf<Fragment>() |
| | | |
| | | 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 //搜索 |
| | | var totalCount = 0 //总答题次数 |
| | | var rightCount = 0 //正确答题次数 |
| | | var time = 0 //学习秒数 |
| | | private var handler:Handler? = null |
| | | |
| | | val indexList = arrayListOf<SortBean>() |
| | | private val indexAdapter = IndexAdapter(indexList) |
| | | |
| | | |
| | | private val filters = arrayListOf("全部用户参与","仅限年度会员参与","仅限运动营成员参与") //筛选选项 |
| | | private val filterAdapter = FilterAdapter(filters) |
| | | val vp by lazy { view_pager } |
| | | var recoverd = false //true已经恢复了进度,后续语音可以自动播放 |
| | | |
| | | 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("没有定位权限") |
| | | tv_last.setOnClickListener { |
| | | val tipDialog = TipDialog() |
| | | tipDialog.arguments = bundleOf("msg" to "是否重新开始答题?确认后将清空当前答题进度") |
| | | tipDialog.setCallback(object :TipDialog.OnClickCallback{ |
| | | override fun onOk() { |
| | | showDialog() |
| | | HttpManager.restart(day,week,5).request(this@MatchActivity){_,_-> |
| | | totalCount = 0 //总答题次数 |
| | | rightCount = 0 //正确答题次数 |
| | | time = 0 //学习秒数 |
| | | indexList.forEach { it.status = 1 } |
| | | indexAdapter.notifyDataSetChanged() |
| | | data?.accuracy = 0.0 |
| | | view_pager.setCurrentItem(0,false) |
| | | (fragments[0] as MatchFragment).recover() |
| | | } |
| | | } |
| | | |
| | | override fun onCancel() { |
| | | } |
| | | }) |
| | | tipDialog.show(supportFragmentManager,"restart") |
| | | } |
| | | adapter.setOnItemClickListener { _, position -> |
| | | startActivity<MatchDetailActivity>("id" to datas[position].id) |
| | | |
| | | tv_exit.setOnClickListener { |
| | | onBackPressed() |
| | | } |
| | | 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) |
| | | |
| | | indexAdapter.setOnItemClickListener { view, position -> |
| | | val sortBean = indexList[position] |
| | | if (position!=view_pager.currentItem&&(sortBean.status!=1||position == indexList.indexOf(indexList.first { it.status == 1 }))){ |
| | | view_pager.setCurrentItem(position,false) |
| | | (fragments[position] as MatchFragment).recover() |
| | | } |
| | | } |
| | | 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) |
| | | } |
| | | } |
| | | |
| | | fun next(){ |
| | | val listenFragment = fragments[view_pager.currentItem] as MatchFragment |
| | | val sortBean = indexList[view_pager.currentItem] |
| | | sortBean.status = if (listenFragment.right) 2 else 3 //修改本地答题结果,并上传服务器 |
| | | HttpManager.answerQuestion(sortBean).request(this){_,data-> |
| | | //保存成功 |
| | | } |
| | | 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>() |
| | | val filter = indexList.filter { it.status != 1 } |
| | | data?.accuracy = filter.filter { it.status == 2 }.size.toDouble() / filter.size |
| | | if (view_pager.currentItem == fragments.lastIndex){ |
| | | handler?.removeMessages(0) |
| | | ResultActivity.startResult(this,day,week,season,5,totalCount,rightCount,data!!.data.integral,time,data!!.data.id) |
| | | finish() |
| | | }else{ |
| | | view_pager.setCurrentItem(view_pager.currentItem+1,true) |
| | | (fragments[view_pager.currentItem+1] as MatchFragment).recover() |
| | | tv_last.visible() |
| | | indexAdapter.notifyDataSetChanged() |
| | | } |
| | | } |
| | | |
| | | override fun initView() { |
| | | if (WeparkApplication.cityName.isNotEmpty()){ |
| | | cb_city.text = WeparkApplication.cityName |
| | | cityCode = WeparkApplication.cityCode |
| | | rv_index.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false) |
| | | rv_index.adapter = indexAdapter |
| | | data?.apply { |
| | | tv_progress.text = "已完成:1/%d\n正确率:%.2f%s".format(subjectList.size,accuracy*100,"%") |
| | | indexList.clear() |
| | | indexList.addAll(list) |
| | | indexAdapter.notifyDataSetChanged() |
| | | subjectList.forEachIndexed { index, subject -> |
| | | fragments.add(MatchFragment.getInstance(index)) |
| | | } |
| | | view_pager.adapter = @SuppressLint("WrongConstant") |
| | | object : FragmentPagerAdapter(supportFragmentManager,0) { |
| | | override fun getItem(p0: Int): Fragment { |
| | | return fragments[p0] |
| | | } |
| | | |
| | | override fun getCount(): Int { |
| | | return fragments.size |
| | | } |
| | | |
| | | override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) { |
| | | } |
| | | } |
| | | view_pager.offscreenPageLimit = fragments.size |
| | | view_pager.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{ |
| | | override fun onPageScrolled( |
| | | position: Int, |
| | | positionOffset: Float, |
| | | positionOffsetPixels: Int |
| | | ) { |
| | | |
| | | } |
| | | |
| | | override fun onPageSelected(position: Int) { |
| | | tv_progress.text = "已完成:%d/%d\n正确率:%.2f%s".format(position+1,subjectList.size,accuracy*100,"%") |
| | | } |
| | | |
| | | override fun onPageScrollStateChanged(state: Int) { |
| | | } |
| | | }) |
| | | } |
| | | 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 |
| | | recover() |
| | | handler= object :Handler(Looper.myLooper()!!){ |
| | | override fun handleMessage(msg: Message) { |
| | | super.handleMessage(msg) |
| | | if (msg.what == 0){ |
| | | time++ |
| | | handler?.sendEmptyMessageDelayed(0,1000L) |
| | | } |
| | | } |
| | | } |
| | | 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() |
| | | handler?.sendEmptyMessageDelayed(0,1000L) |
| | | } |
| | | |
| | | 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 recover(){ |
| | | HttpManager.teamSchedule(day,week,5).request(this){_,data-> |
| | | view_pager.setCurrentItem((data?.schedule?:1)-1,false) |
| | | if (view_pager.currentItem != 0) |
| | | tv_last.visible() |
| | | totalCount = data?.answerNumber?:0 |
| | | rightCount = data?.correctNumber?:0 |
| | | |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.RECOVERD)) |
| | | recoverd = true |
| | | } |
| | | } |
| | | |
| | | 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) |
| | | } |
| | | } |
| | | override fun onBackPressed() { |
| | | val tipDialog = TipDialog() |
| | | tipDialog.arguments = bundleOf("msg" to "未完成全部答题,确认退出吗?") |
| | | tipDialog.setCallback(object :TipDialog.OnClickCallback{ |
| | | override fun onOk() { |
| | | val list = (data?.subjectList?: arrayListOf()).flatMap { it }.subList(0,view_pager.currentItem) |
| | | HttpManager.exitLearning(view_pager.currentItem,season,time,totalCount,rightCount,day,week,5,data?.data?.id?:"", list.joinToString(",") { it.id }).request(this@MatchActivity){ _, _-> |
| | | finish() |
| | | } |
| | | adapter.notifyDataSetChanged() |
| | | } |
| | | } |
| | | |
| | | override fun onCancel() { |
| | | } |
| | | }) |
| | | tipDialog.show(supportFragmentManager,"exit") |
| | | } |
| | | |
| | | |
| | | private fun closeFilter(){ |
| | | bg_filter.gone() |
| | | rv_condition.gone() |
| | | cl_city.gone() |
| | | override fun onDestroy() { |
| | | super.onDestroy() |
| | | handler?.removeMessages(0) |
| | | handler = null |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.STOP_TIMER)) |
| | | } |
| | | } |