auth to server-V72
This commit is contained in:
parent
fcfc70c386
commit
bca3cc9588
4 changed files with 67 additions and 11 deletions
|
|
@ -53,8 +53,14 @@ interface TecnologiaApi {
|
||||||
suspend fun getFriends(): FriendListResponse
|
suspend fun getFriends(): FriendListResponse
|
||||||
@GET("api/friends/pending")
|
@GET("api/friends/pending")
|
||||||
suspend fun getFriendsPending(): PendingFriendsResponse
|
suspend fun getFriendsPending(): PendingFriendsResponse
|
||||||
|
|
||||||
|
//accept and reject
|
||||||
@POST("api/friends/{id}/accept")
|
@POST("api/friends/{id}/accept")
|
||||||
suspend fun acceptFriendRequest(
|
suspend fun acceptFriendRequest(
|
||||||
@Path("id") friendshipId: String
|
@Path("id") friendshipId: String
|
||||||
): Response<Unit>
|
): Response<Unit>
|
||||||
|
@POST("api/friends/{id}/reject")
|
||||||
|
suspend fun rejectFriendRequest(
|
||||||
|
@Path("id") friendshipId: String
|
||||||
|
): Response<Unit>
|
||||||
}
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ package com.example.login_v3.data.repository.basic
|
||||||
import com.example.login_v3.data.api.TecnologiaApi
|
import com.example.login_v3.data.api.TecnologiaApi
|
||||||
import com.example.login_v3.data.api.api_class.Friend
|
import com.example.login_v3.data.api.api_class.Friend
|
||||||
import com.example.login_v3.data.api.api_class.PendingFriendApiModel
|
import com.example.login_v3.data.api.api_class.PendingFriendApiModel
|
||||||
|
import retrofit2.Response
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class FriendsRepository @Inject constructor(
|
class FriendsRepository @Inject constructor(
|
||||||
|
|
@ -33,14 +34,22 @@ class FriendsRepository @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun acceptFriendRequest(friendshipId: String): Result<Unit> {
|
// 接受
|
||||||
|
suspend fun acceptFriendRequest(id: String) = safeApiCall {
|
||||||
|
apiService.acceptFriendRequest(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 拒絕
|
||||||
|
suspend fun rejectFriendRequest(id: String) = safeApiCall {
|
||||||
|
apiService.rejectFriendRequest(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 統一處理 Response<Unit> 的工具函式
|
||||||
|
private suspend fun safeApiCall(call: suspend () -> Response<Unit>): Result<Unit> {
|
||||||
return try {
|
return try {
|
||||||
val response = apiService.acceptFriendRequest(friendshipId)
|
val response = call()
|
||||||
if (response.isSuccessful) {
|
if (response.isSuccessful) Result.success(Unit)
|
||||||
Result.success(Unit)
|
else Result.failure(Exception("API 錯誤: ${response.code()}"))
|
||||||
} else {
|
|
||||||
Result.failure(Exception("接受失敗: ${response.code()}"))
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Result.failure(e)
|
Result.failure(e)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -82,7 +82,11 @@ fun Message_contact_list(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
items(uiState.pendingRequests) { request ->
|
items(uiState.pendingRequests) { request ->
|
||||||
PendingFriendRow(request)
|
PendingFriendRow(
|
||||||
|
request,
|
||||||
|
onAccept = { id -> contactListViewModel.acceptFriend(id) },
|
||||||
|
onReject = { id -> /* 實作邏輯同 accept */ }
|
||||||
|
)
|
||||||
}
|
}
|
||||||
item { HorizontalDivider(modifier = Modifier.padding(vertical = 12.dp)) }
|
item { HorizontalDivider(modifier = Modifier.padding(vertical = 12.dp)) }
|
||||||
}
|
}
|
||||||
|
|
@ -152,7 +156,11 @@ fun FriendRow(friend: Friend) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun PendingFriendRow(request: PendingFriendApiModel) {
|
fun PendingFriendRow(
|
||||||
|
request: PendingFriendApiModel,
|
||||||
|
onAccept: (String) -> Unit, // 增加回呼
|
||||||
|
onReject: (String) -> Unit // 增加回呼
|
||||||
|
) {
|
||||||
ListItem(
|
ListItem(
|
||||||
headlineContent = { Text("${request.displayName}") },
|
headlineContent = { Text("${request.displayName}") },
|
||||||
supportingContent = { Text("想加你為好友") },
|
supportingContent = { Text("想加你為好友") },
|
||||||
|
|
@ -167,10 +175,14 @@ fun PendingFriendRow(request: PendingFriendApiModel) {
|
||||||
},
|
},
|
||||||
trailingContent = {
|
trailingContent = {
|
||||||
Row {
|
Row {
|
||||||
IconButton(onClick = { /* TODO: 呼叫 ViewModel 拒絕 */ }) {
|
IconButton(onClick = {
|
||||||
|
request.friendshipId?.let { onReject(it) }
|
||||||
|
}) {
|
||||||
Icon(Icons.Default.Close, contentDescription = "拒絕", tint = Color.Red)
|
Icon(Icons.Default.Close, contentDescription = "拒絕", tint = Color.Red)
|
||||||
}
|
}
|
||||||
IconButton(onClick = { /* TODO: 呼叫 ViewModel 接受 */ }) {
|
IconButton(onClick = {
|
||||||
|
request.friendshipId?.let { onAccept(it) }
|
||||||
|
}) {
|
||||||
Icon(Icons.Default.Check, contentDescription = "接受", tint = Color.Green)
|
Icon(Icons.Default.Check, contentDescription = "接受", tint = Color.Green)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -53,4 +53,33 @@ class ContactListViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//accept and reject
|
||||||
|
fun acceptFriend(friendshipId: String) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
// 這裡可以選擇性開啟 loading
|
||||||
|
val result = repository.acceptFriendRequest(friendshipId)
|
||||||
|
|
||||||
|
result.onSuccess {
|
||||||
|
// 關鍵:重新整理資料,讓 UI 自動更新
|
||||||
|
refreshAll()
|
||||||
|
}.onFailure { error ->
|
||||||
|
Log.e("DEBUG_API", "接受請求錯誤: ${error.message}")
|
||||||
|
_uiState.update { it.copy(error = "無法接受請求:${error.message}") }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fun rejectFriend(friendshipId: String) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
val result = repository.rejectFriendRequest(friendshipId)
|
||||||
|
|
||||||
|
result.onSuccess {
|
||||||
|
refreshAll() // 刷新列表,該筆請求會消失
|
||||||
|
}.onFailure { error ->
|
||||||
|
Log.e("DEBUG_API", "拒絕請求錯誤: ${error.message}")
|
||||||
|
_uiState.update { it.copy(error = "無法拒絕請求:${error.message}") }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue