lmw
2024-06-19 046174d13aeee8ed585ab2d4e28b8bce272e0217
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.dollearn.student.dialog
 
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import android.view.Gravity
import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentManager
import cn.sinata.xldutils.activity.BaseActivity
import cn.sinata.xldutils.activity.SelectPhotoDialog
import cn.sinata.xldutils.utils.isValidPhone
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.request
import com.dollearn.student.utils.extention.clickDelay
import com.dollearn.student.utils.extention.uploadWithCompress
import com.dollearn.student.utils.interfaces.StringCallback
import kotlinx.android.synthetic.main.dialog_edit_user.*
import org.jetbrains.anko.support.v4.startActivityForResult
import java.lang.Exception
 
class EditUserDialog:BaseDialogFragment() {
    override fun setContentView() = R.layout.dialog_edit_user
 
    override fun setGravity() = Gravity.BOTTOM
 
    private val isUserManage by lazy { //true:学员管理(联系方式处去掉必填标识,且不要校验必填)
        arguments?.getBoolean("isUserManage",false)?:false
    }
 
    var callback:StringCallback? = null
    var avatar:String = ""
 
    private var isRefused = false
 
    override fun initView() {
        avatar = arguments?.getString("avatar")?:""
        iv_avatar.setImageURI(avatar)
        et_height.setText(arguments?.getString("height"))
        et_weight.setText(arguments?.getString("weight"))
        et_phone.setText(arguments?.getString("phone"))
 
        if (isUserManage){
            tv_1.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,0,0)
            et_phone.hint = "请输入联系方式"
        }
 
        iv_avatar.clickDelay {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                if (Environment.isExternalStorageManager())
                    startActivityForResult<SelectPhotoDialog>(2)
                else if (!isRefused) {
                    try {
                        val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                        intent.data = Uri.parse("package:${requireContext().packageName}")
                        startActivityForResult(intent, 1024)
                    } catch (e: Exception) {
                        val intent = Intent()
                        intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
                        startActivityForResult(intent, 1024)
                    }
 
                } else {
                    myToast("缺少文件权限")
                }
            } else
                startActivityForResult<SelectPhotoDialog>(1)
        }
 
        tv_cancel.setOnClickListener { dismissAllowingStateLoss() }
        tv_action.clickDelay {
            if (avatar.isNullOrEmpty()){
                myToast("请上传头像")
                return@clickDelay
            }
            val height = et_height.text.toString()
            if (height.isNullOrEmpty()){
                myToast("请填写身高")
                return@clickDelay
            }
            val weight = et_weight.text.toString()
            if (weight.isNullOrEmpty()){
                myToast("请填写体重")
                return@clickDelay
            }
            var weightD:Double
            try {
                weightD = weight.toDouble()
            }catch (e: Exception){
                myToast("体重输入有误")
                return@clickDelay
            }
            val phone = et_phone.text.toString()
            if (!phone.isNullOrEmpty()&&!phone.isValidPhone()){
                myToast("请输入正确的手机号")
                return@clickDelay
            }
            HttpManager.editParticipant(avatar,arguments?.getString("id")?:"",height.toInt(),weightD,phone,null,null,null,null).request(requireActivity() as BaseActivity){_,_->
                myToast("修改成功")
                callback?.onResult("")
                dismissAllowingStateLoss()
            }
        }
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == 1024 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
            isRefused = !Environment.isExternalStorageManager()
        }else if (resultCode == Activity.RESULT_OK&&data!=null){
            data.getStringExtra("path")!!.uploadWithCompress(requireActivity() as BaseActivity, object :
                StringCallback {
                override fun onResult(rst: String) {
                    avatar = rst
                    iv_avatar.setImageURI(avatar)
                }
            })
        }
    }
 
    companion object{
        fun show(fm:FragmentManager,id:String,avatar:String,height:String,weight:String,phone:String,callback:StringCallback,isUserManage:Boolean = false){
            val editUserDialog = EditUserDialog()
            editUserDialog.arguments = bundleOf("id" to id,"avatar" to avatar,"height" to height,"weight" to weight,"phone" to phone,"isUserManage" to isUserManage)
            editUserDialog.callback = callback
            editUserDialog.showAllowingStateLoss(fm,"edit")
        }
    }
}