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 494c624..391bb74 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,10 @@ import android.content.Intent; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; +import android.text.Editable; +import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; @@ -34,6 +38,9 @@ Tampopo tampopo; + private final Handler handler = new Handler(Looper.getMainLooper()); + private Runnable runnable; + @Override @@ -57,66 +64,38 @@ friendIdTextView.setText(tampopo.getChatFriendId()); receiverMessage.setKeyListener(null);//受信メッセージ編集不可 -// buttonSend.setOnClickListener(new OnClickListener() { -// @Override -// public void onClick(View view) { -// String receiverText = receiverMessage.getText().toString();//receiverMessageにユーザーが入力した文字を取り出して、String型の 変数receiverText に入れる -// if (!receiverText.isEmpty()) { -// String senderId = tampopo.getUserId(); -// String token = tampopo.getToken(); -// ChatroomViewModel.loadLatestMessage(null, senderId, token); -// receiverMessage.setText(receiverText);//受信欄に表示 -// senderMessage.setText("");//送信欄をクリア -// } -// } -// }); + // メッセージ入力時にリアルタイムで送信 (300msディレイ) + senderMessage.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } -// Intent intent = getIntent(); -// String friendId = intent.getStringExtra("friendId"); -// String chatroomId = intent.getStringExtra("chatroomId"); + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + handler.removeCallbacks(runnable); + } - - - //String myId = tampopo.getUserId(); - //String friendId = tampopo.getChatFriendId(); - String chatroomId = tampopo.getChatroomId(); - roomId.setText(chatroomId); - //String chatroomId = "0"; - //String token = tampopo.getToken(); - - // chatroomViewModel.enterChatroom(myId, friendId, token); - - //Log.d("ChatDebug", "チャット相手ID: " + friendId); - Log.d("ChatDebug", "チャットルームID: " + chatroomId); - - -// 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がクリックされたときの処理 - @Override - public void onClick(View view) { //ボタンがクリックされたときに実行される処理本体 - String senderText = senderMessage.getText().toString(); //senderMessageにユーザーが入力した文字を取り出して、String型の 変数receiverText に入れる - Log.d("ChatDebug", senderText); - if (!senderText.isEmpty()) { //senderTextが空でなければ - String senderId = tampopo.getUserId(); - String token = tampopo.getToken(); - String chatroomId = tampopo.getChatroomId(); - //String chatroomId = "0"; - chatViewModel.sendMessage(chatroomId, senderId, senderText, token); - senderMessage.setText("");//自分のメッセージを消す - } - } + @Override + public void afterTextChanged(Editable s) { + runnable = () -> { + String senderId = tampopo.getUserId(); + String token = tampopo.getToken(); + String chatroomId = tampopo.getChatroomId(); + if (chatroomId != null) { + chatViewModel.sendMessage(chatroomId, senderId, s.toString(), token); + } + }; + handler.postDelayed(runnable, 300); // 300ms delay + } }); + // 送信ボタンはリアルタイム送信に不要なため、機能を無効化 + sendButton.setOnClickListener(null); + + String chatroomId = tampopo.getChatroomId(); + roomId.setText(chatroomId); + + Log.d("ChatDebug", "チャットルームID: " + chatroomId); //メッセージ受信 chatViewModel.getLatestMessageLiveData().observe(this, chatMessage -> {