2025-12-19 15:11:49 +08:00
|
|
|
package com.example.livestreaming;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
|
|
|
|
|
|
import com.example.livestreaming.databinding.ActivityConversationBinding;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class ConversationActivity extends AppCompatActivity {
|
|
|
|
|
|
|
|
|
|
private static final String EXTRA_CONVERSATION_ID = "extra_conversation_id";
|
|
|
|
|
private static final String EXTRA_CONVERSATION_TITLE = "extra_conversation_title";
|
|
|
|
|
|
|
|
|
|
private ActivityConversationBinding binding;
|
|
|
|
|
|
|
|
|
|
private ConversationMessagesAdapter adapter;
|
|
|
|
|
private final List<ChatMessage> messages = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
public static void start(Context context, String conversationId, String title) {
|
|
|
|
|
Intent intent = new Intent(context, ConversationActivity.class);
|
|
|
|
|
intent.putExtra(EXTRA_CONVERSATION_ID, conversationId);
|
|
|
|
|
intent.putExtra(EXTRA_CONVERSATION_TITLE, title);
|
|
|
|
|
context.startActivity(intent);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 16:31:46 +08:00
|
|
|
private static final String EXTRA_UNREAD_COUNT = "extra_unread_count";
|
|
|
|
|
|
|
|
|
|
private int initialUnreadCount = 0;
|
|
|
|
|
|
2025-12-19 15:11:49 +08:00
|
|
|
@Override
|
|
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
binding = ActivityConversationBinding.inflate(getLayoutInflater());
|
|
|
|
|
setContentView(binding.getRoot());
|
|
|
|
|
|
|
|
|
|
String title = getIntent() != null ? getIntent().getStringExtra(EXTRA_CONVERSATION_TITLE) : null;
|
|
|
|
|
binding.titleText.setText(title != null ? title : "会话");
|
2025-12-22 16:31:46 +08:00
|
|
|
|
|
|
|
|
// 获取该会话的初始未读数量
|
|
|
|
|
initialUnreadCount = getIntent() != null ? getIntent().getIntExtra(EXTRA_UNREAD_COUNT, 0) : 0;
|
2025-12-19 15:11:49 +08:00
|
|
|
|
|
|
|
|
binding.backButton.setOnClickListener(v -> finish());
|
|
|
|
|
|
|
|
|
|
setupMessages();
|
|
|
|
|
setupInput();
|
2025-12-22 16:31:46 +08:00
|
|
|
|
|
|
|
|
// 用户进入会话时,标记该会话的消息为已读,减少未读数量
|
|
|
|
|
if (initialUnreadCount > 0) {
|
|
|
|
|
UnreadMessageManager.decrementUnreadCount(this, initialUnreadCount);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onPause() {
|
|
|
|
|
super.onPause();
|
|
|
|
|
// 当用户离开会话时,更新总未读数量(如果会话未读数量已减少)
|
2025-12-19 15:11:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setupMessages() {
|
|
|
|
|
adapter = new ConversationMessagesAdapter();
|
|
|
|
|
|
|
|
|
|
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
|
|
|
|
|
layoutManager.setStackFromEnd(false);
|
|
|
|
|
binding.messagesRecyclerView.setLayoutManager(layoutManager);
|
|
|
|
|
binding.messagesRecyclerView.setAdapter(adapter);
|
|
|
|
|
|
|
|
|
|
messages.clear();
|
|
|
|
|
String title = binding.titleText.getText() != null ? binding.titleText.getText().toString() : "";
|
|
|
|
|
messages.add(new ChatMessage(title, "你好~"));
|
|
|
|
|
messages.add(new ChatMessage("我", "在的,有什么需要帮忙?"));
|
|
|
|
|
adapter.submitList(new ArrayList<>(messages));
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setupInput() {
|
|
|
|
|
binding.sendButton.setOnClickListener(v -> sendMessage());
|
|
|
|
|
|
|
|
|
|
binding.messageInput.setOnEditorActionListener((v, actionId, event) -> {
|
|
|
|
|
if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
|
|
|
|
|
sendMessage();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
binding.messageInput.setOnFocusChangeListener((v, hasFocus) -> {
|
|
|
|
|
if (hasFocus) scrollToBottom();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendMessage() {
|
|
|
|
|
String text = binding.messageInput.getText() != null ? binding.messageInput.getText().toString().trim() : "";
|
|
|
|
|
if (TextUtils.isEmpty(text)) return;
|
|
|
|
|
|
|
|
|
|
messages.add(new ChatMessage("我", text));
|
|
|
|
|
adapter.submitList(new ArrayList<>(messages));
|
|
|
|
|
binding.messageInput.setText("");
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void scrollToBottom() {
|
|
|
|
|
if (messages.isEmpty()) return;
|
|
|
|
|
binding.messagesRecyclerView.post(() -> binding.messagesRecyclerView.scrollToPosition(messages.size() - 1));
|
|
|
|
|
}
|
|
|
|
|
}
|