Skip to content

Commit

Permalink
🎉Release 1.3,阅后即焚2.0(BETA),阅后即焚修复倒计时Emoji错误的问题,修复线程问题,调整ChatCore相关代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Hny0305Lin committed Aug 1, 2024
1 parent e1884c3 commit 0f82aa3
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.Emoji.EmojiTimerManager;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.R;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.R.string;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.SQLite.SQLiteDataBaseAPP;

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Random;

Expand Down Expand Up @@ -185,11 +187,12 @@ void bind(ChatUtilsForMessage message) {
}
}


private EmojiTimerManager emojiTimerManager = new EmojiTimerManager();

// 接收消息的ViewHolder(阅后即焚)
private class ReceivedMessageBurnHolder extends RecyclerView.ViewHolder {
TextView messageText,timestampText,emojiText;
private EmojiTimer emojiTimer;
private boolean isTimerRunning = false;

ReceivedMessageBurnHolder(View itemView) {
super(itemView);
Expand Down Expand Up @@ -236,42 +239,50 @@ void bind(ChatUtilsForMessage message) {
messageText.setText(message.getMessage());
timestampText.setText(message.getTimestamp());

// 启动Emoji计时器
if (!isTimerRunning) {
if (emojiTimer != null) {
emojiTimer.stopTimer();
}
emojiTimer = new EmojiTimer(emojiText);
emojiTimer.startTimer(ChatUtilsForSettings.getBurntimer()); // 2分钟倒计时
isTimerRunning = true;
}
// 使用EmojiTimerManager来管理和启动计时器
emojiTimerManager.startTimer(emojiText, ChatUtilsForSettings.getBurntimer()); // 2分钟倒计时

// 使用同一个Handler来处理UI更新和消息删除
Handler messageHandler = new Handler();
ReceivedMessageBurnHandler burnHandler = new ReceivedMessageBurnHandler(this, message);

// 设置消息2分钟后从数据源中删除并更新UI
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//Log
Log.d(TAG, "ReceivedMessageBurnHandler is running");
// 检查当前消息是否仍在数据源中
int currentIndex = chatUtilsForMessages.indexOf(message);
if (currentIndex != -1) {
// 从数据源中删除消息
chatUtilsForMessages.remove(currentIndex);
// 通知RecyclerView某项已被删除
notifyItemRemoved(currentIndex);
// 通知RecyclerView更新位置,防止位置错乱
notifyItemRangeChanged(currentIndex, chatUtilsForMessages.size());
}
messageHandler.postDelayed(burnHandler, ChatUtilsForSettings.getBurntimer()); // 延迟时间为120000毫秒,即2分钟
}
}

private class ReceivedMessageBurnHandler implements Runnable {
private final WeakReference<ReceivedMessageBurnHolder> holderWeakReference;
private final ChatUtilsForMessage message;

ReceivedMessageBurnHandler(ReceivedMessageBurnHolder holder, ChatUtilsForMessage message) {
this.holderWeakReference = new WeakReference<>(holder);
this.message = message;
}

@Override
public void run() {
ReceivedMessageBurnHolder holder = holderWeakReference.get();
if (holder != null && message != null) {
//Log
Log.d(TAG, "ReceivedMessageBurnHandler is running");
// 检查当前消息是否仍在数据源中
int currentIndex = chatUtilsForMessages.indexOf(message);
if (currentIndex != -1) {
// 从数据源中删除消息
chatUtilsForMessages.remove(currentIndex);
// 通知RecyclerView某项已被删除
notifyItemRemoved(currentIndex);
// 通知RecyclerView更新位置,防止位置错乱
notifyItemRangeChanged(currentIndex, chatUtilsForMessages.size());
}
}, ChatUtilsForSettings.getBurntimer()); // 延迟时间为120000毫秒,即2分钟
}
}
}

// 发送消息的ViewHolder(阅后即焚)
private class SentMessageBurnHolder extends RecyclerView.ViewHolder {
TextView messageText, timestampText, emojiText;
private EmojiTimer emojiTimer;
private boolean isTimerRunning = false;

SentMessageBurnHolder(View itemView) {
super(itemView);
Expand Down Expand Up @@ -317,34 +328,44 @@ void bind(ChatUtilsForMessage message) {
messageText.setText(message.getMessage());
timestampText.setText(message.getTimestamp());

// 启动Emoji计时器
if (!isTimerRunning) {
if (emojiTimer != null) {
emojiTimer.stopTimer();
}
emojiTimer = new EmojiTimer(emojiText);
emojiTimer.startTimer(ChatUtilsForSettings.getBurntimer()); // 2分钟倒计时
isTimerRunning = true;
}
// 使用EmojiTimerManager来管理和启动计时器
emojiTimerManager.startTimer(emojiText, ChatUtilsForSettings.getBurntimer()); // 2分钟倒计时

// 使用同一个Handler来处理UI更新和消息删除
Handler messageHandler = new Handler();
SentMessageBurnHandler burnHandler = new SentMessageBurnHandler(this, message);

// 设置消息2分钟后从数据源中删除并更新UI
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//Log
Log.d(TAG, "SentMessageBurnHandler is running");
// 检查当前消息是否仍在数据源中
int currentIndex = chatUtilsForMessages.indexOf(message);
if (currentIndex != -1) {
// 从数据源中删除消息
chatUtilsForMessages.remove(currentIndex);
// 通知RecyclerView某项已被删除
notifyItemRemoved(currentIndex);
// 通知RecyclerView更新位置,防止位置错乱
notifyItemRangeChanged(currentIndex, chatUtilsForMessages.size());
}
messageHandler.postDelayed(burnHandler, ChatUtilsForSettings.getBurntimer()); // 延迟时间为120000毫秒,即2分钟
}
}

private class SentMessageBurnHandler implements Runnable {
private final WeakReference<SentMessageBurnHolder> holderWeakReference;
private final ChatUtilsForMessage message;

SentMessageBurnHandler(SentMessageBurnHolder holder, ChatUtilsForMessage message) {
this.holderWeakReference = new WeakReference<>(holder);
this.message = message;
}

@Override
public void run() {
SentMessageBurnHolder holder = holderWeakReference.get();
if (holder != null && message != null) {
//Log
Log.d(TAG, "SentMessageBurnHandler is running");
// 检查当前消息是否仍在数据源中
int currentIndex = chatUtilsForMessages.indexOf(message);
if (currentIndex != -1) {
// 从数据源中删除消息
chatUtilsForMessages.remove(currentIndex);
// 通知RecyclerView某项已被删除
notifyItemRemoved(currentIndex);
// 通知RecyclerView更新位置,防止位置错乱
notifyItemRangeChanged(currentIndex, chatUtilsForMessages.size());
}
}, ChatUtilsForSettings.getBurntimer()); // 延迟时间为120000毫秒,即2分钟
}
}
}

Expand Down Expand Up @@ -553,7 +574,7 @@ void bind(ChatUtilsForMessage message) {
@SuppressLint("NotifyDataSetChanged")
public void updateMessages(List<ChatUtilsForMessage> newMessages, RecyclerView recyclerView) {
this.chatUtilsForMessages = newMessages;
notifyDataSetChanged();
//notifyDataSetChanged();
recyclerView.scrollToPosition(chatUtilsForMessages.size() - 1);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore;
package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.Emoji;

public class EmojiClock {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.Emoji;

import android.os.CountDownTimer;
import android.widget.TextView;
import java.util.HashMap;
import java.util.Map;

public class EmojiTimerManager {
private Map<TextView, EmojiTimer> activeTimers = new HashMap<>();

public void startTimer(TextView textView, long durationInMillis) {
// 停止当前正在textView上运行的计时器(如果有)
if (activeTimers.containsKey(textView)) {
activeTimers.get(textView).stopTimer();
}

EmojiTimer timer = new EmojiTimer(textView, this);
activeTimers.put(textView, timer);
timer.startTimer(durationInMillis);
}

void removeTimer(EmojiTimer timer) {
activeTimers.remove(timer.getTextView());
}

public class EmojiTimer {
private TextView textView;
private CountDownTimer countDownTimer;
private EmojiTimerManager manager;

public EmojiTimer(TextView textView, EmojiTimerManager manager) {
this.textView = textView;
this.manager = manager;
}

public void startTimer(long durationInMillis) {
countDownTimer = new CountDownTimer(durationInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
updateTextView(millisUntilFinished);
}

@Override
public void onFinish() {
textView.setText(EmojiClock.getEmojiForMinute(0) + EmojiClock.getEmojiForSecond(0));
manager.removeTimer(EmojiTimer.this); // 自动从管理器中移除
}
};
countDownTimer.start();
}

public void stopTimer() {
if (countDownTimer != null) {
countDownTimer.cancel();
countDownTimer = null;
}
}

private void updateTextView(long millisUntilFinished) {
int totalSeconds = (int) (millisUntilFinished / 1000);
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;

String minuteEmoji = EmojiClock.getEmojiForMinute(minutes);
String secondEmoji = EmojiClock.getEmojiForSecond(seconds);

textView.setText(minuteEmoji + secondEmoji);
}

public TextView getTextView() {
return textView;
}
}
}

This file was deleted.

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.3.340.2024.0730</string>
<string name="app_version">1.3.350.2024.0801</string>

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

0 comments on commit 0f82aa3

Please sign in to comment.