Fix compilation errors in ChatViewModel

- Fixed PeerData type reference (should be BitchatPeer)
- Fixed Task initialization in init() method with proper async/await syntax
- Fixed cancellables binding issue with proper unwrapping
- Maintained duplicate peer ID protection in peerIndex dictionary
This commit is contained in:
jack
2025-08-01 20:24:09 +02:00
parent d605a0b6db
commit e2d50e3684
+20 -9
View File
@@ -194,21 +194,28 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
MessageRetryService.shared.meshService = meshService MessageRetryService.shared.meshService = meshService
// Initialize Nostr services // Initialize Nostr services
Task { @MainActor in Task { [weak self] in
nostrRelayManager = NostrRelayManager.shared await MainActor.run {
nostrRelayManager?.connect() self?.nostrRelayManager = NostrRelayManager.shared
self?.nostrRelayManager?.connect()
messageRouter = MessageRouter( if let meshService = self?.meshService,
let nostrRelayManager = self?.nostrRelayManager {
self?.messageRouter = MessageRouter(
meshService: meshService, meshService: meshService,
nostrRelay: nostrRelayManager! nostrRelay: nostrRelayManager
) )
}
// Initialize peer manager // Initialize peer manager
peerManager = PeerManager(meshService: meshService) if let meshService = self?.meshService {
peerManager?.updatePeers() self?.peerManager = PeerManager(meshService: meshService)
self?.peerManager?.updatePeers()
// Bind peer manager's peer list to our published property // Bind peer manager's peer list to our published property
peerManager?.$peers if let peerManager = self?.peerManager,
var cancellables = self?.cancellables {
peerManager.$peers
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [weak self] peers in .sink { [weak self] peers in
SecureLogger.log("📱 UI: Received \(peers.count) peers from PeerManager", SecureLogger.log("📱 UI: Received \(peers.count) peers from PeerManager",
@@ -221,7 +228,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
self?.allPeers = peers self?.allPeers = peers
// Update peer index for O(1) lookups // Update peer index for O(1) lookups
// Deduplicate peers by ID to prevent crash from duplicate keys // Deduplicate peers by ID to prevent crash from duplicate keys
var uniquePeers: [String: PeerData] = [:] var uniquePeers: [String: BitchatPeer] = [:]
for peer in peers { for peer in peers {
// Keep the first occurrence of each peer ID // Keep the first occurrence of each peer ID
if uniquePeers[peer.id] == nil { if uniquePeers[peer.id] == nil {
@@ -243,6 +250,10 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
} }
} }
.store(in: &cancellables) .store(in: &cancellables)
self?.cancellables = cancellables
}
}
}
} }
// Set up Noise encryption callbacks // Set up Noise encryption callbacks