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:
jack
2025-07-02 21:19:32 +02:00
parent fc5eba4a58
commit 13686b3ce9
3 changed files with 31 additions and 13 deletions
+1 -4
View File
@@ -226,10 +226,7 @@ class BluetoothMeshService: NSObject {
print("[DEBUG] Sending private message to \(recipientNickname): \(content)")
self.broadcastPacket(packet)
// Also show the message locally
DispatchQueue.main.async {
self.delegate?.didReceiveMessage(message)
}
// Don't call didReceiveMessage here - let the view model handle it directly
}
}
}
+29 -8
View File
@@ -76,6 +76,27 @@ class ChatViewModel: ObservableObject {
guard !content.isEmpty 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
meshService.sendPrivateMessage(content, to: peerID, recipientNickname: recipientNickname)
}
@@ -182,6 +203,9 @@ extension ChatViewModel: BitchatDelegate {
}
privateChats[peerID]?.append(message)
// Trigger UI update for private chats
objectWillChange.send()
// Mark as unread if not currently viewing this chat
if selectedPrivateChatPeer != peerID {
unreadPrivateMessages.insert(peerID)
@@ -189,16 +213,13 @@ extension ChatViewModel: BitchatDelegate {
// Show notification banner
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 {
// Our own message - find recipient by nickname
if let recipientNickname = message.recipientNickname,
let recipientPeerID = getPeerIDForNickname(recipientNickname) {
if privateChats[recipientPeerID] == nil {
privateChats[recipientPeerID] = []
}
privateChats[recipientPeerID]?.append(message)
}
// Our own message that was echoed back - ignore it since we already added it locally
print("[DEBUG] Ignoring our own private message echo")
}
} else {
// Regular public message
+1 -1
View File
@@ -61,7 +61,7 @@ struct ContentView: View {
}
.padding(.top, 60)
.transition(.move(edge: .top).combined(with: .opacity))
.animation(.easeInOut, value: viewModel.privateMessageNotification)
.animation(.easeInOut, value: viewModel.privateMessageNotification != nil)
}
}
#if os(macOS)