mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-24 23:45:18 +00:00
Fix read receipts for existing messages when opening chat
- Added delay to ensure messages are loaded before checking - Fixed logic to check if message is FROM the other person (not TO them) - Added onAppear handler to send read receipts when view loads - Added detailed logging to debug the flow - Now correctly identifies received messages that need read receipts
This commit is contained in:
@@ -879,16 +879,23 @@ class ChatViewModel: ObservableObject {
|
||||
}
|
||||
|
||||
func startPrivateChat(with peerID: String) {
|
||||
print("[Delivery] Starting private chat with peer \(peerID)")
|
||||
selectedPrivateChatPeer = peerID
|
||||
unreadPrivateMessages.remove(peerID)
|
||||
|
||||
// Initialize chat history if needed
|
||||
if privateChats[peerID] == nil {
|
||||
privateChats[peerID] = []
|
||||
print("[Delivery] Initialized empty chat history for peer \(peerID)")
|
||||
} else {
|
||||
print("[Delivery] Found existing chat history with \(privateChats[peerID]?.count ?? 0) messages for peer \(peerID)")
|
||||
}
|
||||
|
||||
// Send read receipts for unread messages from this peer
|
||||
markPrivateMessagesAsRead(from: peerID)
|
||||
// Add a small delay to ensure UI has updated
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
|
||||
self?.markPrivateMessagesAsRead(from: peerID)
|
||||
}
|
||||
}
|
||||
|
||||
func endPrivateChat() {
|
||||
@@ -901,15 +908,15 @@ class ChatViewModel: ObservableObject {
|
||||
return
|
||||
}
|
||||
|
||||
print("[Delivery] Checking \(messages.count) messages from peer \(peerID) for read receipts")
|
||||
print("[Delivery] Checking \(messages.count) messages in chat with peer \(peerID) for read receipts")
|
||||
|
||||
// Find messages from the peer that haven't been read yet
|
||||
for message in messages {
|
||||
// Only send read receipts for messages from the other peer (not our own)
|
||||
// and only if the status is delivered (not already read)
|
||||
print("[Delivery] Message \(message.id) from \(message.sender), senderPeerID: \(message.senderPeerID ?? "nil"), status: \(message.deliveryStatus?.displayText ?? "none")")
|
||||
print("[Delivery] Message \(message.id) from \(message.sender), senderPeerID: \(message.senderPeerID ?? "nil"), myNickname: \(nickname), status: \(message.deliveryStatus?.displayText ?? "none")")
|
||||
|
||||
if message.senderPeerID == peerID {
|
||||
// Check if this is a message FROM the other person TO us
|
||||
if message.sender != nickname && message.senderPeerID != nil {
|
||||
if let status = message.deliveryStatus {
|
||||
switch status {
|
||||
case .sent, .delivered:
|
||||
|
||||
@@ -531,7 +531,18 @@ struct ContentView: View {
|
||||
.onChange(of: viewModel.selectedPrivateChatPeer) { newPeerID in
|
||||
// When switching to a private chat, send read receipts
|
||||
if let peerID = newPeerID {
|
||||
viewModel.markPrivateMessagesAsRead(from: peerID)
|
||||
// Small delay to ensure messages are loaded
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
||||
viewModel.markPrivateMessagesAsRead(from: peerID)
|
||||
}
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
// Also check when view appears
|
||||
if let peerID = viewModel.selectedPrivateChatPeer {
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
||||
viewModel.markPrivateMessagesAsRead(from: peerID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user