diff --git a/app/src/main/java/com/example/tampopo_client/models/Chatroom.java b/app/src/main/java/com/example/tampopo_client/models/Chatroom.java index 5a08e92..74622ba 100644 --- a/app/src/main/java/com/example/tampopo_client/models/Chatroom.java +++ b/app/src/main/java/com/example/tampopo_client/models/Chatroom.java @@ -2,21 +2,48 @@ public class Chatroom { private String chatroomId; // チャットルームのID + private String partnerUserId; // 相手ユーザーID + private String user1Id; // 1人目のユーザーID + private String user2Id; // 2人目のユーザーID public Chatroom() {} - - public Chatroom(String chatroomId) { + public Chatroom(String chatroomId, String user1Id, String user2Id) { this.chatroomId = chatroomId; + this.user1Id = user1Id; + this.user2Id = user2Id; + } + + + public Chatroom(String chatroomId,String partnerUserId) { + this.chatroomId = chatroomId; + this.partnerUserId = partnerUserId; } public String getChatroomId() { return chatroomId; } - public String foundChatroomId(){return chatroomId;} - public void setChatroomId(String chatroomId) { this.chatroomId = chatroomId; } + public String getUser1Id() { + return user1Id; + } + + + + public String getUser2Id() { + return user2Id; + } + + + public String getPartnerUserId() { + return partnerUserId; + } + + public void setPartnerUserId(String partnerUserId) { + this.partnerUserId = partnerUserId; + } + } diff --git a/app/src/main/java/com/example/tampopo_client/resources/ChatroomResource.java b/app/src/main/java/com/example/tampopo_client/resources/ChatroomResource.java index 2574364..4384636 100644 --- a/app/src/main/java/com/example/tampopo_client/resources/ChatroomResource.java +++ b/app/src/main/java/com/example/tampopo_client/resources/ChatroomResource.java @@ -27,6 +27,7 @@ @GET("foundChatroom") Call getMyChatroom( @Query("EnterUserId") String userId + ); 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 0c5a721..7712406 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 @@ -3,46 +3,267 @@ import android.os.Handler; import android.os.Looper; import android.util.Log; - +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; +import com.example.tampopo_client.models.ChatMessage; +import com.example.tampopo_client.models.Chatroom; +import com.example.tampopo_client.resources.ChatroomResource; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.jackson.JacksonConverterFactory; public class ChatViewModel extends RealTimeViewModel { + + private final Retrofit retrofit; + private final ChatroomResource chatroomResource; + + // --- LiveData --- + private final MutableLiveData chatroomIdLiveData = new MutableLiveData<>(); + private final MutableLiveData> chatMessages = new MutableLiveData<>(new ArrayList<>()); + private final MutableLiveData latestMessage = new MutableLiveData<>(); + private final MutableLiveData chatroomClosed = new MutableLiveData<>(); + private final HashMap> chatFriendToFriendLiveData = new HashMap<>(); + + // 通話中(リアルタイム監視用) + private final MutableLiveData chatFriendToMeLiveData = new MutableLiveData<>(); + private final MutableLiveData chatToFriendLiveData = new MutableLiveData<>(); + + // ===== 以下は追加部分(Handlerループ) ===== + private final Handler handler = new Handler(Looper.getMainLooper()); + private boolean isChecking = false; // 二重起動防止フラグ + + + // =============================== + // 通知・リアルタイム関連 + // =============================== private final List notificationListeners = new ArrayList<>(); + private boolean notificationSent = false; // 一度だけ通知を送るためのフラグ - //private static final double NOTIFICATION_RECEIVE_PROBABILITY = 0.5; + private String userId; + private String token; + private String chatroomId; - //natty - private boolean notificationSent = false; // 一度だけ送信するためのフラグ + public ChatViewModel() { + this.retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/tampopo/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + + this.chatroomResource = retrofit.create(ChatroomResource.class); + } + + public ChatViewModel(String userId, String token,String chatroomId) { + this.userId = userId; + this.token = token; + this.chatroomId = chatroomId; -// @Override -// public Runnable onUpdate() { -// return () -> { -// // 1% の確率で onNotificationReceived() が呼び出される -// double borderValue = Math.floor(Math.random() * 100); -// double currentValue = NOTIFICATION_RECEIVE_PROBABILITY * 100; -// if (currentValue >= borderValue) { -// Log.d("ChatViewModel", "Received test notification."); -// notificationListeners.forEach(listener -> listener.onNotificationReceived()); -// } -// }; -// } - //natty + this.retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/tampopo/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + + this.chatroomResource = retrofit.create(ChatroomResource.class); + } + @Override public Runnable onUpdate() { return () -> { if (!notificationSent) { - // 10秒後に一回だけ通知を送る - notificationSent = true; // もう送らない + notificationSent = true; new Handler(Looper.getMainLooper()).postDelayed(() -> { Log.d("ChatViewModel", "Received test notification (after 10s)."); notificationListeners.forEach(NotificationListener::onNotificationReceived); - }, 10_000); // 10秒(10000ms) + }, 10_000); // 10秒後に通知 } + foundChatroom(userId); + 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; } + + // =============================== + // 1. チャットルームに入る(かける側) + // =============================== + public void enterChatroom(String myId, String partnerId, String token) { + Call call = chatroomResource.enterChatroom(myId, partnerId, token); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful() && response.body() != null) { + chatroomIdLiveData.setValue(response.body().getChatroomId()); + Log.d("ChatVM", "enterChatroom success → chatroomId: " + response.body().getChatroomId()); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("ChatVM", "enterChatroom error: " + t.getMessage()); + } + }); + } + + // 1.5 自分がchatroomに入っているのか確認する + public void foundChatroom(String userId) { + Call call = chatroomResource.getMyChatroom(userId); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful() && response.body() != null) { + Chatroom chatroom = response.body(); + + chatroomIdLiveData.setValue(chatroom.getChatroomId()); + chatFriendToMeLiveData.setValue(chatroom.getPartnerUserId()); + } else { + chatroomIdLiveData.setValue(null); + chatroomClosed.setValue(true); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("ChatroomVM", "checkCurrentChatroom error: " + t.getMessage()); + } + }); + } + + // =============================== + // 1.55 現在通話中のペアを確認 + // =============================== + public void fetchActiveChatPair(String userId, String token) { + Call call = chatroomResource.getMyChatroom(userId); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful() && response.body() != null) { + Chatroom activeRoom = response.body(); + chatroomIdLiveData.setValue(activeRoom.getChatroomId()); + + String user1 = activeRoom.getUser1Id(); + String user2 = activeRoom.getUser2Id(); + + // ログインユーザーが user1 の場合 + if (userId.equals(user1)) { + chatFriendToMeLiveData.setValue(user2); // もう一方のユーザーを LiveData にセット + // HashMap にもセット + chatFriendToFriendLiveData.put(userId, new MutableLiveData<>(user2)); + } + // ログインユーザーが user2 の場合 + else if (userId.equals(user2)) { + chatToFriendLiveData.setValue(user1); // もう一方のユーザーを LiveData にセット + chatFriendToFriendLiveData.put(userId, new MutableLiveData<>(user1)); + } + + Log.d("ChatVM", "Active pair found: " + user1 + " ↔ " + user2); + } else { + chatroomIdLiveData.setValue(null); + chatroomClosed.setValue(true); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("ChatVM", "fetchActiveChatPair error: " + t.getMessage()); + } + }); + } + + + // =============================== + // 2. メッセージ送信 + // =============================== + public void sendMessage(String chatroomId, String senderId, String message, String token, boolean isFromMainActivity) { + Call call = chatroomResource.sendMessage(token, chatroomId, senderId, message); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful() && response.body() != null) { + ChatMessage newMessage = response.body(); + if (isFromMainActivity) { + chatFriendToMeLiveData.setValue(newMessage.getSenderId()); + } else { + latestMessage.setValue(newMessage); + } + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("ChatVM", "sendMessage error: " + t.getMessage()); + } + }); + } + + // =============================== + // 3. 最新メッセージ取得 + // =============================== + public void loadLatestMessage(String chatroomId, String userId, String token) { + Call> call = chatroomResource.getMessages(token, chatroomId, userId); + call.enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if (response.isSuccessful() && response.body() != null && !response.body().isEmpty()) { + latestMessage.setValue(response.body().get(response.body().size() - 1)); + } + } + + @Override + public void onFailure(Call> call, Throwable t) { + Log.e("ChatVM", "loadLatestMessage error: " + t.getMessage()); + } + }); + } + + // =============================== + // 4. チャットルーム削除 + // =============================== + public void destroyChatroom(String chatroomId, String userId, String token) { + Call call = chatroomResource.destroyChatroom(token, chatroomId, userId); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + chatroomClosed.setValue(true); + if (!response.isSuccessful()) { + Log.e("ChatVM", "destroyChatroom failed: " + response.code()); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + chatroomClosed.setValue(true); + Log.e("ChatVM", "destroyChatroom error: " + t.getMessage()); + } + }); + } + + // ===== ここから Handlerループ部分をそのまま追記 ===== + /** + * 10秒ごとにサーバー確認を行う + */ + private void startServerCheck() { + handler.postDelayed(new Runnable() { + @Override + public void run() { + Log.d("ChatViewModel", "Checking server / Received test notification."); + notificationListeners.forEach(NotificationListener::onNotificationReceived); + handler.postDelayed(this, 10_000); + } + }, 10_000); + } + public void addNotificationListener(NotificationListener listener) { notificationListeners.add(listener); } @@ -50,4 +271,12 @@ public void clearNotificationListener() { notificationListeners.clear(); } + + /** + * 定期確認を止めたい場合に使う + */ + public void stopServerCheck() { + handler.removeCallbacksAndMessages(null); + isChecking = false; + } } diff --git a/app/src/main/java/com/example/tampopo_client/viewmodels/ChatViewModelFactory.java b/app/src/main/java/com/example/tampopo_client/viewmodels/ChatViewModelFactory.java new file mode 100644 index 0000000..48d5ce0 --- /dev/null +++ b/app/src/main/java/com/example/tampopo_client/viewmodels/ChatViewModelFactory.java @@ -0,0 +1,34 @@ +package com.example.tampopo_client.viewmodels; + +import androidx.annotation.NonNull; +import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelProvider; + +import java.lang.reflect.InvocationTargetException; + +public class ChatViewModelFactory implements ViewModelProvider.Factory { + private final String userId; + private final String token; + private final String chatroomId; + + public ChatViewModelFactory(String userId, String token, String chatroomId) { + this.userId = userId; + this.token = token; + this.chatroomId = chatroomId; + } + + @NonNull + @Override + public T create(@NonNull Class modelClass) { + if (modelClass.isAssignableFrom(ChatViewModel.class)) { + try { + return modelClass.getConstructor(String.class, String.class).newInstance(userId, token); + } catch (InvocationTargetException | IllegalAccessException | InstantiationException | + NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + throw new IllegalStateException("作成するViewModelが異なります。"); + } +} + diff --git a/app/src/main/java/com/example/tampopo_client/viewmodels/ChatroomViewModel.java b/app/src/main/java/com/example/tampopo_client/viewmodels/ChatroomViewModel.java deleted file mode 100644 index 901b43c..0000000 --- a/app/src/main/java/com/example/tampopo_client/viewmodels/ChatroomViewModel.java +++ /dev/null @@ -1,180 +0,0 @@ -package com.example.tampopo_client.viewmodels; - -import android.util.Log; - -import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.ViewModel; - -import com.example.tampopo_client.models.ChatMessage; -import com.example.tampopo_client.models.Chatroom; -import com.example.tampopo_client.resources.ChatroomResource; - -import java.util.ArrayList; -import java.util.List; - -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import retrofit2.Retrofit; -import retrofit2.converter.jackson.JacksonConverterFactory; - -public class ChatroomViewModel extends ViewModel { - - private final Retrofit retrofit; - private final ChatroomResource chatroomResource; - - private final MutableLiveData chatroomId = new MutableLiveData<>(); - private final MutableLiveData> chatMessages = new MutableLiveData<>(new ArrayList<>()); - private final MutableLiveData latestMessage = new MutableLiveData<>(); - private final MutableLiveData chatroomClosed = new MutableLiveData<>(); - - public ChatroomViewModel() { - this.retrofit = new Retrofit.Builder() - .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/tampopo/") - .addConverterFactory(JacksonConverterFactory.create()) - .build(); - - this.chatroomResource = retrofit.create(ChatroomResource.class); - } - - // LiveData getter - public MutableLiveData getChatroomIdLiveData() { - return chatroomId; - } - - public MutableLiveData> getChatMessages() { - return chatMessages; - } - - public MutableLiveData getLatestMessageLiveData() { - return latestMessage; - } - - public MutableLiveData getChatroomClosed() { - return chatroomClosed; - } - - // 1. チャットルームに入る(入れる側) - public void enterChatroom(String myId, String partnerId, String token) { - //自分のIdと相手のidと自分tokenを送る - Call call = chatroomResource.enterChatroom(myId, partnerId, token); - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful() && response.body() != null) { - chatroomId.setValue(response.body().getChatroomId()); - } - }//サーバーからchatroomIdが送られてきてそれを保持する処理 - - @Override - public void onFailure(Call call, Throwable t) { - Log.e("ChatViewModel", "enterChatroom error: " + t.getMessage()); - } - }); - } - - //1.5自分がcahtroomに入っているのかを確認する - public void foundChatroom(String userId) { - Call call = chatroomResource.getMyChatroom(userId); - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful() && response.body() != null) { - chatroomId.setValue(response.body().getChatroomId()); - //これがあっているのか確認chatroomIdどんなうごきをするのか確認せよ - } else { - chatroomId.setValue(null); // どこにも入っていない場合 - chatroomClosed.setValue(true); - } - } - - @Override - public void onFailure(Call call, Throwable t) { - Log.e("ChatroomVM", "checkCurrentChatroom error: " + t.getMessage()); - } - }); - } - - // 2. メッセージ送信(リアルタイム風)送る側 - //自分のchatはmychatmesaageに変更するかも - public void sendMessage(String chatroomId, String senderId, String content, String token) { - //このメソッドは 指定したチャットルーム (chatroomId) に、あるユーザー (senderId) が、入力したメッセージ (message) を、認証トークン (token) を使って送信する役割です。 - Call call = chatroomResource.sendMessage(token, chatroomId, senderId, content); - //Retrofit を使って 「サーバーのAPIにメッセージ送信リクエストを作る」 メソッド。 - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful() && response.body() != null) { - ChatMessage newMessage = response.body(); - // 成功したらそのまま新しいメッセージをUIに流す - -// // 既存のメッセージリストを更新 - //受け取る側のコードっぽいので不要だと思ってます理由はnewMessageでUIを表示するのでそれ以外いらない -// //画面に表示されているメッセージリスト (chatMessages) を取り出し、そこに 新しいメッセージを追加して更新しています。 -// List current = chatMessages.getValue(); -// if (current == null) current = new ArrayList<>(); - - - // operationResult は表示不要なので更新しない - } - // 失敗した場合も「エラーです」とは出さない(履歴を残さない仕様) - } - - @Override - public void onFailure(Call call, Throwable t) { - // ネットワークエラーでも通知はしない(UI上に履歴が残らない通話風チャットだから) - // 必要なら内部ログにだけ残す - Log.e("ChatViewModel", "sendMessage error: " + t.getMessage()); - } - }); - } - - - // 3. 最新メッセージ取得(エラー時はUIに表示しない)受け取る側 - public void loadLatestMessage(String chatroomId, String partnerId, String token) { - Call> call = chatroomResource.getMessages(token, chatroomId, partnerId); - call.enqueue(new Callback>() { - @Override - public void onResponse(Call> call, Response> response) { - if (response.isSuccessful() && response.body() != null && !response.body().isEmpty()) { - // 最新メッセージだけ反映 - latestMessage.setValue(response.body().get(response.body().size() - 1)); - } - // else の場合は何もしない(UIにエラーを出さない) - } - - @Override - public void onFailure(Call> call, Throwable t) { - // ネットワークエラー時もUIには表示しない - Log.e("ChatViewModel", "loadLatestMessage error: " + t.getMessage()); - } - }); - } - - - // 4. チャットルーム削除 削除を実行した人の処理。削除された側の処理は含まれてません - public void destroyChatroom(String chatroomId, String userId, String token) { - Call call = chatroomResource.destroyChatroom(token, chatroomId, userId); - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - // 成功でも失敗でも、画面を閉じる処理をUIに通知 - // 例えば LiveData を使って Activity/Fragment に伝える - chatroomClosed.setValue(true); - - // ログには残す - if (!response.isSuccessful()) { - Log.e("ChatViewModel", "destroyChatroom failed: " + response.code()); - } - } - - @Override - public void onFailure(Call call, Throwable t) { - // ネットワークエラーでも UI は閉じる - chatroomClosed.setValue(true); - Log.e("ChatViewModel", "destroyChatroom error: " + t.getMessage()); - } - }); - } -} - diff --git a/app/src/main/java/com/example/tampopo_client/viewmodels/FriendReceivedRequestViewModel.java b/app/src/main/java/com/example/tampopo_client/viewmodels/FriendReceivedRequestViewModel.java index 0b01c76..2fe45a8 100644 --- a/app/src/main/java/com/example/tampopo_client/viewmodels/FriendReceivedRequestViewModel.java +++ b/app/src/main/java/com/example/tampopo_client/viewmodels/FriendReceivedRequestViewModel.java @@ -17,58 +17,80 @@ import retrofit2.converter.scalars.ScalarsConverterFactory; public class FriendReceivedRequestViewModel extends ViewModel { - //サーバー(API)と通信するためのツール + // サーバー(API)と通信するためのツール private final Retrofit retrofit; - //APIの窓口 + // API の窓口 private final FriendRequestsResource friendRequestsResource; - //自分が受け取った申請 + // 自分が受け取った申請 private final MutableLiveData> receivedRequests; - //通信結果の状態 + // 通信結果の状態 private final MutableLiveData operationResult; - public FriendReceivedRequestViewModel() { + // 直近で使用したユーザーIDをキャッシュしておく + private String cachedUserId; + private String cachedToken; + public FriendReceivedRequestViewModel() { this.retrofit = new Retrofit.Builder() .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/tampopo/") .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(JacksonConverterFactory.create()) .build(); + this.friendRequestsResource = retrofit.create(FriendRequestsResource.class); this.receivedRequests = new MutableLiveData<>(new ArrayList<>()); this.operationResult = new MutableLiveData<>(); } - //viewがobserve出来るように + // viewがobserve出来るように public MutableLiveData> getReceivedRequestsLiveData() { return receivedRequests; } - //サーバーから受け取ったFriendReceivedRequestのデータを格納してキャッシュしていくぞ - public void loadReceivedRequests(String token) { - //tokenを渡して、受信フレンド申請一覧を取得するHTTPリクエスト(Webのサーバーに対して何かをお願いするメッセージ」)を作る準備をしている + public MutableLiveData getOperationResultLiveData() { + return operationResult; + } + + // サーバーから受け取った FriendRequest のデータを読み込む + public void loadReceivedRequests(String token, String myUserId) { + // キャッシュ保存(削除後の再読み込み用) + this.cachedUserId = myUserId; + this.cachedToken = token; + Call> call = friendRequestsResource.getFriendRequests(token); - //call.enqueueでサーバーへ送信(何を?) call.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.isSuccessful()) { - //通信が成功したらLiveDataへのキャッシュ - receivedRequests.setValue(response.body()); - operationResult.setValue("Success"); - System.out.println("Success SetValue" + response.body()); + List allRequests = response.body(); + if (allRequests == null) allRequests = new ArrayList<>(); + + // ✅ 自分が受け取ったものだけを抽出 + List receivedOnly = new ArrayList<>(); + for (FriendRequest req : allRequests) { + if (req.getReceiverId() != null && req.getReceiverId().equals(myUserId)) { + receivedOnly.add(req); + } + } + + receivedRequests.setValue(receivedOnly); + operationResult.setValue("Success (filtered " + receivedOnly.size() + " requests)"); + System.out.println("Success SetValue (filtered): " + receivedOnly); + } else { operationResult.setValue("Error: " + response.code()); - System.out.println("response error"); + System.out.println("Response error: " + response.code()); } } @Override public void onFailure(Call> call, Throwable t) { operationResult.setValue("Network error: " + t.getMessage()); - System.out.println("ネットワークエラー: " + t); + System.out.println("Network error: " + t); } }); } + // フレンドリクエスト削除メソッド public void deleteFriendRequest(String friendRequestId, String token) { Call call = friendRequestsResource.deleteFriendRequest(friendRequestId, token); @@ -78,8 +100,13 @@ public void onResponse(Call call, Response response) { if (response.isSuccessful()) { operationResult.setValue("Friend request deleted successfully."); - loadReceivedRequests(token); // 削除後、一覧を更新 System.out.println("Deleted friend request ID: " + friendRequestId); + + // ✅ キャッシュがある場合は再読み込み + if (cachedToken != null && cachedUserId != null) { + loadReceivedRequests(cachedToken, cachedUserId); + } + } else { operationResult.setValue("Error deleting request: " + response.code()); System.out.println("Error deleting request: " + response.code()); @@ -97,3 +124,4 @@ + 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 d75baff..2c8e95c 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 @@ -39,6 +39,7 @@ import com.example.tampopo_client.Tampopo; import com.example.tampopo_client.models.Activity; import com.example.tampopo_client.viewmodels.ActivityViewModel; +import com.example.tampopo_client.viewmodels.ChatViewModelFactory; import com.google.android.material.imageview.ShapeableImageView; import com.example.tampopo_client.viewmodels.ActivityViewModelFactory; import com.example.tampopo_client.viewmodels.ChatViewModel; @@ -64,6 +65,7 @@ ActivityViewModel activityViewModel; Tampopo tampopo; + //追加しました! private ChatViewModel chatViewModel; @@ -109,9 +111,10 @@ ActivityViewModelFactory factory = new ActivityViewModelFactory(tampopo.getUserId(), tampopo.getToken()); // Factoryを使って、引数をコンストラクタにわたしつつViewModelを作成 activityViewModel = new ViewModelProvider(this, factory).get(ActivityViewModel.class); + //追加しました!!!!!!!!!!! // ChatViewModelを初期化する - chatViewModel = new ViewModelProvider(this).get(ChatViewModel.class); - + ChatViewModelFactory factory1 = new ChatViewModelFactory(tampopo.getUserId(), tampopo.getToken(), tampopo.getChatroomId()); + chatViewModel = new ViewModelProvider(this, factory1).get(ChatViewModel.class); // MutableLiveData>friendUserIdsLiveDate = activityViewModel.getFriendUserIdsLiveData(); // friendUserIdsLiveDate.observe(this, new Observer>() {