auth to server-V45

This commit is contained in:
Eric85710 2026-05-04 06:33:57 +08:00
parent 396dcaf1f2
commit 956cf29376
2 changed files with 40 additions and 10 deletions

View file

@ -1,6 +1,9 @@
package com.example.login_v3.home.setting.setting_detail_page.detail_UI package com.example.login_v3.home.setting.setting_detail_page.detail_UI
import android.util.Log import android.util.Log
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.border import androidx.compose.foundation.border
@ -88,6 +91,18 @@ fun Loaded_setting_profile_page(
var showDialog by remember { mutableStateOf(false) } var showDialog by remember { mutableStateOf(false) }
var tempBio by remember { mutableStateOf(profile.bio ?: "") } var tempBio by remember { mutableStateOf(profile.bio ?: "") }
//image chooser
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickVisualMedia()
) { uri ->
// 2. 當使用者選完照片後,會回傳一個 Uri
if (uri != null) {
// 直接把本地 Uri 轉成字串丟進去 (僅限本地 Demo,重整 App 後會失效)
viewModel.updateProfile(avatarUrl = uri.toString())
}
}
Column( Column(
modifier = Modifier.fillMaxSize() modifier = Modifier.fillMaxSize()
) { ) {
@ -156,7 +171,12 @@ fun Loaded_setting_profile_page(
contentDescription = "Avatar of ${profile.display_name}", contentDescription = "Avatar of ${profile.display_name}",
modifier = Modifier modifier = Modifier
.size(avatarSize) .size(avatarSize)
.clip(CircleShape), .clip(CircleShape)
.clickable {
// 假設你拿到了一個新網址
launcher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
}
,
contentScale = ContentScale.Crop, contentScale = ContentScale.Crop,
placeholder = painterResource(R.drawable.avatar_v1), placeholder = painterResource(R.drawable.avatar_v1),
error = painterResource(R.drawable.avatar_v1) error = painterResource(R.drawable.avatar_v1)
@ -283,17 +303,13 @@ fun Loaded_setting_profile_page(
text = { text = {
TextField( TextField(
value = tempBio, value = tempBio,
onValueChange = { onValueChange = { tempBio = it },
tempBio = it
Log.d("BioDebug", "目前打字內容: $it")
},
placeholder = { Text("Enter your bio") } placeholder = { Text("Enter your bio") }
) )
}, },
confirmButton = { confirmButton = {
TextButton(onClick = { TextButton(onClick = {
Log.d("BioDebug", "準備送出的 Bio: $tempBio") viewModel.updateProfile(bio = tempBio) // 呼叫你寫好的 API 邏輯
viewModel.updateBio(tempBio) // 呼叫你寫好的 API 邏輯
showDialog = false showDialog = false
}) { }) {
Text("Save", color = MaterialTheme.colorScheme.onBackground) Text("Save", color = MaterialTheme.colorScheme.onBackground)

View file

@ -51,12 +51,26 @@ class PersonalProfileViewModel @Inject constructor(
} }
//update profile //update profile
fun updateBio(newBio: String) { fun updateProfile(
displayName: String? = null,
bio: String? = null,
avatarUrl: String? = null,
bannerUrl: String? = null,
status: String? = null
) {
viewModelScope.launch { viewModelScope.launch {
val request = UserProfileUpdateRequest(bio = newBio) val request = UserProfileUpdateRequest(
display_name = displayName,
bio = bio,
avatar_url = avatarUrl,
banner_url = bannerUrl,
status = status
)
repository.patchUserProfile(request).onSuccess { repository.patchUserProfile(request).onSuccess {
// 更新成功後,可以重新 loadProfile() 或更新本地狀態
fetchProfile() fetchProfile()
}.onFailure {
// 處理錯誤
} }
} }
} }