|
@@ -1,20 +1,95 @@
|
|
|
package org.brynhild.graduation.activity.teacher.ui.user
|
|
|
|
|
|
+import android.annotation.SuppressLint
|
|
|
+import android.app.Activity
|
|
|
+import android.content.Intent
|
|
|
import android.os.Bundle
|
|
|
+import android.os.Handler
|
|
|
+import android.os.Message
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
|
import android.view.ViewGroup
|
|
|
+import android.widget.Toast
|
|
|
+import androidx.appcompat.app.AlertDialog
|
|
|
import androidx.fragment.app.Fragment
|
|
|
+import com.bumptech.glide.Glide
|
|
|
+import com.google.gson.Gson
|
|
|
+import org.brynhild.graduation.R
|
|
|
+import org.brynhild.graduation.activity.common.LoginActivity
|
|
|
+import org.brynhild.graduation.common.config.LoginConfiguration
|
|
|
+import org.brynhild.graduation.common.config.MyApplication
|
|
|
+import org.brynhild.graduation.common.constant.BroadcastConstant
|
|
|
+import org.brynhild.graduation.common.transfer.Result
|
|
|
+import org.brynhild.graduation.common.transfer.dto.FileInfo
|
|
|
+import org.brynhild.graduation.common.transfer.handler.ResponseHandler
|
|
|
+import org.brynhild.graduation.common.transfer.utils.ServiceCreator
|
|
|
+import org.brynhild.graduation.common.transfer.vo.user.ModifyAccountInfo
|
|
|
+import org.brynhild.graduation.common.utils.FileUploadUtil
|
|
|
+import org.brynhild.graduation.common.utils.JsonUtils
|
|
|
+import org.brynhild.graduation.common.utils.Uri2PathUtil
|
|
|
import org.brynhild.graduation.databinding.FragmentUserTeacherBinding
|
|
|
+import org.brynhild.graduation.databinding.ModifyAccountBasicInfoDialogBinding
|
|
|
+import org.brynhild.graduation.databinding.ModifyAccountPasswordInfoDialogBinding
|
|
|
+import org.brynhild.graduation.service.http.AccountService
|
|
|
+import retrofit2.Call
|
|
|
+import retrofit2.Callback
|
|
|
+import retrofit2.Response
|
|
|
+import java.io.File
|
|
|
+import java.io.IOException
|
|
|
|
|
|
class UserFragment : Fragment() {
|
|
|
|
|
|
private var _binding: FragmentUserTeacherBinding? = null
|
|
|
+ private val refreshBasicInfo = 1
|
|
|
+ private val updateAccountAvatar = 2
|
|
|
+ private lateinit var modifyAccountBasicInfoDialogBinding: ModifyAccountBasicInfoDialogBinding
|
|
|
+ private lateinit var modifyAccountPasswordInfoDialogBinding: ModifyAccountPasswordInfoDialogBinding
|
|
|
|
|
|
- // This property is only valid between onCreateView and
|
|
|
- // onDestroyView.
|
|
|
private val binding get() = _binding!!
|
|
|
|
|
|
+ val handler: Handler = Handler {
|
|
|
+ when (it.what) {
|
|
|
+ refreshBasicInfo -> {
|
|
|
+ if (LoginConfiguration.userInfo != null) {
|
|
|
+ binding.email.text = LoginConfiguration.userInfo!!.user.email
|
|
|
+ binding.tel.text = LoginConfiguration.userInfo!!.user.tel
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ updateAccountAvatar -> {
|
|
|
+ val path = it.obj as String
|
|
|
+ Glide.with(this).load(path).into(binding.iconImage)
|
|
|
+ val service = ServiceCreator.create(AccountService::class.java)
|
|
|
+ val data = ModifyAccountInfo(null, null, path, null)
|
|
|
+ service.modifyAccountInfo(LoginConfiguration.userInfo!!.token, data)
|
|
|
+ .enqueue(object : Callback<Result> {
|
|
|
+ override fun onResponse(call: Call<Result>, response: Response<Result>) {
|
|
|
+ ResponseHandler.handle(response, { ret ->
|
|
|
+ Toast.makeText(
|
|
|
+ MyApplication.context,
|
|
|
+ ret.message,
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ LoginConfiguration.userInfo!!.user.avatar = path
|
|
|
+ }, { ret ->
|
|
|
+ Toast.makeText(
|
|
|
+ MyApplication.context,
|
|
|
+ ret.message,
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onFailure(call: Call<Result>, t: Throwable) {
|
|
|
+ Toast.makeText(MyApplication.context, "请求失败", Toast.LENGTH_SHORT).show()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ false
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
override fun onCreateView(
|
|
|
inflater: LayoutInflater,
|
|
|
container: ViewGroup?,
|
|
@@ -24,6 +99,20 @@ class UserFragment : Fragment() {
|
|
|
_binding = FragmentUserTeacherBinding.inflate(inflater, container, false)
|
|
|
val root: View = binding.root
|
|
|
|
|
|
+ val user = LoginConfiguration.userInfo!!.user
|
|
|
+ binding.username.text = user.username
|
|
|
+ binding.email.text = user.email
|
|
|
+ binding.lastLogin.text = user.lastLogin
|
|
|
+ binding.name.text = user.name
|
|
|
+ binding.sex.text = user.sex
|
|
|
+ binding.tel.text = user.tel
|
|
|
+
|
|
|
+ Glide.with(this)
|
|
|
+ .load(user.avatar ?: R.drawable.nav_icon_default)
|
|
|
+ .into(binding.iconImage)
|
|
|
+
|
|
|
+ bindListener()
|
|
|
+
|
|
|
return root
|
|
|
}
|
|
|
|
|
@@ -31,4 +120,195 @@ class UserFragment : Fragment() {
|
|
|
super.onDestroyView()
|
|
|
_binding = null
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ @SuppressLint("UseRequireInsteadOfGet")
|
|
|
+ private fun bindListener() {
|
|
|
+ binding.modifyBtn.setOnClickListener {
|
|
|
+ modifyAccountBasicInfoDialogBinding =
|
|
|
+ ModifyAccountBasicInfoDialogBinding.inflate(LayoutInflater.from(this@UserFragment.context))
|
|
|
+ val user = LoginConfiguration.userInfo!!.user
|
|
|
+ modifyAccountBasicInfoDialogBinding.email.setText(user.email)
|
|
|
+ modifyAccountBasicInfoDialogBinding.tel.setText(user.tel)
|
|
|
+ if(this@UserFragment.context!=null){
|
|
|
+ AlertDialog.Builder(this@UserFragment.context!!)
|
|
|
+ .setTitle("修改联系方式")
|
|
|
+ .setView(modifyAccountBasicInfoDialogBinding.root)
|
|
|
+ .setPositiveButton("提交修改") { _, _ ->
|
|
|
+ val service = ServiceCreator.create(AccountService::class.java)
|
|
|
+ val data = ModifyAccountInfo(
|
|
|
+ null, modifyAccountBasicInfoDialogBinding.email.text.toString(),
|
|
|
+ null, modifyAccountBasicInfoDialogBinding.tel.text.toString()
|
|
|
+ )
|
|
|
+
|
|
|
+ service.modifyAccountInfo(LoginConfiguration.userInfo!!.token, data)
|
|
|
+ .enqueue(object : Callback<Result> {
|
|
|
+ override fun onResponse(
|
|
|
+ call: Call<Result>,
|
|
|
+ response: Response<Result>
|
|
|
+ ) {
|
|
|
+ ResponseHandler.handle(response, {
|
|
|
+ Toast.makeText(
|
|
|
+ MyApplication.context,
|
|
|
+ it.message,
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ val oldEmail = LoginConfiguration.userInfo!!.user.email
|
|
|
+ LoginConfiguration.userInfo!!.user.tel =
|
|
|
+ modifyAccountBasicInfoDialogBinding.tel.text.toString()
|
|
|
+ LoginConfiguration.userInfo!!.user.email =
|
|
|
+ modifyAccountBasicInfoDialogBinding.email.text.toString()
|
|
|
+ handler.sendEmptyMessage(refreshBasicInfo)
|
|
|
+ if (oldEmail != LoginConfiguration.userInfo!!.user.email) {
|
|
|
+ LoginConfiguration.login_password = ""
|
|
|
+ LoginConfiguration.userInfo = null
|
|
|
+ LoginConfiguration.saveLoginInfo()
|
|
|
+ val intent = Intent(BroadcastConstant.OFFLINE)
|
|
|
+ MyApplication.context.sendBroadcast(intent)
|
|
|
+ }
|
|
|
+ }, {
|
|
|
+ Toast.makeText(
|
|
|
+ MyApplication.context,
|
|
|
+ it.message,
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onFailure(call: Call<Result>, t: Throwable) {
|
|
|
+ Toast.makeText(MyApplication.context, "请求失败", Toast.LENGTH_SHORT)
|
|
|
+ .show()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ .setNegativeButton("取消修改") { _, _ ->
|
|
|
+
|
|
|
+ }
|
|
|
+ .show()
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ binding.modifyPassword.setOnClickListener {
|
|
|
+ modifyAccountPasswordInfoDialogBinding =
|
|
|
+ ModifyAccountPasswordInfoDialogBinding.inflate(LayoutInflater.from(this@UserFragment.context))
|
|
|
+ if(this@UserFragment.context!=null){
|
|
|
+ AlertDialog.Builder(this@UserFragment.context!!)
|
|
|
+ .setTitle("修改密码")
|
|
|
+ .setView(modifyAccountPasswordInfoDialogBinding.root)
|
|
|
+ .setPositiveButton("提交修改") { _, _ ->
|
|
|
+ val password =
|
|
|
+ modifyAccountPasswordInfoDialogBinding.newPassword.text.toString()
|
|
|
+ val passwordConfirm =
|
|
|
+ modifyAccountPasswordInfoDialogBinding.newPasswordConfirm.text.toString()
|
|
|
+ if (password != passwordConfirm) {
|
|
|
+ Toast.makeText(MyApplication.context, "两次密码不匹配", Toast.LENGTH_SHORT).show()
|
|
|
+ return@setPositiveButton
|
|
|
+ }
|
|
|
+ val service = ServiceCreator.create(AccountService::class.java)
|
|
|
+ val data = ModifyAccountInfo(
|
|
|
+ password, null,
|
|
|
+ null, null
|
|
|
+ )
|
|
|
+ service.modifyAccountInfo(LoginConfiguration.userInfo!!.token, data)
|
|
|
+ .enqueue(object : Callback<Result> {
|
|
|
+ override fun onResponse(
|
|
|
+ call: Call<Result>,
|
|
|
+ response: Response<Result>
|
|
|
+ ) {
|
|
|
+ ResponseHandler.handle(response, {
|
|
|
+ Toast.makeText(
|
|
|
+ MyApplication.context,
|
|
|
+ it.message,
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ LoginConfiguration.login_password = ""
|
|
|
+ LoginConfiguration.userInfo = null
|
|
|
+ LoginConfiguration.saveLoginInfo()
|
|
|
+
|
|
|
+ val intent = Intent(BroadcastConstant.OFFLINE)
|
|
|
+ MyApplication.context.sendBroadcast(intent)
|
|
|
+ }, {
|
|
|
+
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onFailure(call: Call<Result>, t: Throwable) {
|
|
|
+ Toast.makeText(MyApplication.context, "请求失败", Toast.LENGTH_SHORT)
|
|
|
+ .show()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ .setNegativeButton("取消修改") { _, _ ->
|
|
|
+
|
|
|
+ }
|
|
|
+ .show()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ binding.iconImage.setOnClickListener {
|
|
|
+ val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
|
|
|
+ intent.addCategory(Intent.CATEGORY_OPENABLE)
|
|
|
+ intent.type = "image/*"
|
|
|
+ startActivityForResult(intent, updateAccountAvatar)
|
|
|
+ }
|
|
|
+
|
|
|
+ binding.logout.setOnClickListener {
|
|
|
+ LoginConfiguration.login_password = ""
|
|
|
+ LoginConfiguration.saveLoginInfo()
|
|
|
+ val i = Intent(MyApplication.context, LoginActivity::class.java)
|
|
|
+ startActivity(i)
|
|
|
+ activity?.finish()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
+ super.onActivityResult(requestCode, resultCode, data)
|
|
|
+ when (requestCode) {
|
|
|
+ updateAccountAvatar -> {
|
|
|
+ if (resultCode == Activity.RESULT_OK && data != null) {
|
|
|
+ val uri = data.data
|
|
|
+ if (Uri2PathUtil.getRealPathFromUri(this@UserFragment.context, uri) != null) {
|
|
|
+ //从uri得到绝对路径,并获取到file文件
|
|
|
+ val file = File(Uri2PathUtil.getRealPathFromUri(this@UserFragment.context, uri))
|
|
|
+ FileUploadUtil.uploadFile(file, object : okhttp3.Callback {
|
|
|
+ override fun onFailure(call: okhttp3.Call, e: IOException) {
|
|
|
+ println("failed")
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onResponse(
|
|
|
+ call: okhttp3.Call,
|
|
|
+ response: okhttp3.Response
|
|
|
+ ) {
|
|
|
+ if (response.isSuccessful) {
|
|
|
+ val body = response.body()?.string()
|
|
|
+ val gson = Gson()
|
|
|
+ val result = gson.fromJson(body, Result::class.java)
|
|
|
+ println(result)
|
|
|
+ var json = gson.toJson(result.data)
|
|
|
+ val fileInfoList = JsonUtils.fromJson2List<FileInfo>(json)
|
|
|
+ if (fileInfoList?.size == 1) {
|
|
|
+ val message = Message()
|
|
|
+ message.what = updateAccountAvatar
|
|
|
+ json = gson.toJson(fileInfoList[0])
|
|
|
+ val info = JsonUtils.fromJson<FileInfo>(json)
|
|
|
+ println(info)
|
|
|
+ if (info != null) {
|
|
|
+ message.obj = info.path
|
|
|
+ handler.sendMessage(message)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ Toast.makeText(MyApplication.context, "获取文件失败", Toast.LENGTH_SHORT).show()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|