package com.dollearn.student.dialog
|
|
import android.app.Activity
|
import android.content.Intent
|
import android.view.Gravity
|
import androidx.core.os.bundleOf
|
import androidx.fragment.app.FragmentManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
import cn.sinata.xldutils.activity.BaseActivity
|
import cn.sinata.xldutils.utils.myToast
|
import cn.sinata.xldutils.utils.showAllowingStateLoss
|
import com.dollearn.student.R
|
import com.dollearn.student.network.HttpManager
|
import com.dollearn.student.network.entity.Student
|
import com.dollearn.student.network.request
|
import com.dollearn.student.ui.home.AddStudentActivity
|
import com.dollearn.student.ui.home.ChooseUserActivity
|
import com.dollearn.student.ui.home.adapter.StudentAdapter
|
import com.dollearn.student.utils.extention.clickDelay
|
import kotlinx.android.synthetic.main.dialog_choose_student.*
|
import org.jetbrains.anko.support.v4.startActivityForResult
|
|
class ChooseStudentDialog:BaseDialogFragment() {
|
override fun setContentView() = R.layout.dialog_choose_student
|
|
override fun setGravity() = Gravity.BOTTOM
|
|
private val list = arrayListOf<Student>()
|
private val adapter by lazy { StudentAdapter(list,if (type == TYPE_MATCH||type == TYPE_WORLD) StudentAdapter.TYPE_MATCH_CHOOSE else StudentAdapter.TYPE_COURSE_CHOOSE) }
|
|
private val checked by lazy { arguments?.getParcelableArrayList<Student>("checked") }
|
private val type by lazy { arguments?.getInt("type", TYPE_COURSE)?: TYPE_COURSE }
|
private val max by lazy { arguments?.getInt("max", -1) }
|
private val gender by lazy { arguments?.getInt("gender", 0) }
|
private val age by lazy { arguments?.getString("age") }
|
private val filterReal by lazy { arguments?.getBoolean("filterReal", false)?:false } //是否过滤非实名学员,默认不过滤
|
|
var callback:ResultCallback? = null
|
|
override fun initView() {
|
if (type != TYPE_COURSE)
|
tv_add_student.text = "添加人员"
|
rv_student.layoutManager = LinearLayoutManager(requireContext())
|
rv_student.adapter = adapter
|
getStudent()
|
tv_add_student.clickDelay {
|
if (type == TYPE_MATCH)
|
startActivityForResult<ChooseUserActivity>(2)
|
else
|
if (type == TYPE_WORLD)
|
startActivityForResult<AddStudentActivity>(2,"type" to AddStudentActivity.TYPE_ADD_WORLD)
|
else
|
startActivityForResult<AddStudentActivity>(2)
|
}
|
adapter.setOnItemClickListener { view, position ->
|
val stuId = if (type == TYPE_MATCH||type == TYPE_WORLD) list[position].id else list[position].stuId
|
if (stuId in adapter.checked)
|
adapter.checked.remove(stuId)
|
else if (max!=-1&&adapter.checked.size == max)
|
myToast("最多选择${max}人")
|
else{
|
if (!age.isNullOrEmpty()){//校验年龄
|
val split = age!!.split("-")
|
val startAge = split[0].toInt()
|
val endAge = split[1].toInt()
|
if (list[position].age?:0 !in startAge..endAge){
|
myToast("年龄不符")
|
return@setOnItemClickListener
|
}
|
}
|
if (gender != 0&&list[position].gender != gender){ //校验性别
|
myToast("性别不符")
|
return@setOnItemClickListener
|
}
|
adapter.checked.add(stuId ?:"")
|
}
|
adapter.notifyDataSetChanged()
|
}
|
tv_action.setOnClickListener {
|
if (adapter.checked.isEmpty())
|
myToast("至少选择一个运动营成员")
|
else{
|
val list1 = list.filter { (if (type == TYPE_COURSE) it.stuId else it.id) in adapter.checked }
|
if (type == TYPE_COURSE)
|
list1.forEach {
|
it.name = it.stuName
|
it.age = it.stuAge
|
it.phone = it.stuPhone
|
it.id = it.stuId
|
}
|
callback?.checked(list1)
|
dismissAllowingStateLoss()
|
}
|
}
|
}
|
|
private fun getStudent(){
|
HttpManager.listOfStu(type,if (filterReal) 1 else null).request(requireActivity() as BaseActivity){_,data->
|
var showDefault = list.isEmpty()
|
list.clear()
|
list.addAll(data?: arrayListOf())
|
if (showDefault){
|
if (!checked.isNullOrEmpty())
|
adapter.checked.addAll(checked!!.map { if (type == TYPE_COURSE) it.stuId?:"" else it.id?:"" })
|
else
|
adapter.checked.addAll(list.filter { it.isNot == 1 }.map { it.stuId?:"" })
|
}
|
adapter.notifyDataSetChanged()
|
}
|
}
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
super.onActivityResult(requestCode, resultCode, data)
|
if (resultCode == Activity.RESULT_OK&&requestCode == 2){
|
getStudent()
|
}
|
}
|
|
interface ResultCallback{
|
fun checked(list: List<Student>)
|
}
|
|
companion object{
|
const val TYPE_COURSE = 1 //运动营运动营成员
|
const val TYPE_MATCH = 2 //参赛人员
|
const val TYPE_WORLD = 3 //参赛人员(世界杯)
|
|
/**
|
* @param filterReal true:需要过滤掉未实名的成员
|
*/
|
fun show(fm:FragmentManager,type:Int,students:ArrayList<Student>,callback: ResultCallback,max:Int? = null,filterReal:Boolean? = false,gender:Int? = null,age:String? = null){
|
val chooseStudentDialog = ChooseStudentDialog()
|
chooseStudentDialog.arguments = bundleOf("type" to type,"checked" to students,"max" to max,"gender" to gender,"age" to age,"filterReal" to filterReal)
|
chooseStudentDialog.callback = callback
|
chooseStudentDialog.showAllowingStateLoss(fm,"stu")
|
}
|
}
|
}
|