| | |
| | | import android.os.Bundle |
| | | import android.provider.MediaStore |
| | | import android.view.View |
| | | import android.widget.ProgressBar |
| | | import android.widget.Toast |
| | | import androidx.activity.result.contract.ActivityResultContracts |
| | | import androidx.appcompat.app.AlertDialog |
| | | import androidx.appcompat.app.AppCompatActivity |
| | | import androidx.core.app.ActivityCompat |
| | | import androidx.core.content.ContextCompat |
| | | import androidx.lifecycle.lifecycleScope |
| | | import com.bumptech.glide.Glide |
| | | import com.example.firstapp.databinding.ActivityEditProfileBinding |
| | | import com.example.firstapp.database.service.ApiService |
| | | import kotlinx.coroutines.launch |
| | | import java.io.File |
| | | import okhttp3.MediaType.Companion.toMediaType |
| | | import okhttp3.MultipartBody |
| | | import okhttp3.RequestBody |
| | | import okhttp3.RequestBody.Companion.asRequestBody |
| | | import okhttp3.RequestBody.Companion.toRequestBody |
| | | |
| | | class EditProfileActivity : AppCompatActivity() { |
| | | private lateinit var binding: ActivityEditProfileBinding |
| | | private var selectedImageUri: Uri? = null |
| | | private lateinit var apiService: ApiService |
| | | private var loadingDialog: AlertDialog? = null |
| | | |
| | | private val pickImage = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> |
| | | if (result.resultCode == Activity.RESULT_OK) { |
| | |
| | | binding.btnSaveBottom.setOnClickListener { |
| | | saveAndFinish() |
| | | } |
| | | |
| | | binding.btnSaveBottom.setOnClickListener { |
| | | saveAndFinish() |
| | | } |
| | | } |
| | | |
| | | private fun saveAndFinish() { |
| | | val newNickname = binding.etNickname.text.toString() |
| | | if (newNickname.isEmpty()) { |
| | | Toast.makeText(this, "昵称不能为空", Toast.LENGTH_SHORT).show() |
| | | return |
| | | } |
| | | lifecycleScope.launch { |
| | | try { |
| | | binding.btnSaveBottom.isEnabled = false |
| | | showLoading() |
| | | |
| | | val resultIntent = Intent().apply { |
| | | putExtra("nickname", newNickname) |
| | | putExtra("avatar_uri", selectedImageUri?.toString()) |
| | | val newNickname = binding.etNickname.text.toString() |
| | | if (newNickname.isEmpty()) { |
| | | Toast.makeText(this@EditProfileActivity, "昵称不能为空", Toast.LENGTH_SHORT).show() |
| | | return@launch |
| | | } |
| | | |
| | | // 准备文件和参数 |
| | | val nicknameBody = newNickname.toRequestBody("text/plain".toMediaType()) |
| | | |
| | | // 如果选择了新头像,处理文件 |
| | | val avatarPart = selectedImageUri?.let { uri -> |
| | | val inputStream = contentResolver.openInputStream(uri) |
| | | val file = File(cacheDir, "avatar_temp") |
| | | inputStream?.use { input -> |
| | | file.outputStream().use { output -> |
| | | input.copyTo(output) |
| | | } |
| | | } |
| | | |
| | | val requestFile = file.asRequestBody("image/*".toMediaType()) |
| | | MultipartBody.Part.createFormData("avatar", file.name, requestFile) |
| | | } |
| | | |
| | | // 调用更新接口 |
| | | apiService.updateProfile( |
| | | nickname = nicknameBody, |
| | | avatar = avatarPart |
| | | ) |
| | | |
| | | Toast.makeText(this@EditProfileActivity, "保存成功", Toast.LENGTH_SHORT).show() |
| | | finish() |
| | | |
| | | } catch (e: Exception) { |
| | | Toast.makeText(this@EditProfileActivity, "保存失败: ${e.message}", Toast.LENGTH_SHORT).show() |
| | | } finally { |
| | | binding.btnSaveBottom.isEnabled = true |
| | | hideLoading() |
| | | } |
| | | } |
| | | setResult(Activity.RESULT_OK, resultIntent) |
| | | finish() |
| | | } |
| | | |
| | | private fun checkAndRequestPermission() { |
| | |
| | | pickImage.launch(intent) |
| | | } |
| | | |
| | | private fun showLoading() { |
| | | if (loadingDialog == null) { |
| | | loadingDialog = AlertDialog.Builder(this) |
| | | .setView(ProgressBar(this)) |
| | | .setCancelable(false) |
| | | .create() |
| | | } |
| | | loadingDialog?.show() |
| | | } |
| | | |
| | | private fun hideLoading() { |
| | | loadingDialog?.dismiss() |
| | | } |
| | | |
| | | private fun String.toRequestBody(mediaType: String): RequestBody { |
| | | return this.toRequestBody(mediaType.toMediaType()) |
| | | } |
| | | |
| | | private fun File.asRequestBody(mediaType: String): RequestBody { |
| | | return this.asRequestBody(mediaType.toMediaType()) |
| | | } |
| | | |
| | | companion object { |
| | | private const val PERMISSION_REQUEST_CODE = 100 |
| | | } |