mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 03:45:20 +00:00
Fix private message notifications and chat updates
- Remove self-notification when sending private messages - Clear unread indicator when viewing active chat - Properly update UI when private messages are sent/received - Prevent duplicate messages in chat history - Use objectWillChange to force SwiftUI updates
This commit is contained in:
@@ -226,10 +226,7 @@ class BluetoothMeshService: NSObject {
|
|||||||
print("[DEBUG] Sending private message to \(recipientNickname): \(content)")
|
print("[DEBUG] Sending private message to \(recipientNickname): \(content)")
|
||||||
self.broadcastPacket(packet)
|
self.broadcastPacket(packet)
|
||||||
|
|
||||||
// Also show the message locally
|
// Don't call didReceiveMessage here - let the view model handle it directly
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.delegate?.didReceiveMessage(message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,6 +76,27 @@ class ChatViewModel: ObservableObject {
|
|||||||
guard !content.isEmpty else { return }
|
guard !content.isEmpty else { return }
|
||||||
guard let recipientNickname = meshService.getPeerNicknames()[peerID] else { return }
|
guard let recipientNickname = meshService.getPeerNicknames()[peerID] else { return }
|
||||||
|
|
||||||
|
// Create the message locally
|
||||||
|
let message = BitchatMessage(
|
||||||
|
sender: nickname,
|
||||||
|
content: content,
|
||||||
|
timestamp: Date(),
|
||||||
|
isRelay: false,
|
||||||
|
originalSender: nil,
|
||||||
|
isPrivate: true,
|
||||||
|
recipientNickname: recipientNickname,
|
||||||
|
senderPeerID: meshService.myPeerID
|
||||||
|
)
|
||||||
|
|
||||||
|
// Add to our private chat history
|
||||||
|
if privateChats[peerID] == nil {
|
||||||
|
privateChats[peerID] = []
|
||||||
|
}
|
||||||
|
privateChats[peerID]?.append(message)
|
||||||
|
|
||||||
|
// Trigger UI update
|
||||||
|
objectWillChange.send()
|
||||||
|
|
||||||
// Send via mesh
|
// Send via mesh
|
||||||
meshService.sendPrivateMessage(content, to: peerID, recipientNickname: recipientNickname)
|
meshService.sendPrivateMessage(content, to: peerID, recipientNickname: recipientNickname)
|
||||||
}
|
}
|
||||||
@@ -182,6 +203,9 @@ extension ChatViewModel: BitchatDelegate {
|
|||||||
}
|
}
|
||||||
privateChats[peerID]?.append(message)
|
privateChats[peerID]?.append(message)
|
||||||
|
|
||||||
|
// Trigger UI update for private chats
|
||||||
|
objectWillChange.send()
|
||||||
|
|
||||||
// Mark as unread if not currently viewing this chat
|
// Mark as unread if not currently viewing this chat
|
||||||
if selectedPrivateChatPeer != peerID {
|
if selectedPrivateChatPeer != peerID {
|
||||||
unreadPrivateMessages.insert(peerID)
|
unreadPrivateMessages.insert(peerID)
|
||||||
@@ -189,16 +213,13 @@ extension ChatViewModel: BitchatDelegate {
|
|||||||
|
|
||||||
// Show notification banner
|
// Show notification banner
|
||||||
showPrivateMessageNotification(from: message.sender, content: message.content)
|
showPrivateMessageNotification(from: message.sender, content: message.content)
|
||||||
|
} else {
|
||||||
|
// We're viewing this chat, make sure unread is cleared
|
||||||
|
unreadPrivateMessages.remove(peerID)
|
||||||
}
|
}
|
||||||
} else if message.sender == nickname {
|
} else if message.sender == nickname {
|
||||||
// Our own message - find recipient by nickname
|
// Our own message that was echoed back - ignore it since we already added it locally
|
||||||
if let recipientNickname = message.recipientNickname,
|
print("[DEBUG] Ignoring our own private message echo")
|
||||||
let recipientPeerID = getPeerIDForNickname(recipientNickname) {
|
|
||||||
if privateChats[recipientPeerID] == nil {
|
|
||||||
privateChats[recipientPeerID] = []
|
|
||||||
}
|
|
||||||
privateChats[recipientPeerID]?.append(message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Regular public message
|
// Regular public message
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
.padding(.top, 60)
|
.padding(.top, 60)
|
||||||
.transition(.move(edge: .top).combined(with: .opacity))
|
.transition(.move(edge: .top).combined(with: .opacity))
|
||||||
.animation(.easeInOut, value: viewModel.privateMessageNotification)
|
.animation(.easeInOut, value: viewModel.privateMessageNotification != nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
|
|||||||
Reference in New Issue
Block a user