From cfded8c4df28631d7cc54c1ecc6fc7650ca893f4 Mon Sep 17 00:00:00 2001 From: jack Date: Sat, 5 Jul 2025 10:48:58 +0200 Subject: [PATCH] Fix room UI and membership tracking issues - Add lock icon to room header for password management - Change "leave" to "leave room" for clarity - Fix bug where non-members appeared in room member list - Only process room messages if user has joined the room - Initialize room data structures on app launch - Prevent auto-join when just mentioning a room --- bitchat/ViewModels/ChatViewModel.swift | 60 +++++++++++++++----------- bitchat/Views/ContentView.swift | 28 +++++++++++- 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/bitchat/ViewModels/ChatViewModel.swift b/bitchat/ViewModels/ChatViewModel.swift index 9050b0fa..2f44ab78 100644 --- a/bitchat/ViewModels/ChatViewModel.swift +++ b/bitchat/ViewModels/ChatViewModel.swift @@ -112,6 +112,15 @@ class ChatViewModel: ObservableObject { private func loadJoinedRooms() { if let savedRooms = userDefaults.stringArray(forKey: joinedRoomsKey) { joinedRooms = Set(savedRooms) + // Initialize empty data structures for joined rooms + for room in joinedRooms { + if roomMessages[room] == nil { + roomMessages[room] = [] + } + if roomMembers[room] == nil { + roomMembers[room] = [] + } + } } } @@ -856,32 +865,33 @@ extension ChatViewModel: BitchatDelegate { } else if let room = message.room { // Room message - // Auto-join room if we see a message for it - if !joinedRooms.contains(room) { - joinRoom(room) - } - - // Add to room messages - if roomMessages[room] == nil { - roomMessages[room] = [] - } - roomMessages[room]?.append(message) - roomMessages[room]?.sort { $0.timestamp < $1.timestamp } - - // Track room members - only track the sender as a member - if roomMembers[room] == nil { - roomMembers[room] = [] - } - if let senderPeerID = message.senderPeerID { - roomMembers[room]?.insert(senderPeerID) - bitchatLog("Added member \(senderPeerID) to room \(room), total members: \(roomMembers[room]?.count ?? 0)", category: "room") + // Only process room messages if we've joined this room + if joinedRooms.contains(room) { + // Add to room messages + if roomMessages[room] == nil { + roomMessages[room] = [] + } + roomMessages[room]?.append(message) + roomMessages[room]?.sort { $0.timestamp < $1.timestamp } + + // Track room members - only track the sender as a member + if roomMembers[room] == nil { + roomMembers[room] = [] + } + if let senderPeerID = message.senderPeerID { + roomMembers[room]?.insert(senderPeerID) + bitchatLog("Added member \(senderPeerID) to room \(room), total members: \(roomMembers[room]?.count ?? 0)", category: "room") + } else { + bitchatLog("No senderPeerID for message in room \(room)", category: "room") + } + + // Update unread count if not currently viewing this room + if currentRoom != room { + unreadRoomMessages[room] = (unreadRoomMessages[room] ?? 0) + 1 + } } else { - bitchatLog("No senderPeerID for message in room \(room)", category: "room") - } - - // Update unread count if not currently viewing this room - if currentRoom != room { - unreadRoomMessages[room] = (unreadRoomMessages[room] ?? 0) + 1 + // We're not in this room, ignore the message + bitchatLog("Ignoring message for room \(room) - not joined", category: "room") } } else { // Regular public message (main chat) diff --git a/bitchat/Views/ContentView.swift b/bitchat/Views/ContentView.swift index a3435235..45376d0b 100644 --- a/bitchat/Views/ContentView.swift +++ b/bitchat/Views/ContentView.swift @@ -259,11 +259,35 @@ struct ContentView: View { Spacer() + // Password button for room creator + if viewModel.roomCreators[currentRoom] == viewModel.meshService.myPeerID || viewModel.roomCreators[currentRoom] == nil { + Button(action: { + // Toggle password protection + if viewModel.passwordProtectedRooms.contains(currentRoom) { + viewModel.removeRoomPassword(for: currentRoom) + } else { + // Show password input + showPasswordInput = true + passwordInputRoom = currentRoom + } + }) { + Image(systemName: viewModel.passwordProtectedRooms.contains(currentRoom) ? "lock.open" : "lock") + .font(.system(size: 14)) + .foregroundColor(secondaryTextColor) + .padding(6) + .overlay( + RoundedRectangle(cornerRadius: 4) + .stroke(secondaryTextColor.opacity(0.5), lineWidth: 1) + ) + } + .buttonStyle(.plain) + } + // Leave room button Button(action: { viewModel.leaveRoom(currentRoom) }) { - Text("leave") + Text("leave room") .font(.system(size: 12, design: .monospaced)) .foregroundColor(Color.red) .padding(.horizontal, 8) @@ -611,7 +635,7 @@ struct ContentView: View { Button(action: { viewModel.leaveRoom(room) }) { - Text("leave") + Text("leave room") .font(.system(size: 10, design: .monospaced)) .foregroundColor(secondaryTextColor) .padding(.horizontal, 8)