auth to server-V73

This commit is contained in:
Eric85710 2026-05-08 17:03:57 +08:00
parent bca3cc9588
commit b1d892f8f4
3 changed files with 29 additions and 13 deletions

View file

@ -55,12 +55,14 @@ interface TecnologiaApi {
suspend fun getFriendsPending(): PendingFriendsResponse suspend fun getFriendsPending(): PendingFriendsResponse
//accept and reject //accept and reject
@POST("api/friends/{id}/accept") // accept and reject
@POST("api/friends/{fromUserId}/accept")
suspend fun acceptFriendRequest( suspend fun acceptFriendRequest(
@Path("id") friendshipId: String @Path("fromUserId") fromUserId: String
): Response<Unit> ): Response<Unit>
@POST("api/friends/{id}/reject")
@POST("api/friends/{fromUserId}/reject")
suspend fun rejectFriendRequest( suspend fun rejectFriendRequest(
@Path("id") friendshipId: String @Path("fromUserId") fromUserId: String
): Response<Unit> ): Response<Unit>
} }

View file

@ -84,8 +84,13 @@ fun Message_contact_list(
items(uiState.pendingRequests) { request -> items(uiState.pendingRequests) { request ->
PendingFriendRow( PendingFriendRow(
request, request,
onAccept = { id -> contactListViewModel.acceptFriend(id) }, isLoading = uiState.isLoading,
onReject = { id -> /* 實作邏輯同 accept */ } onAccept = { _ ->
request.fromUserId?.let { id -> contactListViewModel.acceptFriend(id) }
},
onReject = { _ ->
request.fromUserId?.let { id -> contactListViewModel.rejectFriend(id) }
}
) )
} }
item { HorizontalDivider(modifier = Modifier.padding(vertical = 12.dp)) } item { HorizontalDivider(modifier = Modifier.padding(vertical = 12.dp)) }
@ -158,6 +163,7 @@ fun FriendRow(friend: Friend) {
@Composable @Composable
fun PendingFriendRow( fun PendingFriendRow(
request: PendingFriendApiModel, request: PendingFriendApiModel,
isLoading: Boolean,
onAccept: (String) -> Unit, // 增加回呼 onAccept: (String) -> Unit, // 增加回呼
onReject: (String) -> Unit // 增加回呼 onReject: (String) -> Unit // 增加回呼
) { ) {
@ -175,14 +181,16 @@ fun PendingFriendRow(
}, },
trailingContent = { trailingContent = {
Row { Row {
IconButton(onClick = { IconButton(
request.friendshipId?.let { onReject(it) } onClick = { request.friendshipId?.let { onReject(it) } },
}) { enabled = !isLoading // 正在載入時禁用
) {
Icon(Icons.Default.Close, contentDescription = "拒絕", tint = Color.Red) Icon(Icons.Default.Close, contentDescription = "拒絕", tint = Color.Red)
} }
IconButton(onClick = { IconButton(
request.friendshipId?.let { onAccept(it) } onClick = { request.friendshipId?.let { onAccept(it) } },
}) { enabled = !isLoading // 正在載入時禁用
) {
Icon(Icons.Default.Check, contentDescription = "接受", tint = Color.Green) Icon(Icons.Default.Check, contentDescription = "接受", tint = Color.Green)
} }
} }

View file

@ -58,7 +58,10 @@ class ContactListViewModel @Inject constructor(
//accept and reject //accept and reject
fun acceptFriend(friendshipId: String) { fun acceptFriend(friendshipId: String) {
viewModelScope.launch { viewModelScope.launch {
// 這裡可以選擇性開啟 loading
//loading
_uiState.update { it.copy(isLoading = true) }
val result = repository.acceptFriendRequest(friendshipId) val result = repository.acceptFriendRequest(friendshipId)
result.onSuccess { result.onSuccess {
@ -72,6 +75,9 @@ class ContactListViewModel @Inject constructor(
} }
fun rejectFriend(friendshipId: String) { fun rejectFriend(friendshipId: String) {
viewModelScope.launch { viewModelScope.launch {
_uiState.update { it.copy(isLoading = true) }
val result = repository.rejectFriendRequest(friendshipId) val result = repository.rejectFriendRequest(friendshipId)
result.onSuccess { result.onSuccess {