From 3bc62c962cbf57b174fbdfea0af7c48f425156a6 Mon Sep 17 00:00:00 2001 From: Hny0305Lin <1553809191@qq.com> Date: Sun, 7 Jul 2024 00:44:18 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89Release=201.3=EF=BC=8C=E6=96=B0UI?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=E6=98=BE=E7=A4=BA=E5=9C=A8=E6=B0=94?= =?UTF-8?q?=E6=B3=A1=E6=97=81=E8=BE=B9=EF=BC=8C=E6=B8=85=E9=99=A4=E5=86=97?= =?UTF-8?q?=E4=BD=99=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nlchat/fun/ChatCore/ChatAdapter.java | 8 +++-- .../nlchat/fun/ChatCore/ChatMessage.java | 8 ++++- .../fun/ChatCore/ChatMessageQueueUpdater.java | 5 ++- .../nlchat/fun/ChatCore/ChatTimestamp.java | 19 +++++++++++ .../nearlink/nlchat/fun/MainActivity.java | 32 ++++--------------- app/src/main/res/values/strings.xml | 2 +- 6 files changed, 43 insertions(+), 31 deletions(-) create mode 100644 app/src/main/java/com/haohanyh/linmengjia/nearlink/nlchat/fun/ChatCore/ChatTimestamp.java 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。 友情感谢