Skip to content

Commit

Permalink
🎉Alpha 1.3,新UI聊天界面(BETA)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hny0305Lin committed Jul 3, 2024
1 parent 570d087 commit fb9192e
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

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

import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;

private List<ChatMessage> chatMessages;

public ChatAdapter(List<ChatMessage> chatMessages) {
this.chatMessages = chatMessages;
}

@Override
public int getItemViewType(int position) {
ChatMessage message = chatMessages.get(position);
if (message.isSent()) {
return VIEW_TYPE_MESSAGE_SENT;
} else {
return VIEW_TYPE_MESSAGE_RECEIVED;
}
}

@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 {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_chat_received, parent, false);
return new ReceivedMessageHolder(view);
}
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ChatMessage message = chatMessages.get(position);
if (holder.getItemViewType() == VIEW_TYPE_MESSAGE_SENT) {
((SentMessageHolder) holder).bind(message);
} else {
((ReceivedMessageHolder) holder).bind(message);
}
}

@Override
public int getItemCount() {
return chatMessages.size();
}

private class SentMessageHolder extends RecyclerView.ViewHolder {
TextView messageText;

SentMessageHolder(View itemView) {
super(itemView);
messageText = itemView.findViewById(R.id.text_message_body);
}

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

private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
TextView messageText;

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

void bind(ChatMessage message) {
messageText.setText(message.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore;

public class ChatMessage {
private String message;
private boolean isSent;

public ChatMessage(String message, boolean isSent) {
this.message = message;
this.isSent = isSent;
}

public String getMessage() {
return message;
}

public boolean isSent() {
return isSent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.clans.fab.FloatingActionMenu;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.haohanyh.linmengjia.nearlink.nlchat.ch34x.CH34xUARTDriver;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatAdapter;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatMessage;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatProcessor;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatUtils;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.Premission.NearLinkChatGetSomePermission;
Expand All @@ -61,8 +65,10 @@

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Expand Down Expand Up @@ -101,6 +107,11 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private final int[] dataBitIds = {id.rbData5, id.rbData6, id.rbData7, id.rbData8};
private final int[] stopBitIds = {id.rbStop1, id.rbStop2};
private final int[] parityIds = {id.rbParityNone, id.rbParityOdd, id.rbParityEven, id.rbParityMark, id.rbParitySpace};
//聊天UI 1.3更新
private RecyclerView recyclerView;
private ChatAdapter chatAdapter;
private List<ChatMessage> chatMessages;

//Context
private Context context = MainActivity.this;

Expand Down Expand Up @@ -267,6 +278,13 @@ private void Init() {
radioButton.setOnCheckedChangeListener(createCheckedChangeListener(i, "Parity"));
}

//聊天UI 1.3更新
recyclerView = findViewById(id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
chatMessages = new ArrayList<>();
chatAdapter = new ChatAdapter(chatMessages);
recyclerView.setAdapter(chatAdapter);

//初始化完成,软件第一次启动必须提示(这里写的第一次启动是软件启动的第一次,而不是使用频率的第一次
HhandlerI.sendEmptyMessage(31);
//如果SQLite有记录,可以显示在UI上
Expand Down Expand Up @@ -406,6 +424,7 @@ private void NearLinkChatReadData() {
});
}

@SuppressLint("NotifyDataSetChanged")
private void updateServerTextView() {
StringBuilder allMessages = new StringBuilder();
Iterator<String> iterator = serverMessageQueue.iterator();
Expand All @@ -420,6 +439,8 @@ private void updateServerTextView() {
}
}
NearLinkServerText.setText(allMessages.toString());
// chatMessages.add(new ChatMessage(allMessages.toString(), false));
// chatAdapter.notifyDataSetChanged();
Log.v(TAG, "消息队列在User上有改动");
}

Expand Down Expand Up @@ -509,6 +530,7 @@ public void NearLinkChatSendData(View view) {
}
}

@SuppressLint("NotifyDataSetChanged")
private void updateClientTextView() {
StringBuilder allMessages = new StringBuilder();
Iterator<String> iterator = clientMessageQueue.iterator();
Expand All @@ -523,6 +545,8 @@ private void updateClientTextView() {
}
}
NearLinkClientText.setText(allMessages.toString());
// chatMessages.add(new ChatMessage(allMessages.toString(), true));
// chatAdapter.notifyDataSetChanged();
Log.v(TAG, "消息队列在Me上有改动");
}

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/bg_chat_bubble_received.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#E5E5EA" />
<corners android:radius="16dp" />
<padding android:left="8dp" android:top="8dp" android:right="8dp" android:bottom="8dp" />
</shape>
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/bg_chat_bubble_sent.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#0084FF" />
<corners android:radius="16dp" />
<padding android:left="8dp" android:top="8dp" android:right="8dp" android:bottom="8dp" />
</shape>
18 changes: 18 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@

</LinearLayout>

<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="15dp"
android:text="New UI"
android:fontFamily="sans-serif-medium"
android:textColor="@color/blue_biaozhun"
tools:ignore="HardcodedText" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/layout/item_chat_received.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
android:gravity="start">

<TextView
android:id="@+id/text_message_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_chat_bubble_received"
android:padding="10dp"
android:textColor="@android:color/black"
android:text="Hello, this is a received message!" />
</LinearLayout>
17 changes: 17 additions & 0 deletions app/src/main/res/layout/item_chat_sent.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
android:gravity="end">

<TextView
android:id="@+id/text_message_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_chat_bubble_sent"
android:padding="10dp"
android:textColor="@android:color/white"
android:text="Hello, this is a sent message!" />
</LinearLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>
<string name="app_name">NLChat</string>
<string name="app_package">com.haohanyh.linmengjia.nearlink.nlchat.fun</string>
<string name="app_version">1.2.93.2024.0703</string>
<string name="app_version">1.3.1.2024.0703</string>

<string name="appwarn">NLChat,浩瀚银河宗旨为用爱和魔法创造Android APP。</string>
<string name="thanks3q">友情感谢</string>
Expand Down

0 comments on commit fb9192e

Please sign in to comment.