package com.dollearn.student.ui.course
|
|
import android.annotation.SuppressLint
|
import android.app.Activity
|
import android.content.Intent
|
import android.util.Log
|
import android.view.View
|
import androidx.core.os.bundleOf
|
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.viewpager.widget.ViewPager
|
import cn.sinata.xldutils.fragment.BaseFragment
|
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.utils.toTime
|
import cn.sinata.xldutils.visible
|
import com.dollearn.student.R
|
import com.dollearn.student.WeparkApplication
|
import com.dollearn.student.WeparkApplication.Companion.storeId
|
import com.dollearn.student.dialog.MapTypeDialog
|
import com.dollearn.student.dialog.TipDialog
|
import com.dollearn.student.dialog.UndistributedClassHourialog
|
import com.dollearn.student.network.Apis
|
import com.dollearn.student.network.HttpManager
|
import com.dollearn.student.network.entity.*
|
import com.dollearn.student.network.requestByF
|
import com.dollearn.student.ui.course.adapter.*
|
import com.dollearn.student.ui.home.AddStudentActivity
|
import com.dollearn.student.ui.home.VideoDetailActivity
|
import com.dollearn.student.utils.Const
|
import com.dollearn.student.utils.Const.EventCode.CHANGE_COURSDATA
|
import com.dollearn.student.utils.ThirdPartyMapsGuide
|
import com.dollearn.student.utils.event.EmptyEvent
|
import com.dollearn.student.utils.extention.clickDelay
|
import com.dollearn.student.utils.extention.loadLongImage
|
import com.dollearn.student.utils.formatDistance
|
import com.dollearn.student.utils.interfaces.StringCallback
|
import com.dollearn.student.utils.interfaces.StringCallback2
|
import kotlinx.android.synthetic.main.fragment_course.*
|
import org.greenrobot.eventbus.EventBus
|
import org.greenrobot.eventbus.Subscribe
|
import org.jetbrains.anko.support.v4.startActivity
|
import org.jetbrains.anko.support.v4.startActivityForResult
|
import org.jetbrains.anko.support.v4.toast
|
import java.lang.Exception
|
import java.util.*
|
|
class CourseFragment : BaseFragment() {
|
override fun contentViewId() = R.layout.fragment_course
|
|
private var currentStudent: String? = null //当前运动营成员
|
private val course = arrayListOf<MyCourse>()
|
private val myCourseAdapter = MyCourseAdapter(course)
|
private val medalList = arrayListOf<Medal>()
|
private val medalAdapter = MedalGridAdapter(medalList)
|
private val exerciseList = arrayListOf<Exercise>()
|
private val exerciseAdapter = ExerciseGridAdapter(exerciseList)
|
private val frags = arrayListOf<WeekFragment>()
|
private val scheduleList = arrayListOf<Data>()
|
lateinit var dialogc: UndistributedClassHourialog
|
override fun onFirstVisibleToUser() {
|
rv_course.layoutManager =
|
LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
|
rv_course.adapter = myCourseAdapter
|
rv_badge.layoutManager = GridLayoutManager(requireContext(), 3)
|
rv_badge.adapter = medalAdapter
|
rv_practice.layoutManager = GridLayoutManager(requireContext(), 2)
|
rv_practice.adapter = exerciseAdapter
|
rv_course_schedule.layoutManager = LinearLayoutManager(requireContext())
|
|
initWeeks()
|
|
tv_switch.setOnClickListener {
|
startActivityForResult<SwitchStudentActivity>(2, "currentStu" to currentStudent)
|
}
|
tv_record.setOnClickListener {
|
startActivity<ReserveRecordActivity>(
|
"id" to currentStudent,
|
"name" to tv_name.text.toString()
|
)
|
}
|
tv_more_report.setOnClickListener {
|
startActivity<ReportActivity>("id" to currentStudent)
|
}
|
|
//详情
|
tv_detail.setOnClickListener {
|
startActivity<MyCourseDetailActivity>(
|
"courseId" to course[myCourseAdapter.checked].courseId,
|
"stuId" to currentStudent
|
)
|
}
|
|
//续课
|
tv_continue.setOnClickListener {
|
showDialog()
|
HttpManager.queryCourseInfo(course[myCourseAdapter.checked].courseId, WeparkApplication.lat, WeparkApplication.lon)
|
.requestByF(this) { _, data ->
|
startActivityForResult<JoinCourseActivity>(
|
1,
|
"course" to data,
|
"isContinue" to true
|
)
|
}
|
}
|
tv_continue_1.setOnClickListener {
|
tv_continue.callOnClick()
|
}
|
|
|
tv_evaluation.setOnClickListener {
|
startActivity<EvaluationActivity>("stuId" to currentStudent)
|
}
|
|
myCourseAdapter.setOnItemClickListener { view, position ->
|
myCourseAdapter.checked = position
|
cl_course_info.visibility = if (course[position].courseType != 1) View.GONE else View.VISIBLE
|
myCourseAdapter.notifyDataSetChanged()
|
refreshCourseInfo()
|
}
|
medalAdapter.setOnItemClickListener { view, position ->
|
startActivity<MedalActivity>("stuId" to currentStudent)
|
}
|
exerciseAdapter.setOnItemClickListener { view, position ->
|
startActivity<VideoDetailActivity>(
|
"id" to exerciseList[position].videoId,
|
"scId" to exerciseList[position].scId,
|
"courseId" to exerciseList[position].coursePackageId,
|
"type" to 3
|
)
|
}
|
|
tv_reward.setOnClickListener {
|
startActivity<PracticeListActivity>("stuId" to currentStudent)
|
}
|
ll_add.setOnClickListener {
|
startActivityForResult<AddStudentActivity>(1)
|
}
|
ll_add_stu.setOnClickListener {
|
startActivity<AddStudentActivity>()
|
}
|
refreshLayout.setOnRefreshListener {
|
getData()
|
}
|
|
tv_class_hour.clickDelay {
|
HttpManager.weeksOfGetHours().requestByF(this@CourseFragment,
|
success = { _, data ->
|
|
data?.let {
|
if (it == 0) {
|
toast("没有可分配的课时")
|
return@let
|
}
|
val arrayListOf = arrayListOf<MyCourse>()
|
arrayListOf.addAll(course.filter { it.isAble == 1 })
|
dialogc = UndistributedClassHourialog.show(
|
childFragmentManager,
|
it,
|
arrayListOf,
|
object : StringCallback {
|
override fun onResult(rst: String) {
|
HttpManager.weeksOfAddHours(rst.toLong())
|
.requestByF(this@CourseFragment,
|
success = { _, data ->
|
getData()
|
})
|
dialogc.dismiss()
|
}
|
})
|
}
|
|
})
|
}
|
EventBus.getDefault().register(this)
|
}
|
|
override fun onDestroy() {
|
super.onDestroy()
|
EventBus.getDefault().unregister(this)
|
}
|
|
@Subscribe
|
fun onEvent(e: EmptyEvent) {
|
if (e.code == Const.EventCode.CHANGE_SHOP || e.code == CHANGE_COURSDATA) {
|
getData()
|
}
|
}
|
|
private fun getData() {
|
HttpManager.queryStudentData(currentStudent).requestByF(this, success = { _, data ->
|
refreshLayout.finishRefresh()
|
data?.apply {
|
gp_none.visibility = if (isThere == 1) { //展示运动营成员信息
|
currentStudent = stuId
|
getSchedule()
|
gp_user.visible()
|
tv_name.text = stuName
|
tv_sex.setCompoundDrawablesRelativeWithIntrinsicBounds(
|
0,
|
0,
|
if (stuSex == 1) R.mipmap.nan else R.mipmap.nv,
|
0
|
)
|
tv_age.text = "/${stuAge}岁"
|
iv_avatar.setImageURI(stuImg)
|
try {
|
val heightS = "${height}cm"
|
tv_height.text =
|
SpanBuilder(heightS).size(heightS.length - 2, heightS.length, 14)
|
.build()
|
val weightS = "${weight}kg"
|
tv_wight.text =
|
SpanBuilder(weightS).size(weightS.length - 2, weightS.length, 14)
|
.build()
|
val bmiS = "${bmi}${bodyStatus}"
|
tv_bmi.text =
|
SpanBuilder(bmiS).size(bmiS.indexOf(bodyStatus), bmiS.length, 14)
|
.build()
|
} catch (e: Exception) {
|
|
}
|
|
if (courseList.isNullOrEmpty()) {
|
ll_course.gone()
|
cl_course_info.gone()
|
} else {
|
ll_course.visible()
|
course.clear()
|
course.addAll(courseList)
|
myCourseAdapter.notifyDataSetChanged()
|
cl_course_info.visibility = if (course[0].courseType != 1) View.GONE else View.VISIBLE
|
refreshCourseInfo()
|
}
|
getMedal(stuId)
|
exerciseList.clear()
|
exerciseList.addAll(exerciseVideoList)
|
exerciseAdapter.notifyDataSetChanged()
|
View.GONE
|
} else {
|
gp_user.gone()
|
getImg()
|
View.VISIBLE
|
}
|
}
|
}) { _, _ ->
|
refreshLayout.finishRefresh(false)
|
}
|
}
|
|
private fun getImg(){
|
val map = hashMapOf<String, Any>()
|
map["position"] = 1
|
HttpManager.queryString(Apis.querySystemImg,map).requestByF(this){ _, data->
|
data?.loadLongImage(requireContext(),iv_none)
|
}
|
}
|
|
private fun initWeeks() {
|
val calendar = Calendar.getInstance()
|
currentDate = System.currentTimeMillis()
|
calendar.set(Calendar.DAY_OF_WEEK, 1)
|
val firstOfWeek = calendar.timeInMillis
|
Log.e(Const.Tag, "英文本周的第一天(星期天)是${firstOfWeek.toTime()}")
|
val startTimes = arrayListOf<Long>()
|
startTimes.addAll((0..9).map { firstOfWeek + (1000L * 24 * 60 * 60 * 7 * it) })
|
frags.clear()
|
frags.addAll(startTimes.map { WeekFragment.newInstance(it) })
|
vp_week.adapter = WeekViewPagerAdapter(childFragmentManager, frags)
|
vp_week.offscreenPageLimit = 10
|
tv_duration.text =
|
"${firstOfWeek.toTime("yyyy.MM.dd")}-${(firstOfWeek + 1000L * 60 * 60 * 24 * 6).toTime("yyyy.MM.dd")}"
|
vp_week.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
|
override fun onPageScrolled(
|
position: Int,
|
positionOffset: Float,
|
positionOffsetPixels: Int
|
) {
|
|
}
|
|
override fun onPageSelected(position: Int) {
|
tv_duration.text = "${startTimes[vp_week.currentItem].toTime("yyyy.MM.dd")} - ${
|
(startTimes[vp_week.currentItem] + 1000L * 24 * 60 * 60 * 6).toTime("yyyy.MM.dd")
|
}"
|
}
|
|
override fun onPageScrollStateChanged(state: Int) {
|
}
|
})
|
tv_last_week.setOnClickListener {
|
vp_week.setCurrentItem(
|
Math.max(
|
vp_week.currentItem - 1,
|
0
|
), true
|
)
|
}
|
tv_next_week.setOnClickListener {
|
vp_week.setCurrentItem(
|
Math.min(
|
vp_week.currentItem + 1,
|
9
|
), true
|
)
|
}
|
iv_last.setOnClickListener { tv_last_week.callOnClick() }
|
iv_next.setOnClickListener { tv_next_week.callOnClick() }
|
}
|
|
private fun getMedal(stuId: String) {
|
HttpManager.stuGoog(stuId).requestByF(this) { _, data ->
|
medalList.clear()
|
medalList.addAll(data ?: arrayListOf())
|
tv_1.visibility = if (medalList.isEmpty()) View.GONE else{
|
medalList.add(Medal("更多","",0,0,"",1))
|
View.VISIBLE
|
}
|
medalAdapter.notifyDataSetChanged()
|
}
|
}
|
|
/**
|
* 显示课时信息
|
*/
|
private fun refreshCourseInfo() {
|
val current = course[myCourseAdapter.checked]
|
val total = "总课时数 ${current.totalNums}"
|
tv_total.text = SpanBuilder(total).size(0, 4, 12).build()
|
val used = "已扣课时数 ${current.deductedNums}"
|
tv_used.text = SpanBuilder(used).size(0, 5, 12).build()
|
val last = "剩余课时数 ${current.remainingNums}"
|
tv_last.text = SpanBuilder(last).size(0, 5, 12).build()
|
}
|
|
fun notifyWeekCheckChanged(time: Long) {
|
currentDate = time
|
frags.forEach {
|
if (it.isAdded)
|
it.refreshAdapter()
|
}
|
getSchedule()
|
}
|
|
@SuppressLint("SetTextI18n")
|
private fun getSchedule() {
|
if (storeId.isEmpty())
|
myToast("请先选择门店")
|
else
|
HttpManager.weeksOfCourseDetails(currentStudent ?: "", storeId, currentDate.toTime("yyyy-MM-dd"))
|
.requestByF(this) { _, data ->
|
data?.let {
|
tv_address.text = it.name + " " + formatDistance(it.distance.toString()) + "km"
|
scheduleList.clear()
|
scheduleList.addAll(it.data?: arrayListOf())
|
tv_address.setOnClickListener {_->
|
if (it.lat.isNotEmpty()){
|
val mapTypeDialog = MapTypeDialog()
|
mapTypeDialog.callback = object :StringCallback{
|
override fun onResult(rst: String) {
|
if(rst == "b"){
|
ThirdPartyMapsGuide.goToBaiduActivity(requireContext(),it.name,it.lon.toDouble(),it.lat.toDouble())
|
}else if (rst == "a"){
|
ThirdPartyMapsGuide.goToGaoDeMap(requireContext(),it.name,it.lon,it.lat)
|
}else{
|
ThirdPartyMapsGuide.goToTencentMap(requireContext(),it.name,it.lon.toDouble(),it.lat.toDouble())
|
}
|
}
|
}
|
mapTypeDialog.showAllowingStateLoss(childFragmentManager,"map")
|
}
|
}
|
}
|
|
}
|
}
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
super.onActivityResult(requestCode, resultCode, data)
|
if (resultCode == Activity.RESULT_OK) {
|
if (requestCode == 2&&data!=null){
|
currentStudent = data.getStringExtra("stuId")
|
}
|
getData()
|
}
|
}
|
|
companion object {
|
var currentDate = 0L //当前选择日期
|
}
|
}
|