diff --git a/bitchat/ViewModels/ChatViewModel.swift b/bitchat/ViewModels/ChatViewModel.swift index fd4fe434..c8be5787 100644 --- a/bitchat/ViewModels/ChatViewModel.swift +++ b/bitchat/ViewModels/ChatViewModel.swift @@ -1473,6 +1473,17 @@ class ChatViewModel: ObservableObject, BitchatDelegate { // MARK: - Public helpers (iOS) #if os(iOS) + /// Return the current, pruned, sorted people list for the active geohash without mutating state. + @MainActor + func visibleGeohashPeople() -> [GeoPerson] { + guard let gh = currentGeohash else { return [] } + let cutoff = Date().addingTimeInterval(-5 * 60) + let map = (geoParticipants[gh] ?? [:]).filter { $0.value >= cutoff } + let people = map + .map { (pub, seen) in GeoPerson(id: pub, displayName: displayNameForNostrPubkey(pub), lastSeen: seen) } + .sorted { $0.lastSeen > $1.lastSeen } + return people + } /// Returns the current participant count for a specific geohash, using the 5-minute activity window. @MainActor func geohashParticipantCount(for geohash: String) -> Int { diff --git a/bitchat/Views/ContentView.swift b/bitchat/Views/ContentView.swift index caa276a8..8940663d 100644 --- a/bitchat/Views/ContentView.swift +++ b/bitchat/Views/ContentView.swift @@ -871,8 +871,12 @@ struct ContentView: View { // Unread icon immediately to the left of the channel badge (independent from channel button) #if os(iOS) let cc = channelPeopleCountAndColor() - let otherPeersCount = cc.0 + var otherPeersCount = cc.0 let countColor = cc.1 + // Ensure geohash count matches the actual pruned list used in the sidebar + if case .location = locationManager.selectedChannel { + otherPeersCount = viewModel.visibleGeohashPeople().count + } #else let peerCounts = viewModel.allPeers.reduce(into: (others: 0, mesh: 0)) { counts, peer in guard peer.id != viewModel.meshService.myPeerID else { return } diff --git a/bitchat/Views/GeohashPeopleList.swift b/bitchat/Views/GeohashPeopleList.swift index 69528c61..e5c5065c 100644 --- a/bitchat/Views/GeohashPeopleList.swift +++ b/bitchat/Views/GeohashPeopleList.swift @@ -24,7 +24,7 @@ struct GeohashPeopleList: View { } return nil }() - let ordered = viewModel.geohashPeople.sorted { a, b in + let ordered = viewModel.visibleGeohashPeople().sorted { a, b in if let me = myHex { if a.id == me && b.id != me { return true } if b.id == me && a.id != me { return false }