diff --git a/app/src/main/java/com/example/tampopo_client/viewmodels/ChatViewModel.java b/app/src/main/java/com/example/tampopo_client/viewmodels/ChatViewModel.java index 80b1fce..3a5592e 100644 --- a/app/src/main/java/com/example/tampopo_client/viewmodels/ChatViewModel.java +++ b/app/src/main/java/com/example/tampopo_client/viewmodels/ChatViewModel.java @@ -9,6 +9,7 @@ import com.example.tampopo_client.models.ChatMessage; import com.example.tampopo_client.models.Chatroom; import com.example.tampopo_client.models.Chatroom; +import com.example.tampopo_client.resources.ActivitiesResource; import com.example.tampopo_client.resources.ChatroomResource; import java.io.IOException; @@ -25,6 +26,7 @@ private final Retrofit retrofit; private final ChatroomResource chatroomResource; + private final ActivitiesResource activitiesResource; // --- LiveData --- private final MutableLiveData chatroomIdLiveData = new MutableLiveData<>(); @@ -58,6 +60,7 @@ .build(); this.chatroomResource = retrofit.create(ChatroomResource.class); + this.activitiesResource = retrofit.create(ActivitiesResource.class); } public void setChatRoomId(String chatRoomId) { @@ -76,6 +79,7 @@ .build(); this.chatroomResource = retrofit.create(ChatroomResource.class); + this.activitiesResource = retrofit.create(ActivitiesResource.class); } @Override @@ -123,6 +127,18 @@ // =============================== + // ユーザーのアクティビティを更新 + // =============================== + private void updateUserActivity(String userId, String token, String activityText) { + try { + Call call = activitiesResource.addActivity(userId, token, activityText); + call.execute(); // 同期実行 + } catch (IOException e) { + Log.e("ChatVM", "updateUserActivity error: " + e.getMessage()); + } + } + + // =============================== // 1. チャットルームに入る(かける側) // =============================== public String enterChatroom(String user0Id, String user1Id, String token) { @@ -131,7 +147,13 @@ Response response = call.execute(); if (response.isSuccessful() && response.body() != null) { Log.d("OK","OK"); - return response.body().getChatRoomId(); + String chatRoomId = response.body().getChatRoomId(); + + // 両方のユーザーのアクティビティを「通話中」に更新 + updateUserActivity(user0Id, token, "通話中"); + updateUserActivity(user1Id, token, "通話中"); + + return chatRoomId; } else { Log.d("Flase","Flase"); return null; @@ -233,6 +255,15 @@ } // =============================== + // 3.5. チャットルーム終了時のアクティビティクリア + // =============================== + public void clearChatActivities(String user0Id, String user1Id, String token) { + // 両方のユーザーのアクティビティを空文字列に更新 + updateUserActivity(user0Id, token, ""); + updateUserActivity(user1Id, token, ""); + } + + // =============================== // 4. チャットルーム削除 // =============================== public void destroyChatroom(String chatroomId, String userId, String token) {