auth to server-V49

This commit is contained in:
Eric85710 2026-05-04 15:50:27 +08:00
parent c8f364433f
commit fcad410119
3 changed files with 32 additions and 15 deletions

View file

@ -1,5 +1,6 @@
package com.example.login_v3.data.api.api_class package com.example.login_v3.data.api.api_class
import com.example.login_v3.R
import com.squareup.moshi.Json import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass import com.squareup.moshi.JsonClass
@ -27,3 +28,12 @@ data class UserProfileUpdateRequest(
val display_name: String? = null, val display_name: String? = null,
val status: String? = null val status: String? = null
) )
val UserProfile.fullAvatarUrl: Any?
get() = if (avatar_url.isNullOrBlank()) {
R.drawable.avatar_v1 // 如果沒網址,直接回傳預設圖片資源
} else if (avatar_url.startsWith("http")) {
avatar_url // 已經是完整網址就直接用
} else {
"http://192.168.0.217$avatar_url" // 否則補上 Base URL
}

View file

@ -43,6 +43,7 @@ class PersonalProfileRepository @Inject constructor(
suspend fun uploadAvatar(avatarPart: MultipartBody.Part): Result<Unit> { suspend fun uploadAvatar(avatarPart: MultipartBody.Part): Result<Unit> {
return try { return try {
val response = api.uploadAvatar(avatarPart) val response = api.uploadAvatar(avatarPart)
if (response.isSuccessful) { if (response.isSuccessful) {
Result.success(Unit) Result.success(Unit)
} else { } else {

View file

@ -21,10 +21,13 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material3.AlertDialog import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Divider import androidx.compose.material3.Divider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextButton import androidx.compose.material3.TextButton
@ -51,6 +54,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage import coil.compose.AsyncImage
import com.example.login_v3.R import com.example.login_v3.R
import com.example.login_v3.data.api.api_class.UserProfile import com.example.login_v3.data.api.api_class.UserProfile
import com.example.login_v3.data.api.api_class.fullAvatarUrl
import com.example.login_v3.home.setting.setting_detail_page.viewmodel.PersonalProfileViewModel import com.example.login_v3.home.setting.setting_detail_page.viewmodel.PersonalProfileViewModel
import com.example.login_v3.home.setting.setting_detail_page.viewmodel.ProfileUiState import com.example.login_v3.home.setting.setting_detail_page.viewmodel.ProfileUiState
import com.example.login_v3.ui.theme.main_orange import com.example.login_v3.ui.theme.main_orange
@ -101,6 +105,7 @@ fun Loaded_setting_profile_page(
if (uri != null) { if (uri != null) {
// ✅ 修正:呼叫上傳圖片的函式,而不是更新網址字串的函式 // ✅ 修正:呼叫上傳圖片的函式,而不是更新網址字串的函式
viewModel.uploadAvatar(context, uri) viewModel.uploadAvatar(context, uri)
Log.d("AvatarURL", "目前的圖片網址為: ${profile.avatar_url}")
} }
} }
@ -166,9 +171,10 @@ fun Loaded_setting_profile_page(
){ ){
Spacer(modifier = Modifier.width(36.dp)) Spacer(modifier = Modifier.width(36.dp))
//user avatar //user avatar
AsyncImage( AsyncImage(
model = profile.avatar_url, model = profile.fullAvatarUrl,
contentDescription = "Avatar of ${profile.display_name}", contentDescription = "Avatar of ${profile.display_name}",
modifier = Modifier modifier = Modifier
.size(avatarSize) .size(avatarSize)
@ -179,7 +185,6 @@ fun Loaded_setting_profile_page(
} }
, ,
contentScale = ContentScale.Crop, contentScale = ContentScale.Crop,
placeholder = painterResource(R.drawable.avatar_v1),
error = painterResource(R.drawable.avatar_v1) error = painterResource(R.drawable.avatar_v1)
) )
@ -274,23 +279,24 @@ fun Loaded_setting_profile_page(
modifier = Modifier.padding(vertical = 8.dp) modifier = Modifier.padding(vertical = 8.dp)
) )
//intro Row() {
Box(
modifier = Modifier
.fillMaxWidth()
.padding(top = 8.dp)
.clickable {
tempBio = profile.bio ?: "" // 開啟時同步目前的 bio
showDialog = true
}
.border(1.dp, Color.White.copy(alpha = 0.3f), RoundedCornerShape(8.dp))
.padding(16.dp)
) {
// 這裡改用 profile.bio // 這裡改用 profile.bio
Text( Text(
text = if (profile.bio.isNullOrBlank()) "Add a bio..." else profile.bio, text = if (profile.bio.isNullOrBlank()) "Add a bio..." else profile.bio,
color = if (profile.bio.isNullOrBlank()) Color.Gray else MaterialTheme.colorScheme.onBackground color = if (profile.bio.isNullOrBlank()) Color.Gray else MaterialTheme.colorScheme.onBackground
) )
Icon(
imageVector = Icons.Default.Edit,
contentDescription = "Edit bio",
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier
.size(20.dp)
.clickable {
tempBio = profile.bio ?: "" // 開啟時同步目前的 bio
showDialog = true
}
)
} }
// 彈出編輯視窗 // 彈出編輯視窗