-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PBE-6197] add member list component to SampleApp (#5434)
* [PBE-6197] add member list component to SampleApp * fix spotless
- Loading branch information
Showing
4 changed files
with
188 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...sample/src/main/java/io/getstream/chat/android/compose/sample/ui/component/MembersList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) 2014-2024 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.getstream.chat.android.compose.sample.ui.component | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.lazy.LazyRow | ||
import androidx.compose.foundation.lazy.itemsIndexed | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.unit.dp | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import io.getstream.chat.android.compose.sample.vm.MembersViewModel | ||
|
||
@Composable | ||
internal fun MembersList( | ||
viewModel: MembersViewModel, | ||
) { | ||
val memberNames by viewModel.memberNames.collectAsStateWithLifecycle() | ||
LazyRow( | ||
modifier = Modifier.height(32.dp) | ||
.fillMaxWidth() | ||
.background(color = Color.LightGray), | ||
verticalAlignment = Alignment.CenterVertically, | ||
) { | ||
itemsIndexed(memberNames) { index, (name, isCurrentUser) -> | ||
if (index == 0) { | ||
Spacer(modifier = Modifier.width(16.dp)) | ||
} | ||
val displayName = when (index == memberNames.lastIndex) { | ||
true -> name | ||
else -> "$name, " | ||
} | ||
Text( | ||
modifier = Modifier, | ||
fontWeight = if (isCurrentUser) FontWeight.Bold else FontWeight.Normal, | ||
text = displayName, | ||
) | ||
if (index == memberNames.lastIndex) { | ||
Spacer(modifier = Modifier.width(16.dp)) | ||
} else { | ||
Spacer(modifier = Modifier.width(4.dp)) | ||
} | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...pose-sample/src/main/java/io/getstream/chat/android/compose/sample/vm/MembersViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (c) 2014-2024 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.getstream.chat.android.compose.sample.vm | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewModelScope | ||
import io.getstream.chat.android.client.ChatClient | ||
import io.getstream.chat.android.models.Channel | ||
import io.getstream.chat.android.state.extensions.watchChannelAsState | ||
import io.getstream.log.taggedLogger | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.combine | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.filterNotNull | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
|
||
internal class MembersViewModel( | ||
private val cid: String, | ||
private val chatClient: ChatClient, | ||
) : ViewModel() { | ||
|
||
private val logger by taggedLogger("Chat:MembersVM") | ||
|
||
private val _memberNames: MutableStateFlow<List<MemberName>> = MutableStateFlow(emptyList()) | ||
val memberNames: StateFlow<List<MemberName>> = _memberNames | ||
|
||
init { | ||
watchChannel() | ||
} | ||
|
||
private fun watchChannel() { | ||
chatClient.watchChannelAsState(cid = cid, messageLimit = 0, coroutineScope = viewModelScope) | ||
.filterNotNull() | ||
.flatMapLatest { state -> | ||
combine( | ||
state.channelData, | ||
state.membersCount, | ||
state.watcherCount, | ||
) { _, _, _ -> | ||
state.toChannel() | ||
} | ||
} | ||
.distinctUntilChanged() | ||
.onEach(this::updateMembers) | ||
.launchIn(viewModelScope) | ||
} | ||
|
||
private fun updateMembers(channel: Channel) { | ||
val currentUserId = chatClient.getCurrentUser()?.id | ||
val names = channel.members.map { | ||
val isCurrentUser = currentUserId == it.user.id | ||
MemberName( | ||
name = if (isCurrentUser) "You" else it.user.name, | ||
isCurrentUser = isCurrentUser, | ||
) | ||
} | ||
logger.d { "[updateMembers] names: ${names.map { it.name }}" } | ||
_memberNames.value = names | ||
} | ||
} | ||
|
||
internal data class MemberName(val name: String, val isCurrentUser: Boolean) | ||
|
||
internal class MembersViewModelFactory( | ||
private val cid: String, | ||
private val chatClient: ChatClient = ChatClient.instance(), | ||
) : ViewModelProvider.Factory { | ||
|
||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return MembersViewModel(cid, chatClient) as T | ||
} | ||
} |