Skip to content

Commit

Permalink
🎉Release 1.3,数据库消息查阅功能(BETA),性能优化和防止内存溢出(BETA),取消背景运行列为Bug的问题(MIUI14问…
Browse files Browse the repository at this point in the history
…题),数据库防溢出优化
  • Loading branch information
Hny0305Lin committed Jul 19, 2024
1 parent b8d6793 commit 41f419f
Show file tree
Hide file tree
Showing 15 changed files with 467 additions and 75 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions UPDATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

| 版本号/功能 | CH340驱动 | 聊天功能 | 中文聊天 | SQLite管理 | 串口处理 | 剪贴板 | 背景运行 | 阅后即焚 | Github CI | 聊天增强 | 缓存内容 |
| --------- | --------- | -------- | -------- | -------- | -------- | -------- | -------- | ------- | :------: | -------- | -------- |
| [1.3.280.2024.0718](https://github.com/Hny0305Lin/NLChat/tree/v1.3.280.2024.0718) || ✅预览消息性能问题 || ✅SQLite需要优化 ||| 💦部分机型Bug,后续修复 || [✅点我查看](https://github.com/Hny0305Lin/NLChat/actions/runs/9988892829/job/27606336860) |||
| [1.3.273.2024.0717.Alpha](https://github.com/Hny0305Lin/NLChat/tree/v1.3.273.2024.0717.Alpha) |||| ✅SQLite修复中 ||| 💦有发现Bug,修复中 || [✅点我查看](https://github.com/Hny0305Lin/NLChat/actions/runs/9968892336/job/27544892288) |||
| [1.3.280.2024.0718](https://github.com/Hny0305Lin/NLChat/tree/v1.3.280.2024.0718) || ✅预览消息性能问题 || ✅SQLite需要优化 ||| || [✅点我查看](https://github.com/Hny0305Lin/NLChat/actions/runs/9988892829/job/27606336860) |||
| [1.3.273.2024.0717.Alpha](https://github.com/Hny0305Lin/NLChat/tree/v1.3.273.2024.0717.Alpha) |||| ✅SQLite修复中 ||| || [✅点我查看](https://github.com/Hny0305Lin/NLChat/actions/runs/9968892336/job/27544892288) |||
| [1.3.269.2024.0716](https://github.com/Hny0305Lin/NLChat/tree/v1.3.269.2024.0716) || ✅调整核心代码 || 💦SQLite部分功能暂时下线 || ✅客户板日志OK ||| [✅点我查看](https://github.com/Hny0305Lin/NLChat/actions/runs/9949202863/job/27485061738) |||
| [1.3.266.2024.0716](https://github.com/Hny0305Lin/NLChat/tree/v1.3.266.2024.0716) || ✅删除老旧代码页面 || 💦 || ✅修复MAC地址获取问题 ||| [✅点我查看](https://github.com/Hny0305Lin/NLChat/actions/runs/9948692531/job/27483727684) |||
| [1.3.252.2024.0714.R](https://github.com/Hny0305Lin/NLChat/tree/v1.3.252.2024.0714.R) ||||| ✅客户板OK |||| [✅点我查看](https://github.com/Hny0305Lin/NLChat/actions/runs/9925572354/job/27418178798) |||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,38 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.haohanyh.linmengjia.nearlink.nlchat.fun.R;

import com.haohanyh.linmengjia.nearlink.nlchat.fun.R.string;

import java.util.List;
import java.util.Random;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
//Log需要的TAG
private static final String TAG = "ChatAdapter & NLChat";

private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private static final int VIEW_TYPE_DEBUG_RECEIVED = 3;
private static final int VIEW_TYPE_MESSAGE_SENT = 1; //正常的消息,发
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2; //正常的消息,收

private static final int VIEW_TYPE_DEBUG_RECEIVED = 3; //Debug消息

private static final int VIEW_TYPE_MESSAGE_SENT_LATEST = -1; //数据库消息记录,发
private static final int VIEW_TYPE_MESSAGE_RECEIVED_LATEST = -2; //数据库消息记录,收

private static final int VIEW_TYPE_DEBUG_LATEST = 0; //历史Debug消息

private static final int VIEW_TYPE_HAOHANYH = 255; //彩蛋Debug

private List<ChatMessage> chatMessages;
private Context context;
Expand All @@ -38,43 +49,80 @@ public ChatAdapter(Context context, List<ChatMessage> chatMessages) {
@Override
public int getItemViewType(int position) {
ChatMessage message = chatMessages.get(position);

if (message.isUser()) {
return VIEW_TYPE_MESSAGE_RECEIVED;
} else if (message.isMe()) {
return VIEW_TYPE_MESSAGE_SENT;
} else if (message.isDebug()) {
return VIEW_TYPE_DEBUG_RECEIVED;
} else if (message.isSQLiteUser()) {
return VIEW_TYPE_MESSAGE_RECEIVED_LATEST;
} else if (message.isSQLiteMe()) {
return VIEW_TYPE_MESSAGE_SENT_LATEST;
} else if (message.isSQLiteDebug()) {
return VIEW_TYPE_DEBUG_LATEST;
} else {
return VIEW_TYPE_MESSAGE_SENT;
return VIEW_TYPE_HAOHANYH;
}
}

// 创建不同类型的 ViewHolder
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_MESSAGE_SENT) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_chat_sent, parent, false);
return new SentMessageHolder(view);
} else if (viewType == VIEW_TYPE_DEBUG_RECEIVED) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_debug, parent, false);
return new ReceivedDEBUGMessageHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_chat_received, parent, false);
return new ReceivedMessageHolder(view);
View view;
switch (viewType) {
case VIEW_TYPE_MESSAGE_RECEIVED:
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_chat_received, parent, false);
return new ReceivedMessageHolder(view);
case VIEW_TYPE_MESSAGE_SENT:
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_chat_sent, parent, false);
return new SentMessageHolder(view);
case VIEW_TYPE_DEBUG_RECEIVED:
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_debug, parent, false);
return new ReceivedDEBUGMessageHolder(view);
case VIEW_TYPE_MESSAGE_RECEIVED_LATEST:
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_chat_received_history, parent, false);
return new ReceivedLatestMessageHolder(view);
case VIEW_TYPE_MESSAGE_SENT_LATEST:
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_chat_sent_history, parent, false);
return new SentLatestMessageHolder(view);
case VIEW_TYPE_DEBUG_LATEST:
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_debug, parent, false);
return new ReceivedDEBUGMessageHolder(view);
default:
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_haohanyh, parent, false);
return new HaohanyhMessageHolder(view);
}
}

// 绑定数据到 ViewHolder
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ChatMessage message = chatMessages.get(position);
if (holder.getItemViewType() == VIEW_TYPE_MESSAGE_SENT) {
int viewType = holder.getItemViewType();

if (viewType == VIEW_TYPE_MESSAGE_SENT) {
((SentMessageHolder) holder).bind(message);
} else if (holder.getItemViewType() == VIEW_TYPE_DEBUG_RECEIVED) {
((ReceivedDEBUGMessageHolder) holder).bind(message);
} else {
} else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
((ReceivedMessageHolder) holder).bind(message);
} else if (viewType == VIEW_TYPE_DEBUG_RECEIVED) {
((ReceivedDEBUGMessageHolder) holder).bind(message);
} else if (viewType == VIEW_TYPE_MESSAGE_SENT_LATEST) {
((SentLatestMessageHolder) holder).bind(message);
} else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED_LATEST) {
((ReceivedLatestMessageHolder) holder).bind(message);
} else if (viewType == VIEW_TYPE_DEBUG_LATEST) {
((ReceivedDEBUGMessageHolder) holder).bind(message);
} else if (viewType == VIEW_TYPE_HAOHANYH) {
((HaohanyhMessageHolder) holder).bind(message);
}
}

Expand Down Expand Up @@ -103,6 +151,26 @@ void bind(ChatMessage message) {
}
}

// 接收消息的ViewHolder
private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
TextView messageText,timestampText;

ReceivedMessageHolder(View itemView) {
super(itemView);
messageText = itemView.findViewById(R.id.text_message_body);
timestampText = itemView.findViewById(R.id.text_message_time);

// 设置自定义字体
ChatUIFontUtils.applyCustomFont(context, messageText);
}

void bind(ChatMessage message) {
messageText.setText(message.getMessage());
timestampText.setText(message.getTimestamp());
}
}


// 接收DEBUG消息的ViewHolder
private class ReceivedDEBUGMessageHolder extends RecyclerView.ViewHolder {
TextView messageText;
Expand All @@ -118,7 +186,7 @@ private class ReceivedDEBUGMessageHolder extends RecyclerView.ViewHolder {
@Override
public boolean onLongClick(View view) {
String result = messageText.getText().toString().trim();

if (result.equals(ChatUtils.getPrefixLogConnected())) {
ChatUIAlertDialog.showSerialLog(context,
ChatUtils.getPrefixLogConnected(),
Expand Down Expand Up @@ -211,23 +279,96 @@ void bind(ChatMessage message) {
}
}

// 接收消息的ViewHolder
private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
// 存储于数据库的历史发送消息的ViewHolder
private class SentLatestMessageHolder extends RecyclerView.ViewHolder {
TextView messageText,timestampText;

ReceivedMessageHolder(View itemView) {
SentLatestMessageHolder(@NonNull View itemView) {
super(itemView);
messageText = itemView.findViewById(R.id.text_message_body);
timestampText = itemView.findViewById(R.id.text_message_time);

// 设置自定义字体
ChatUIFontUtils.applyCustomFont(context, messageText);
ChatUIFontUtils.applyCustomFont(context, messageText, 2);

// 设置长按监听器
timestampText.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(context, "这个历史消息是:" + messageText.getText().toString() + "\n" + timestampText.getText().toString(), Toast.LENGTH_LONG).show();
return true; // 返回true表示事件已处理
}
});

// 设置随机颜色
setRandomTextColor(timestampText);
}

void bind(ChatMessage message) {
messageText.setText(message.getMessage());
timestampText.setText(message.getTimestamp());
}

private void setRandomTextColor(TextView textView) {
Random random = new Random();
int red = random.nextInt(156) + 100; // 100-255
int green = random.nextInt(156) + 100; // 100-255
int blue = random.nextInt(156) + 100; // 100-255
int color = Color.rgb(red, green, blue);
textView.setTextColor(color);
}
}

// 存储于数据库的历史接收消息的ViewHolder
private class ReceivedLatestMessageHolder extends RecyclerView.ViewHolder {
TextView messageText,timestampText;

ReceivedLatestMessageHolder(@NonNull View itemView) {
super(itemView);
messageText = itemView.findViewById(R.id.text_message_body);
timestampText = itemView.findViewById(R.id.text_message_time);

// 设置自定义字体
ChatUIFontUtils.applyCustomFont(context, messageText, 2);

// 设置长按监听器
timestampText.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(context, "这个历史消息是:" + messageText.getText().toString() + "\n" + timestampText.getText().toString(), Toast.LENGTH_LONG).show();
return true; // 返回true表示事件已处理
}
});

// 设置随机颜色
setRandomTextColor(timestampText);
}

void bind(ChatMessage message) {
messageText.setText(message.getMessage());
timestampText.setText(message.getTimestamp());
}

private void setRandomTextColor(TextView textView) {
Random random = new Random();
int red = random.nextInt(156) + 100; // 100-255
int green = random.nextInt(156) + 100; // 100-255
int blue = random.nextInt(156) + 100; // 100-255
int color = Color.rgb(red, green, blue);
textView.setTextColor(color);
}
}

// 浩瀚银河预留的ViewHolder
private class HaohanyhMessageHolder extends RecyclerView.ViewHolder {

HaohanyhMessageHolder(@NonNull View itemView) {
super(itemView);
}

void bind(ChatMessage message) {

}
}

// 更新消息列表并滚动到底部
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ public class ChatMessage {
private String timestamp;
private int loglevel;
private boolean isUser;
private boolean isDebug;
private boolean isMe;
private boolean isDebug;
private boolean isSQLiteUser;
private boolean isSQLiteMe;
private boolean isSQLiteDebug;


/**
* 构造方法,适用于User消息
Expand All @@ -22,6 +26,18 @@ public ChatMessage(String message, String timestamp, boolean isUser) {
this.isUser = isUser;
}

/**
* 构造方法,适用于Me消息
* @param message Me消息
* @param isMe 传参
* @param timestamp Me消息时间
*/
public ChatMessage(String message, boolean isMe, String timestamp) {
this.message = message;
this.timestamp = timestamp;
this.isMe = isMe;
}

/**
* 构造方法,适用于Debug日志消息
* @param message Debug日志内容
Expand All @@ -34,16 +50,35 @@ public ChatMessage(String message, boolean isDebug, int loglevel) {
this.loglevel = loglevel;
}


/**
* 构造方法,适用于Me消息
* @param message Me消息
* @param isMe 传参
* @param timestamp Me消息时间
* 构造方法,适用于数据库历史消息
* @param message 内容,历史消息
* @param timestamp 历史消息时间
* @param latest 判断
* @param sqlite 识别为who,1为user历史,2为me历史,3为debug历史
*/
public ChatMessage(String message, boolean isMe, String timestamp) {
public ChatMessage(String message, String timestamp, boolean latest, int sqlite) {
this.message = message;
this.timestamp = timestamp;
this.isMe = isMe;

if (sqlite == 1) {
isSQLiteUser = latest;
isSQLiteMe = false;
isSQLiteDebug = false;
} else if (sqlite == 2) {
isSQLiteUser = false;
isSQLiteMe = latest;
isSQLiteDebug = false;
} else if (sqlite == 3) {
isSQLiteUser = false;
isSQLiteMe = false;
isSQLiteDebug = latest;
} else {
isSQLiteUser = false;
isSQLiteMe = false;
isSQLiteDebug = false;
}
}

public String getMessage() {
Expand All @@ -62,11 +97,23 @@ public boolean isUser() {
return isUser;
}

public boolean isMe() {
return isMe;
}

public boolean isDebug() {
return isDebug;
}

public boolean isMe() {
return isMe;
public boolean isSQLiteUser() {
return isSQLiteUser;
}

public boolean isSQLiteMe() {
return isSQLiteMe;
}

public boolean isSQLiteDebug() {
return isSQLiteDebug;
}
}
Loading

0 comments on commit 41f419f

Please sign in to comment.