From f67cf3b81a00f732ca743431258ae6b78f5f40ab Mon Sep 17 00:00:00 2001
From: tj <1378534974@qq.com>
Date: 星期四, 17 四月 2025 15:05:28 +0800
Subject: [PATCH] 11、我的	切换头像  点击切换头像没有显示允许存储权限的窗口,华为的手机目前有 49、首页	实时刷新  点击全部取件或其他分类后,回到上一层,内容没有刷新(5个分类) 52、数据统计	数据统计  1.周月年的柱状图统计逻辑需要修改为只统计快递类的数据  2.按年的图形统计,右下方加上图示说明 53、首页	首页登录  点击用户协议、隐私政策无反应

---
 app/src/main/java/com/example/firstapp/ui/profile/EditProfileActivity.kt |  105 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 91 insertions(+), 14 deletions(-)

diff --git a/app/src/main/java/com/example/firstapp/ui/profile/EditProfileActivity.kt b/app/src/main/java/com/example/firstapp/ui/profile/EditProfileActivity.kt
index 49d435c..608eef6 100644
--- a/app/src/main/java/com/example/firstapp/ui/profile/EditProfileActivity.kt
+++ b/app/src/main/java/com/example/firstapp/ui/profile/EditProfileActivity.kt
@@ -18,8 +18,11 @@
 import androidx.core.content.ContextCompat
 import androidx.lifecycle.lifecycleScope
 import com.bumptech.glide.Glide
+import com.example.firstapp.database.response.UserInfo
 import com.example.firstapp.databinding.ActivityEditProfileBinding
 import com.example.firstapp.database.service.ApiService
+import com.example.firstapp.database.service.RetrofitClient
+import com.example.firstapp.utils.PreferencesManager
 import kotlinx.coroutines.launch
 import java.io.File
 import okhttp3.MediaType.Companion.toMediaType
@@ -31,18 +34,43 @@
 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) {
-            result.data?.data?.let { uri ->
-                selectedImageUri = uri
-                Glide.with(this)
-                    .load(uri)
-                    .circleCrop()
-                    .into(binding.ivAvatar)
-            }
+//    private val pickImage = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
+//        if (result.resultCode == Activity.RESULT_OK) {
+//            result.data?.data?.let { uri ->
+//                selectedImageUri = uri
+//                Glide.with(this)
+//                    .load(uri)
+//                    .circleCrop()
+//                    .into(binding.ivAvatar)
+//            }
+//        }
+//    }
+
+    private val pickImageLauncher = registerForActivityResult(
+        ActivityResultContracts.GetContent()
+    ) { uri: Uri? ->
+        uri?.let {
+            selectedImageUri = uri
+            // 这里直接处理选中的头像
+            Glide.with(this)
+                .load(it)
+                .into(binding.ivAvatar) // 替换成你的 ImageView id
+
+            // 如果需要上传可以用 contentResolver.openInputStream(uri)
+        }
+    }
+
+
+    // 👇 就放在这里
+    private val permissionLauncher = registerForActivityResult(
+        ActivityResultContracts.RequestPermission()
+    ) { isGranted: Boolean ->
+        if (isGranted) {
+            openGallery()
+        } else {
+            Toast.makeText(this, "需要权限才能选择头像", Toast.LENGTH_SHORT).show()
         }
     }
 
@@ -76,13 +104,29 @@
         }
 
         binding.ivAvatar.setOnClickListener {
-            checkAndRequestPermission()
+//            checkAndRequestPermission()
+            checkStoragePermission()
         }
 
         binding.btnSaveBottom.setOnClickListener {
             saveAndFinish()
         }
     }
+
+    fun checkStoragePermission() {
+        val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+            Manifest.permission.READ_MEDIA_IMAGES
+        } else {
+            Manifest.permission.READ_EXTERNAL_STORAGE
+        }
+
+        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
+            permissionLauncher.launch(permission)
+        } else {
+            openGallery()
+        }
+    }
+
 
     private fun saveAndFinish() {
         lifecycleScope.launch {
@@ -114,12 +158,38 @@
                 }
 
                 // 调用更新接口
-                apiService.updateProfile(
+                RetrofitClient.apiService.updateProfile(
                     nickname = nicknameBody,
                     avatar = avatarPart
                 )
 
                 Toast.makeText(this@EditProfileActivity, "保存成功", Toast.LENGTH_SHORT).show()
+                // 更新用户信息
+                // 从本地获取保存的手机号
+                val savedPhone = PreferencesManager.getPhone()
+                if (savedPhone.isNullOrEmpty()) {
+                    Toast.makeText(this@EditProfileActivity, "用户未登录", Toast.LENGTH_SHORT).show()
+                    return@launch
+                }
+                val response = RetrofitClient.apiService.getUserInfo(savedPhone)
+                if (response.code == "0" && response.data != null) {
+                    // 保存用户信息
+                    val userInfo:UserInfo = response.data
+                    // 获取传入的数据
+                    val currentNickname = userInfo.name
+                    val currentAvatarUrl = userInfo.cover
+
+                    // 设置当前数据
+                    binding.etNickname.setText(currentNickname)
+                    if (!currentAvatarUrl.isNullOrEmpty()) {
+                        Glide.with(this@EditProfileActivity)
+                            .load(currentAvatarUrl)
+                            .circleCrop()
+                            .into(binding.ivAvatar)
+                    }
+
+                }
+
                 finish()
 
             } catch (e: Exception) {
@@ -166,9 +236,16 @@
         }
     }
 
-    private fun openGallery() {
+    private fun openGallery_bak() {
         val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
-        pickImage.launch(intent)
+//        pickImage.launch(intent)
+    }
+
+    private fun openGallery() {
+        pickImageLauncher.launch("image/*")
+//        val intent = Intent(Intent.ACTION_PICK)
+//        intent.type = "image/*"
+//        startActivityForResult(intent, 1001)
     }
 
     private fun showLoading() {

--
Gitblit v1.9.3