diff --git a/CHANGELOG.md b/CHANGELOG.md index eaaec71b84e..9df4c2cf1b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ - Fixed `ChannelState.watchers` not being updated when a watcher gets deleted. [#5378](https://github.com/GetStream/stream-chat-android/pull/5378) ### ⬆️ Improved +- Keep members updated on QueryChannelsState. [5382](https://github.com/GetStream/stream-chat-android/pull/5382) ### ✅ Added diff --git a/stream-chat-android-state/src/main/java/io/getstream/chat/android/state/plugin/logic/querychannels/internal/QueryChannelsStateLogic.kt b/stream-chat-android-state/src/main/java/io/getstream/chat/android/state/plugin/logic/querychannels/internal/QueryChannelsStateLogic.kt index f0064134cda..824c1e9c747 100644 --- a/stream-chat-android-state/src/main/java/io/getstream/chat/android/state/plugin/logic/querychannels/internal/QueryChannelsStateLogic.kt +++ b/stream-chat-android-state/src/main/java/io/getstream/chat/android/state/plugin/logic/querychannels/internal/QueryChannelsStateLogic.kt @@ -34,6 +34,7 @@ import io.getstream.log.taggedLogger import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.async +@Suppress("TooManyFunctions") internal class QueryChannelsStateLogic( private val mutableState: QueryChannelsMutableState, private val stateRegistry: StateRegistry, @@ -145,7 +146,10 @@ internal class QueryChannelsStateLogic( val existingChannels = mutableState.rawChannels ?: emptyMap() mutableState.setChannels( existingChannels + - channels.map { it.cid to it.joinMessages(existingChannels[it.cid]) }, + channels.map { + it.cid to it.joinMessages(existingChannels[it.cid]) + .joinMembers(existingChannels[it.cid]) + }, ) channels.map { channel -> coroutineScope.async { @@ -166,6 +170,24 @@ internal class QueryChannelsStateLogic( .distinctBy { it.id }, ) + /** + * The list of members is merged with the existing list of members but only used if it is smaller than the + * number of members in the channel. + * + * @param existingChannel Channel? The existing channel. + * + * @return Channel The channel with the members list updated. + */ + private fun Channel.joinMembers(existingChannel: Channel?): Channel = + existingChannel?.let { oldChannel -> + val members = (members.associateBy { it.getUserId() } + (oldChannel.members.associateBy { it.getUserId() })) + .takeUnless { it.size > memberCount } + ?.values + ?.toList() + ?: members + copy(members = members) + } ?: this + /** * Remove channels to state. */