From 1eaba703e9e59fcc37a0d103993eabd463515af7 Mon Sep 17 00:00:00 2001 From: jack Date: Wed, 2 Jul 2025 21:39:31 +0200 Subject: [PATCH] Fix peer dropdown layout and announcement timing - Move peer dropdown next to bitchat title - Add immediate peer announcement on connection - Fix iOS menu chevron visibility with platform-specific styling - Force UI updates for join/leave messages - Add peers to active list immediately on connection - Remove all announcement delays for instant name resolution --- bitchat/Services/BluetoothMeshService.swift | 54 ++++++++++----------- bitchat/ViewModels/ChatViewModel.swift | 6 +++ bitchat/Views/ContentView.swift | 21 +++++--- 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/bitchat/Services/BluetoothMeshService.swift b/bitchat/Services/BluetoothMeshService.swift index 9d46c2d7..71a5b27b 100644 --- a/bitchat/Services/BluetoothMeshService.swift +++ b/bitchat/Services/BluetoothMeshService.swift @@ -559,6 +559,9 @@ extension BluetoothMeshService: CBCentralManagerDelegate { connectedPeripherals[peerID] = peripheral print("[DEBUG] Connected to peer: \(peerID)") + + // Add to active peers immediately + activePeers.insert(peerID) print("[DEBUG] Active peers: \(activePeers)") // Update peer list to show we're connecting @@ -622,43 +625,36 @@ extension BluetoothMeshService: CBPeripheralDelegate { peripheral.setNotifyValue(true, for: characteristic) peripheralCharacteristics[peripheral] = characteristic - // Send key exchange and announce immediately - DispatchQueue.main.async { [weak self] in - guard let self = self else { return } - - let publicKeyData = self.encryptionService.publicKey.rawRepresentation - let packet = BitchatPacket( - type: MessageType.keyExchange.rawValue, + // Send key exchange and announce immediately without any delay + let publicKeyData = self.encryptionService.publicKey.rawRepresentation + let packet = BitchatPacket( + type: MessageType.keyExchange.rawValue, + senderID: self.myPeerID.data(using: .utf8)!, + recipientID: nil, + timestamp: UInt64(Date().timeIntervalSince1970), + payload: try! JSONEncoder().encode(publicKeyData), + signature: nil, + ttl: 1 + ) + + if let data = packet.data { + peripheral.writeValue(data, for: characteristic, type: .withResponse) + } + + // Send announce packet immediately after key exchange + if let vm = self.delegate as? ChatViewModel { + let announcePacket = BitchatPacket( + type: MessageType.announce.rawValue, senderID: self.myPeerID.data(using: .utf8)!, recipientID: nil, timestamp: UInt64(Date().timeIntervalSince1970), - payload: try! JSONEncoder().encode(publicKeyData), + payload: vm.nickname.data(using: .utf8)!, signature: nil, ttl: 1 ) - - if let data = packet.data { + if let data = announcePacket.data { peripheral.writeValue(data, for: characteristic, type: .withResponse) } - - // Send announce packet immediately after key exchange - if let vm = self.delegate as? ChatViewModel { - DispatchQueue.main.async { [weak self] in - guard let self = self else { return } - let announcePacket = BitchatPacket( - type: MessageType.announce.rawValue, - senderID: self.myPeerID.data(using: .utf8)!, - recipientID: nil, - timestamp: UInt64(Date().timeIntervalSince1970), - payload: vm.nickname.data(using: .utf8)!, - signature: nil, - ttl: 1 - ) - if let data = announcePacket.data { - peripheral.writeValue(data, for: characteristic, type: .withResponse) - } - } - } } } } diff --git a/bitchat/ViewModels/ChatViewModel.swift b/bitchat/ViewModels/ChatViewModel.swift index 9106dcaa..232e59e1 100644 --- a/bitchat/ViewModels/ChatViewModel.swift +++ b/bitchat/ViewModels/ChatViewModel.swift @@ -252,6 +252,9 @@ extension ChatViewModel: BitchatDelegate { ) messages.append(systemMessage) print("[DEBUG] Added join message, total messages: \(messages.count)") + + // Force UI update + objectWillChange.send() } func didDisconnectFromPeer(_ peerID: String) { @@ -265,6 +268,9 @@ extension ChatViewModel: BitchatDelegate { ) messages.append(systemMessage) print("[DEBUG] Added leave message, total messages: \(messages.count)") + + // Force UI update + objectWillChange.send() } func didUpdatePeerList(_ peers: [String]) { diff --git a/bitchat/Views/ContentView.swift b/bitchat/Views/ContentView.swift index 35b156f2..e6e8aae6 100644 --- a/bitchat/Views/ContentView.swift +++ b/bitchat/Views/ContentView.swift @@ -106,14 +106,15 @@ struct ContentView: View { .opacity(0) } else { // Public chat header - Text("bitchat") - .font(.system(size: 18, weight: .medium, design: .monospaced)) - .foregroundColor(textColor) - - Spacer() - - // Peer status section - peerStatusView + HStack(spacing: 12) { + Text("bitchat") + .font(.system(size: 18, weight: .medium, design: .monospaced)) + .foregroundColor(textColor) + + // Peer status section + peerStatusView + .frame(maxWidth: 120) + } Spacer() @@ -188,7 +189,11 @@ struct ContentView: View { } } } + #if os(macOS) .menuStyle(.borderlessButton) + #else + .menuStyle(.automatic) + #endif } private var messagesView: some View {