diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 09ad5d2..40840fd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -61,6 +61,9 @@ + 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 1cf6c18..1b360d7 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 @@ -57,7 +57,7 @@ this.chatroomResource = retrofit.create(ChatroomResource.class); } - public ChatViewModel(String userId, String token,String chatroomId) { + public ChatViewModel(String userId, String token, String chatroomId) { this.userId = userId; this.token = token; this.chatroomId = chatroomId; @@ -81,19 +81,36 @@ notificationListeners.forEach(NotificationListener::onNotificationReceived); }, 10_000); // 10秒後に通知 } - foundChatroom(userId); + foundChatroom(userId,token); loadLatestMessage(chatroomId, userId, token); }; } // getter - public MutableLiveData getChatroomIdLiveData() { return chatroomIdLiveData; } - public MutableLiveData> getChatMessages() { return chatMessages; } - public MutableLiveData getLatestMessageLiveData() { return latestMessage; } - public MutableLiveData getChatroomClosed() { return chatroomClosed; } - public MutableLiveData getChatFriendToMeLiveData() { return chatFriendToMeLiveData; } - public MutableLiveData getChatToFriendLiveData() { return chatToFriendLiveData; } + public MutableLiveData getChatroomIdLiveData() { + return chatroomIdLiveData; + } + + public MutableLiveData> getChatMessages() { + return chatMessages; + } + + public MutableLiveData getLatestMessageLiveData() { + return latestMessage; + } + + public MutableLiveData getChatroomClosed() { + return chatroomClosed; + } + + public MutableLiveData getChatFriendToMeLiveData() { + return chatFriendToMeLiveData; + } + + public MutableLiveData getChatToFriendLiveData() { + return chatToFriendLiveData; + } // =============================== // 1. チャットルームに入る(かける側) @@ -117,8 +134,8 @@ } // 1.5 自分がchatroomに入っているのか確認する - public void foundChatroom(String userId) { - Call call = chatroomResource.getMyChatroom(userId); + public void foundChatroom(String userId, String token) { + Call call = chatroomResource.getMyChatroom(userId, token); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { @@ -144,7 +161,7 @@ // 1.55 現在通話中のペアを確認 // =============================== public void fetchActiveChatPair(String userId, String token) { - Call call = chatroomResource.getMyChatroom(userId); + Call call = chatroomResource.getMyChatroom(userId, token); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { @@ -247,6 +264,7 @@ } // ===== ここから Handlerループ部分をそのまま追記 ===== + /** * 10秒ごとにサーバー確認を行う */ @@ -277,3 +295,4 @@ isChecking = false; } } +// \ No newline at end of file diff --git a/app/src/main/java/com/example/tampopo_client/viewmodels/UserViewModel.java b/app/src/main/java/com/example/tampopo_client/viewmodels/UserViewModel.java index 1ddbd4b..15936e6 100644 --- a/app/src/main/java/com/example/tampopo_client/viewmodels/UserViewModel.java +++ b/app/src/main/java/com/example/tampopo_client/viewmodels/UserViewModel.java @@ -7,6 +7,8 @@ import com.example.tampopo_client.models.User; import com.example.tampopo_client.resources.UserResource; +import java.io.IOException; + import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -105,24 +107,55 @@ } }); } + //ニックネーム + public String getNickname(String id) { + Call call = userResource.getName(id); + try { + Response response = call.execute(); + + if (response.isSuccessful()) { + System.out.println(response.code()); + return response.body(); + } else { + System.out.println(response.code()); + return null; + } + } catch (IOException e) { + throw new RuntimeException(e); + } +// + } //アイコン - public void getIcon(String id) { + public String getIcon(String id) { Call call = userResource.getIcon(id); - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful()) { - icon.setValue(response.body()); - System.out.println(response.code()); - } else { - System.out.println(response.code()); - } + try { + Response response = call.execute(); + + if (response.isSuccessful()) { + System.out.println(response.code()); + return response.body(); + } else { + System.out.println(response.code()); + return null; } - @Override public void onFailure(Call call, Throwable t) { - System.out.println("エラー: " + t.getMessage()); - } - }); + } catch (IOException e) { + throw new RuntimeException(e); + } +// call.enqueue(new Callback() { +// @Override +// public void onResponse(Call call, Response response) { +// if (response.isSuccessful()) { +// icon.setValue(response.body()); +// System.out.println(response.code()); +// } else { +// System.out.println(response.code()); +// } +// } +// @Override public void onFailure(Call call, Throwable t) { +// System.out.println("エラー: " + t.getMessage()); +// } +// }); } public void updateIcon(String id, String newIcon, String token) { diff --git a/app/src/main/java/com/example/tampopo_client/views/ChatActivity.java b/app/src/main/java/com/example/tampopo_client/views/ChatActivity.java index 37bf1b0..48ac410 100644 --- a/app/src/main/java/com/example/tampopo_client/views/ChatActivity.java +++ b/app/src/main/java/com/example/tampopo_client/views/ChatActivity.java @@ -2,6 +2,7 @@ import android.content.Intent; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; @@ -29,6 +30,8 @@ Tampopo tampopo; + + @Override protected void onCreate(Bundle savedInstanceState) { tampopo = (Tampopo)getApplication(); @@ -60,6 +63,30 @@ // } // }); + Intent intent = getIntent(); + String friendId = intent.getStringExtra("friendId"); + String chatroomId = intent.getStringExtra("chatroomId"); + + Log.d("ChatDebug", "チャット相手ID: " + friendId); + Log.d("ChatDebug", "チャットルームID: " + chatroomId); + + String myId = tampopo.getUserId(); + String token = tampopo.getToken(); + chatroomViewModel.enterChatroom(myId, friendId, token); + + if (friendId == null || friendId.isEmpty()) { + Log.e("ChatDebug","Error: friendIdがnullです"); + } + + chatroomViewModel.getChatroomIdLiveData().observe(this, id -> { + if (id != null) { + tampopo.setChatroomId(id); + System.out.println("チャットルーム作成完了! ID: " + id); + } else { + System.out.println("チャットルーム作成に失敗しました"); + } + }); + //メッセージ送信 sendButton.setOnClickListener(new OnClickListener() { //sendButtonがクリックされたときの処理 @@ -70,7 +97,7 @@ String senderId = tampopo.getUserId(); String token = tampopo.getToken(); String chatroomId = tampopo.getChatroomId(); - chatroomViewModel.sendMessage(chatroomId, senderId, senderText, token); + chatroomViewModel.sendMessage(chatroomId, senderId,senderText, token); senderMessage.setText(senderText);//自分のメッセージを送信欄に表示 } } @@ -109,3 +136,4 @@ } } +// \ No newline at end of file diff --git a/app/src/main/java/com/example/tampopo_client/views/MainActivity.java b/app/src/main/java/com/example/tampopo_client/views/MainActivity.java index 4ae89c3..f2fa84a 100644 --- a/app/src/main/java/com/example/tampopo_client/views/MainActivity.java +++ b/app/src/main/java/com/example/tampopo_client/views/MainActivity.java @@ -46,6 +46,7 @@ import com.example.tampopo_client.viewmodels.ChatViewModel; import com.example.tampopo_client.viewmodels.NotificationListener; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -63,11 +64,13 @@ private Map userViews = new HashMap<>(); ActivityViewModel activityViewModel; - private UserViewModel userViewModel; + UserViewModel userViewModel; Tampopo tampopo; //追加しました! private ChatViewModel chatViewModel; + private final List recentUpdatedFriends = new ArrayList<>(); // 最新6人 + @Override @@ -133,6 +136,7 @@ // }); MutableLiveData> friendsLiveData = activityViewModel.getFriendUserIdsLiveData(); + friendsLiveData.observe(this, new Observer>() { @Override public void onChanged(List friends) { @@ -144,6 +148,8 @@ for (String friendId: userViews.keySet()) { final String updateFriendId = friendId; MutableLiveData> activitiesLiveData = activityViewModel.getActivitiesLiveDataFromUserId(updateFriendId); + //MutableLiveData> activitiesLiveData = activityViewModel.getActivitiesLiveDataFromUserId(updateFriendId); + FriendIconView friendView = userViews.get(friendId); if(friendView == null) continue; //activitiesLiveData.observeForever(friendView); @@ -157,6 +163,39 @@ Activity latest = activities.get(activities.size() - 1); userView.setComment(latest.getText()); } + //アイコンとニックネーム情報をとってくる + new Thread(() -> { + String nickname = userViewModel.getNickname(updateFriendId); + String iconUrl = userViewModel.getIcon(updateFriendId); + + runOnUiThread(() -> { + if (nickname != null && !nickname.isEmpty()) { + userView.setNickname(nickname); + } else { + userView.setNickname(updateFriendId); + } + +// if (iconUrl != null && !iconUrl.isEmpty()) { +// userView.setIconUrl(iconUrl); +// } else { +// userView.setIconUrl("http://nitta-lab-www.is.konan-u.ac.jp/tampopo/images/default_icon.png"); +// } + }); + }).start(); + //フレンドの位置決め + // 最新更新フレンドをリストに追加(最大6人保持) + synchronized (recentUpdatedFriends) { + // すでに存在する場合は削除して再追加(重複防止) + recentUpdatedFriends.remove(updateFriendId); + // 先頭に追加(最近更新した人ほど前) + recentUpdatedFriends.add(0, updateFriendId); + + // 6人を超えたら古いものを削除 + if (recentUpdatedFriends.size() > 6) { + recentUpdatedFriends.remove(recentUpdatedFriends.size() - 1); + } + } + } }); //activitiesLiveData.observe(this, userViews.get(friendId)); @@ -540,7 +579,7 @@ // 通知を受信したときにダイアログを表示 runOnUiThread(() -> showChatNotification("user02")); // アイコンを赤枠に - runOnUiThread(() -> highlightUserIcon("user01")); + //runOnUiThread(() -> highlightUserIcon("user01")); } // @Override // protected void onNewIntent(Intent intent) {