@@ -264,20 +264,26 @@ fun PeopleSection(
264264 val privateChats by viewModel.privateChats.observeAsState(emptyMap())
265265 val favoritePeers by viewModel.favoritePeers.observeAsState(emptySet())
266266
267+ // Pre-calculate all favorite states to ensure proper state synchronization
268+ val peerFavoriteStates = remember(favoritePeers, connectedPeers) {
269+ connectedPeers.associateWith { peerID ->
270+ val fingerprint = viewModel.privateChatManager.getPeerFingerprint(peerID)
271+ favoritePeers.contains(fingerprint)
272+ }
273+ }
274+
275+ Log .d(" SidebarComponents" , " Recomposing with ${favoritePeers.size} favorites, peer states: $peerFavoriteStates " )
276+
267277 // Smart sorting: unread DMs first, then by most recent DM, then favorites, then alphabetical
268278 val sortedPeers = connectedPeers.sortedWith(
269279 compareBy<String > { ! hasUnreadPrivateMessages.contains(it) } // Unread DM senders first
270280 .thenByDescending { privateChats[it]?.maxByOrNull { msg -> msg.timestamp }?.timestamp?.time ? : 0L } // Most recent DM (convert Date to Long)
271- .thenBy {
272- val fingerprint = viewModel.privateChatManager.getPeerFingerprint(it)
273- fingerprint == null || ! favoritePeers.contains(fingerprint)
274- } // Favorites
281+ .thenBy { ! (peerFavoriteStates[it] ? : false ) } // Favorites first
275282 .thenBy { (if (it == nickname) " You" else (peerNicknames[it] ? : it)).lowercase() } // Alphabetical
276283 )
277284
278285 sortedPeers.forEach { peerID ->
279- val fingerprint = viewModel.privateChatManager.getPeerFingerprint(peerID)
280- val isFavorite = favoritePeers.contains(fingerprint)
286+ val isFavorite = peerFavoriteStates[peerID] ? : false
281287
282288 PeerItem (
283289 peerID = peerID,
@@ -289,7 +295,7 @@ fun PeopleSection(
289295 colorScheme = colorScheme,
290296 onItemClick = { onPrivateChatStart(peerID) },
291297 onToggleFavorite = {
292- Log .d(" SidebarComponents" , " Sidebar toggle favorite: peerID=$peerID , fingerprint= $fingerprint , currentFavorite=$isFavorite " )
298+ Log .d(" SidebarComponents" , " Sidebar toggle favorite: peerID=$peerID , currentFavorite=$isFavorite " )
293299 viewModel.toggleFavorite(peerID)
294300 }
295301 )
0 commit comments