| | |
| | | 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 |
| | |
| | | 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 -> |
| | |
| | | } |
| | | |
| | | // 调用更新接口 |
| | | 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) { |