From 956cf29376f122382dfe7f66df65fde696a5a1e9 Mon Sep 17 00:00:00 2001 From: Eric85710 Date: Mon, 4 May 2026 06:33:57 +0800 Subject: [PATCH] auth to server-V45 --- .../detail_UI/setting_Profile_Page.kt | 30 ++++++++++++++----- .../viewmodel/Profile_Viewmodel.kt | 20 +++++++++++-- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/detail_UI/setting_Profile_Page.kt b/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/detail_UI/setting_Profile_Page.kt index c7efed0..52adb6d 100644 --- a/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/detail_UI/setting_Profile_Page.kt +++ b/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/detail_UI/setting_Profile_Page.kt @@ -1,6 +1,9 @@ package com.example.login_v3.home.setting.setting_detail_page.detail_UI 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.background import androidx.compose.foundation.border @@ -88,6 +91,18 @@ fun Loaded_setting_profile_page( var showDialog by remember { mutableStateOf(false) } 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( modifier = Modifier.fillMaxSize() ) { @@ -156,7 +171,12 @@ fun Loaded_setting_profile_page( contentDescription = "Avatar of ${profile.display_name}", modifier = Modifier .size(avatarSize) - .clip(CircleShape), + .clip(CircleShape) + .clickable { + // 假設你拿到了一個新網址 + launcher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)) + } + , contentScale = ContentScale.Crop, placeholder = painterResource(R.drawable.avatar_v1), error = painterResource(R.drawable.avatar_v1) @@ -283,17 +303,13 @@ fun Loaded_setting_profile_page( text = { TextField( value = tempBio, - onValueChange = { - tempBio = it - Log.d("BioDebug", "目前打字內容: $it") - }, + onValueChange = { tempBio = it }, placeholder = { Text("Enter your bio") } ) }, confirmButton = { TextButton(onClick = { - Log.d("BioDebug", "準備送出的 Bio: $tempBio") - viewModel.updateBio(tempBio) // 呼叫你寫好的 API 邏輯 + viewModel.updateProfile(bio = tempBio) // 呼叫你寫好的 API 邏輯 showDialog = false }) { Text("Save", color = MaterialTheme.colorScheme.onBackground) diff --git a/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/viewmodel/Profile_Viewmodel.kt b/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/viewmodel/Profile_Viewmodel.kt index d95f358..14199f6 100644 --- a/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/viewmodel/Profile_Viewmodel.kt +++ b/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/viewmodel/Profile_Viewmodel.kt @@ -51,12 +51,26 @@ class PersonalProfileViewModel @Inject constructor( } //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 { - 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 { - // 更新成功後,可以重新 loadProfile() 或更新本地狀態 fetchProfile() + }.onFailure { + // 處理錯誤 } } }