From a9475e8c784d842e2719b369f2079cef83b8dc31 Mon Sep 17 00:00:00 2001 From: Eric85710 Date: Sat, 2 May 2026 16:56:34 +0800 Subject: [PATCH] auth to server-V35 --- .../login_v3/data/api/Main_API_Service.kt | 6 +++ .../data/api/api_class/Personal_Profile.kt | 9 +++++ .../basic/Personal_Profile_Repository.kt | 40 ++++++++++++++----- .../viewmodel/Profile_Viewmodel.kt | 19 +++++---- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/example/login_v3/data/api/Main_API_Service.kt b/app/src/main/java/com/example/login_v3/data/api/Main_API_Service.kt index 2a8ef5c..70d1e23 100644 --- a/app/src/main/java/com/example/login_v3/data/api/Main_API_Service.kt +++ b/app/src/main/java/com/example/login_v3/data/api/Main_API_Service.kt @@ -5,6 +5,7 @@ import retrofit2.Response import retrofit2.http.Body import retrofit2.http.GET import retrofit2.http.Header +import retrofit2.http.PATCH import retrofit2.http.POST interface TecnologiaApi { @@ -24,6 +25,11 @@ interface TecnologiaApi { @GET("api/me") suspend fun getUserProfile(): Response + @PATCH("me") + suspend fun updateProfile( + @Body request: UserProfileUpdateRequest + ): Response + // 發送好友請求 @POST("api/friends/request") suspend fun sendFriendRequest( diff --git a/app/src/main/java/com/example/login_v3/data/api/api_class/Personal_Profile.kt b/app/src/main/java/com/example/login_v3/data/api/api_class/Personal_Profile.kt index 9357ca0..1e4db5a 100644 --- a/app/src/main/java/com/example/login_v3/data/api/api_class/Personal_Profile.kt +++ b/app/src/main/java/com/example/login_v3/data/api/api_class/Personal_Profile.kt @@ -16,4 +16,13 @@ data class UserProfile( val is_verified: Boolean?, val last_login: String?, val created_at: String? +) + +@JsonClass(generateAdapter = true) +data class UserProfileUpdateRequest( + val avatar_url: String? = null, + val banner_url: String? = null, + val bio: String? = null, + val display_name: String? = null, + val status: String? = null ) \ No newline at end of file diff --git a/app/src/main/java/com/example/login_v3/data/repository/basic/Personal_Profile_Repository.kt b/app/src/main/java/com/example/login_v3/data/repository/basic/Personal_Profile_Repository.kt index bfbc817..8c335c9 100644 --- a/app/src/main/java/com/example/login_v3/data/repository/basic/Personal_Profile_Repository.kt +++ b/app/src/main/java/com/example/login_v3/data/repository/basic/Personal_Profile_Repository.kt @@ -2,19 +2,39 @@ package com.example.login_v3.data.repository.basic import com.example.login_v3.data.api.TecnologiaApi import com.example.login_v3.data.api.api_class.UserProfile +import com.example.login_v3.data.api.api_class.UserProfileUpdateRequest import javax.inject.Inject class PersonalProfileRepository @Inject constructor( - private val api: TecnologiaApi + private val api: TecnologiaApi // 確保你的 API interface 裡有 getUserProfile 和 updateProfile ) { - suspend fun getMyProfile(): UserProfile { - val response = api.getUserProfile() - if (response.isSuccessful) { - // body()!! 雖然簡單,但建議確保 API 回傳格式正確 - return response.body() ?: throw Exception("回傳資料為空") - } else { - // 這裡可以根據 response.code() 給出更詳細的錯誤訊息 - throw Exception("讀取失敗 (${response.code()})") + // 取得資料 + suspend fun getMyProfile(): Result { + return try { + val response = api.getUserProfile() + if (response.isSuccessful) { + val body = response.body() + if (body != null) Result.success(body) + else Result.failure(Exception("回傳資料為空")) + } else { + Result.failure(Exception("讀取失敗 (${response.code()})")) + } + } catch (e: Exception) { + Result.failure(e) } } -} \ No newline at end of file + + // 更新資料 (PATCH) + suspend fun patchUserProfile(request: UserProfileUpdateRequest): Result { + return try { + val response = api.updateProfile(request) + if (response.isSuccessful) { + Result.success(Unit) + } else { + Result.failure(Exception("更新失敗: ${response.code()}")) + } + } catch (e: Exception) { + Result.failure(e) + } + } +} 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 74c64f3..1edce9a 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 @@ -34,14 +34,17 @@ class PersonalProfileViewModel @Inject constructor( fun fetchProfile() { viewModelScope.launch { _uiState.value = ProfileUiState.Loading - try { - val profile = repository.getMyProfile() - _uiState.value = ProfileUiState.Success(profile) - } catch (e: Exception) { - // 如果是 401,Interceptor 會處理登出, - // 這裡處理一般的網路錯誤或 API 錯誤 - _uiState.value = ProfileUiState.Error(e.localizedMessage ?: "未知錯誤") - } + + val result = repository.getMyProfile() + + result + .onSuccess { profile -> + _uiState.value = ProfileUiState.Success(profile) + } + .onFailure { e -> + _uiState.value = + ProfileUiState.Error(e.localizedMessage ?: "未知錯誤") + } } } } \ No newline at end of file