罗明文
2024-06-16 9673bcd57c6100ad9fdfbee728ef078104511fc1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.dollearn.student.dialog
 
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_switch_student.*
import org.jetbrains.anko.support.v4.startActivityForResult
 
/**
 * 单选
 */
class SwitchStudentDialog:BaseDialogFragment() {
    override fun setContentView() = R.layout.dialog_switch_student
 
    override fun setGravity() = Gravity.BOTTOM
 
    private val list = arrayListOf<Student>()
    private val adapter by lazy {  StudentAdapter(list,StudentAdapter.TYPE_MATCH_CHOOSE) }
 
    private val checked by lazy { arguments?.getParcelable<Student>("checked") }
 
    var callback:ResultCallback? = null
 
    override fun initView() {
        rv_student.layoutManager = LinearLayoutManager(requireContext())
        rv_student.adapter = adapter
        getStudent()
 
        iv_close.clickDelay {
            dismissAllowingStateLoss()
        }
        adapter.setOnItemClickListener { view, position ->
            val stuId = list[position].id
            adapter.checked.clear()
            adapter.checked.add(stuId?:"")
            adapter.notifyDataSetChanged()
        }
        tv_action.setOnClickListener {
            if (adapter.checked.isEmpty())
                myToast("请选择一个人员")
            else{
                val list1 = list.filter { it.id in adapter.checked }
                callback?.checked(list1)
                dismissAllowingStateLoss()
            }
        }
    }
 
    private fun getStudent(){
        HttpManager.getParticipant().request(requireActivity() as BaseActivity){_,data->
            list.clear()
            list.addAll(data?: arrayListOf())
            if (checked!=null)
                adapter.checked.add(checked!!.id?:"" )
            adapter.notifyDataSetChanged()
        }
    }
 
    interface ResultCallback{
        fun checked(list: List<Student>)
    }
 
    companion object{
        /**
         * @param filterReal true:需要过滤掉未实名的成员
         */
        fun show(fm:FragmentManager,student:Student?,callback: ResultCallback){
            val chooseStudentDialog = SwitchStudentDialog()
            chooseStudentDialog.arguments = bundleOf("checked" to student)
            chooseStudentDialog.callback = callback
            chooseStudentDialog.showAllowingStateLoss(fm,"stu")
        }
    }
}