diff --git a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatAdapter.java b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatAdapter.java index e2aea85..ccfd4ee 100644 --- a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatAdapter.java +++ b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatAdapter.java @@ -71,11 +71,12 @@ public int getItemCount() { // 发送消息的ViewHolder private class SentMessageHolder extends RecyclerView.ViewHolder { - TextView messageText; + TextView messageText,timestampText; SentMessageHolder(View itemView) { super(itemView); messageText = itemView.findViewById(R.id.text_message_body); + timestampText = itemView.findViewById(R.id.text_message_time); // 设置自定义字体 ChatFontUtils.applyCustomFont(context, messageText); @@ -83,16 +84,18 @@ private class SentMessageHolder extends RecyclerView.ViewHolder { void bind(ChatMessage message) { messageText.setText(message.getMessage()); + timestampText.setText(message.getTimestamp()); } } // 接收消息的ViewHolder private class ReceivedMessageHolder extends RecyclerView.ViewHolder { - TextView messageText; + TextView messageText,timestampText; ReceivedMessageHolder(View itemView) { super(itemView); messageText = itemView.findViewById(R.id.text_message_body); + timestampText = itemView.findViewById(R.id.text_message_time); // 设置自定义字体 ChatFontUtils.applyCustomFont(context, messageText); @@ -100,6 +103,7 @@ private class ReceivedMessageHolder extends RecyclerView.ViewHolder { void bind(ChatMessage message) { messageText.setText(message.getMessage()); + timestampText.setText(message.getTimestamp()); } } } \ No newline at end of file diff --git a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessage.java b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessage.java index 6a4b57a..14e4daf 100644 --- a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessage.java +++ b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessage.java @@ -3,10 +3,12 @@ public class ChatMessage { private String message; + private String timestamp; private boolean isSent; - public ChatMessage(String message, boolean isSent) { + public ChatMessage(String message, String timestamp, boolean isSent) { this.message = message; + this.timestamp = timestamp; this.isSent = isSent; } @@ -14,6 +16,10 @@ public String getMessage() { return message; } + public String getTimestamp() { + return timestamp; + } + public boolean isSent() { return isSent; } diff --git a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessageQueueUpdater.java b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessageQueueUpdater.java index d7774ae..207523c 100644 --- a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessageQueueUpdater.java +++ b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatMessageQueueUpdater.java @@ -16,6 +16,8 @@ public class ChatMessageQueueUpdater { private static final String TAG = "ChatMessageQueueUpdater & NLChat"; + private ChatTimestamp chatTimestamp = new ChatTimestamp(); // 聊天时间戳 + private Queue messageQueue; // 消息队列 private TextView textView; // 显示消息的 TextView private List chatMessages; // 聊天消息列表 @@ -63,7 +65,8 @@ public void updateTextView() { // 新UI处理,将新消息添加到 chatMessages 列表中 for (String newMessage : newMessages) { boolean isUser = logPrefix.contains("User: "); - chatMessages.add(new ChatMessage(newMessage, isUser)); + String timestamp = chatTimestamp.getCurrentTimestamp(); // 获取当前时间戳 + chatMessages.add(new ChatMessage(newMessage, timestamp, isUser)); } chatAdapter.notifyDataSetChanged(); // 旧UI处理 diff --git a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatTimestamp.java b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatTimestamp.java new file mode 100644 index 0000000..22c5a7e --- /dev/null +++ b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatTimestamp.java @@ -0,0 +1,19 @@ +/* 受Haohanyh Computer Software Products Open Source LICENSE保护 https://github.com/Hny0305Lin/LICENSE/blob/main/LICENSE */ +package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +public class ChatTimestamp { + + public String getCurrentTimestamp() { //使用展示时间戳时,只需要展示今天日期和时间即可 + // 获取当前时间戳的逻辑 + return new SimpleDateFormat("MM-dd,HH:mm", Locale.getDefault()).format(new Date()); + } + + public String saveCurrentTimestamp() { //数据库写入时间戳时,写入完整 + // 获取当前时间戳的逻辑 + return new SimpleDateFormat("MM-dd HH:mm:ss.SSS", Locale.getDefault()).format(new Date()); + } +} diff --git a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/MainActivity.java b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/MainActivity.java index 7cabd05..a5b1e2c 100644 --- a/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/MainActivity.java +++ b/app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/MainActivity.java @@ -54,6 +54,7 @@ import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatMessageQueueUpdater; import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatProcessorForExtract; import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatSaveMessageDatabaseManager; +import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatTimestamp; import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatUIAlertDialog; import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatUIAnimationUtils; import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatUtils; @@ -69,7 +70,6 @@ import com.haohanyh.linmengjia.nearlink.nlchat.fun.WCHUart.WCHUartSettings; import java.nio.charset.StandardCharsets; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -138,9 +138,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe private ChatSaveMessageDatabaseManager chatSaveMessageDatabaseManager; //聊天时间戳 - @SuppressLint("SimpleDateFormat") - //private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS"); - private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss.SSS"); + private ChatTimestamp chatTimestamp = new ChatTimestamp(); //调用SQLite private SQLiteDataBaseAPP dbHelper; @@ -549,13 +547,8 @@ private void NearLinkChatReadData() { runOnUiThread(() -> { //如果需要存储到数据库中 if (ChatUtils.isSqlitemanager()) { - // 分离时间戳和消息内容 - String[] parts = processedString.split(" - ", 2); - if (parts.length == 2) { - String timestamp = parts[0]; - String message = parts[1]; - saveMessageToDatabase(timestamp, message, "User"); - } + String timestamp = chatTimestamp.saveCurrentTimestamp(); + saveMessageToDatabase(timestamp, processedString, "User"); } //如果需要UI滚动消息 if (ChatUtils.isScrollingMessages()) { @@ -599,10 +592,6 @@ private String CH34xProcessingForReadData(String string) { completeSecondData = completeFirstData.replace(ChatUtils.getPrefixClient(), "").trim(); Log.v(TAG, "长度:completeSecondData.length="+ completeSecondData.length() + "\t内容:" + completeSecondData); } - //添加时间戳 - String timestamp = dateFormat.format(new java.util.Date()); - completeSecondData = timestamp + " - " + completeSecondData; - //确保消息以换行符结尾 if (!completeSecondData.endsWith("\n")) { completeSecondData += "\n"; @@ -691,13 +680,8 @@ public void NearLinkChatSendData(View view) { runOnUiThread(() -> { //如果需要存储到数据库中 if (ChatUtils.isSqlitemanager()) { - // 分离时间戳和消息内容 - String[] parts = TextOfClient.split(" - ", 2); - if (parts.length == 2) { - String timestamp = parts[0]; - String message = parts[1]; - saveMessageToDatabase(timestamp, message, "Me"); - } + String timestamp = chatTimestamp.saveCurrentTimestamp(); + saveMessageToDatabase(timestamp, messageSend, "Me"); } //如果需要UI滚动消息 if (ChatUtils.isScrollingMessages()) { @@ -725,10 +709,6 @@ public void NearLinkChatSendData(View view) { } private String CH34xProcessingForSendData(String string) { - //添加时间戳 - String timestamp = dateFormat.format(new java.util.Date()); - string = timestamp + " - " + string; - //确保消息以换行符结尾 if (!string.endsWith("\n")) { string += "\n"; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 75364a8..3076242 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,7 +1,7 @@ NLChat com.haohanyh.linmengjia.nearlink.nlchat.fun - 1.3.95.2024.0707 + 1.3.100.2024.0707 NLChat,浩瀚银河宗旨为用爱和魔法创造Android APP。 友情感谢