package com.dollearn.student.ui.home
|
|
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.View
|
import cn.sinata.xldutils.activity.SelectPhotoDialog
|
import cn.sinata.xldutils.gone
|
import cn.sinata.xldutils.utils.isValidIdCard
|
import cn.sinata.xldutils.utils.isValidPhone
|
import cn.sinata.xldutils.utils.myToast
|
import com.dollearn.student.R
|
import com.dollearn.student.dialog.ChooseBirthDialog
|
import com.dollearn.student.dialog.SexDialog
|
import com.dollearn.student.network.HttpManager
|
import com.dollearn.student.network.entity.Student
|
import com.dollearn.student.network.request
|
import com.dollearn.student.ui.TransparentStatusBarActivity
|
import com.dollearn.student.utils.Const
|
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.activity_add_student.*
|
import org.greenrobot.eventbus.EventBus
|
import org.jetbrains.anko.startActivityForResult
|
import java.lang.Exception
|
|
class AddStudentActivity:TransparentStatusBarActivity() {
|
override fun setContentView() = R.layout.activity_add_student
|
|
private val type by lazy { intent.getIntExtra("type", TYPE_COURSE) }
|
private val user by lazy { intent.getParcelableExtra<Student>("user") }
|
private var isRefused = false
|
|
private var avatar = ""
|
|
override fun initClick() {
|
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:${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_birth.clickDelay {
|
val chooseBirthDialog = ChooseBirthDialog()
|
chooseBirthDialog.setCallback(object : ChooseBirthDialog.Callback {
|
override fun onOk(s: String) {
|
tv_birth.text = s
|
}
|
})
|
chooseBirthDialog.show(supportFragmentManager, "birth")
|
}
|
tv_sex.clickDelay {
|
SexDialog.show(supportFragmentManager,object :StringCallback{
|
override fun onResult(rst: String) {
|
tv_sex.text = rst
|
}
|
})
|
}
|
tv_action.clickDelay {
|
if (iv_avatar.visibility == View.VISIBLE && avatar.isEmpty()){
|
myToast("请上传头像")
|
return@clickDelay
|
}
|
val name = et_name.text.toString()
|
if (name.isNullOrEmpty()){
|
myToast("请输入成员姓名")
|
return@clickDelay
|
}
|
var birth = tv_birth.text.toString()
|
if (birth.isNullOrEmpty()){
|
myToast("请选择生日")
|
return@clickDelay
|
}
|
val sex = tv_sex.text.toString()
|
if (sex.isNullOrEmpty()){
|
myToast("请选择性别")
|
return@clickDelay
|
}
|
val height = et_height.text.toString()
|
if (type!= TYPE_EDIT_MATCH&&type!= TYPE_EDIT_WORLD&&height.isNullOrEmpty()){
|
myToast("请填写身高")
|
return@clickDelay
|
}
|
var heightD = 0.0
|
if (type!= TYPE_EDIT_MATCH&&type!= TYPE_EDIT_WORLD){
|
try {
|
heightD = height.toDouble()
|
}catch (e:Exception){
|
myToast("身高输入有误")
|
return@clickDelay
|
}
|
}
|
val weight = et_weight.text.toString()
|
if (type!= TYPE_EDIT_MATCH&&type!= TYPE_EDIT_WORLD&&weight.isNullOrEmpty()){
|
myToast("请填写体重")
|
return@clickDelay
|
}
|
var weightD = 0.0
|
if (type!= TYPE_EDIT_MATCH&&type!= TYPE_EDIT_WORLD){
|
try {
|
weightD = weight.toDouble()
|
}catch (e:Exception){
|
myToast("体重输入有误")
|
return@clickDelay
|
}
|
}
|
val idCard = et_id_card.text.toString()
|
val phone = et_phone.text.toString()
|
when(type){
|
TYPE_ADD_MATCH ->{
|
if (phone.isNotEmpty()&&!phone.isValidPhone()){
|
myToast("请输入正确的手机号")
|
return@clickDelay
|
}
|
if (idCard.isValidIdCard())
|
birth = "${idCard.substring(6,10)}-${idCard.substring(10,12)}-${idCard.substring(12,14)}"
|
HttpManager.addParticipant(avatar,name,birth,if (sex == "男") 1 else 2,heightD,weightD,
|
if (idCard.isNullOrEmpty()) null else idCard,if (phone.isNullOrEmpty()) null else phone)
|
.request(this){_,data->
|
myToast("添加成功")
|
setResult(Activity.RESULT_OK)
|
finish()
|
}
|
}
|
TYPE_ADD_WORLD->{
|
if (!phone.isValidPhone()){
|
myToast("请输入正确的手机号")
|
return@clickDelay
|
}
|
if (!idCard.isValidIdCard()){
|
myToast("请输入正确的身份证号")
|
return@clickDelay
|
}
|
birth = "${idCard.substring(6,10)}-${idCard.substring(10,12)}-${idCard.substring(12,14)}"
|
HttpManager.addParticipant(avatar,name,birth,if (sex == "男") 1 else 2,heightD,weightD,
|
if (idCard.isNullOrEmpty()) null else idCard,if (phone.isNullOrEmpty()) null else phone)
|
.request(this){_,data->
|
myToast("添加成功")
|
setResult(Activity.RESULT_OK)
|
finish()
|
}
|
}
|
TYPE_EDIT_MATCH, TYPE_EDIT_WORLD->{
|
if (!idCard.isValidIdCard()){
|
myToast("请输入正确的身份证号")
|
return@clickDelay
|
}
|
birth = "${idCard.substring(6,10)}-${idCard.substring(10,12)}-${idCard.substring(12,14)}"
|
HttpManager.editParticipant(user?.avatar?:"",user?.id?:"",null,null,null,name,idCard,if (sex == "男") 1 else 2,birth)
|
.request(this){_,data->
|
myToast("修改成功")
|
setResult(Activity.RESULT_OK)
|
finish()
|
}
|
}
|
else->{
|
if (!idCard.isValidIdCard()){
|
myToast("请输入正确的身份证号")
|
return@clickDelay
|
}
|
birth = "${idCard.substring(6,10)}-${idCard.substring(10,12)}-${idCard.substring(12,14)}"
|
HttpManager.addStudent(name,birth,if (sex == "男") 1 else 2,avatar,heightD,weightD, if (idCard.isNullOrEmpty()) null else idCard,if (phone.isNullOrEmpty()) null else phone,if (type == TYPE_EDIT_STUDENT) user?.stuId else null).request(this){_,data->
|
myToast(if (type == TYPE_EDIT_STUDENT) "修改成功" else "添加成功")
|
EventBus.getDefault().post(Const.EventCode.CHANGE_COURSDATA)
|
setResult(Activity.RESULT_OK)
|
finish()
|
}
|
}
|
|
}
|
|
}
|
}
|
|
override fun initView() {
|
when (type) {
|
TYPE_COURSE ->{
|
tv_id_title.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_must,0,0,0)
|
}
|
TYPE_ADD_MATCH -> {
|
title = "添加人员"
|
tv_birth.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.mipmap.calender,0)
|
// tv_2.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_must,0,0,0)
|
}
|
TYPE_ADD_WORLD -> {
|
title = "添加人员"
|
tv_birth.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.mipmap.calender,0)
|
tv_2.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_must,0,0,0)
|
tv_id_title.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_must,0,0,0)
|
}
|
TYPE_EDIT_MATCH -> {
|
title = "选择人员"
|
iv_avatar.gone()
|
cl_add.gone()
|
tv_birth.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.mipmap.calender,0)
|
et_name.setText(user?.name)
|
tv_birth.text = user?.birthday
|
tv_sex.text = if (user?.gender == 1) "男" else "女"
|
}
|
TYPE_EDIT_WORLD -> {
|
title = "人员管理"
|
iv_avatar.gone()
|
cl_add.gone()
|
tv_birth.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.mipmap.calender,0)
|
tv_id_title.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_must,0,0,0)
|
et_name.setText(user?.name)
|
tv_birth.text = user?.birthday
|
tv_sex.text = if (user?.gender == 1) "男" else "女"
|
et_name.isEnabled = false
|
tv_birth.isEnabled = false
|
tv_sex.isEnabled = false
|
}
|
TYPE_EDIT_STUDENT -> {
|
tv_id_title.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.ic_must,0,0,0)
|
title = "修改运动营成员信息"
|
avatar = user?.stuHeadImg?:""
|
iv_avatar.setImageURI(avatar)
|
et_name.setText(user?.stuName)
|
tv_birth.text = user?.birthday
|
tv_sex.text = if (user?.sex == 1) "男" else "女"
|
et_height.setText(user?.stuHeight.toString())
|
et_weight.setText(user?.stuWeight.toString())
|
et_phone.setText(user?.stuPhone)
|
et_id_card.setText(user?.idCard)
|
}
|
}
|
}
|
|
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(this, object :
|
StringCallback {
|
override fun onResult(rst: String) {
|
avatar = rst
|
iv_avatar.setImageURI(avatar)
|
}
|
})
|
}
|
}
|
|
companion object{
|
const val TYPE_COURSE = 1//添加运动营成员
|
const val TYPE_ADD_MATCH = 2//添加参赛人员 //1.23:人员管理也是赛事人员同流程和判断条件,因此复用type
|
const val TYPE_ADD_WORLD = 5//添加世界杯人员
|
const val TYPE_EDIT_WORLD = 6//编辑世界杯人员
|
const val TYPE_EDIT_MATCH = 3//修改参赛人员
|
const val TYPE_EDIT_STUDENT = 4//修改运动营成员信息
|
}
|
}
|