ConversationList
Display a list of conversations.
The ConversationList component represents a conversation list UI, which displays a list of conversations that the current user is a participant of.
fun ConversationList(appId, userId, selectedConversationId, token, baseTheme, themePath, themeCustom, customCssPath, onSelectConversation, modifier, key, controller)
Parameters
Your app's unique TalkJS ID. You can find it on the Settings page of the TalkJS dashboard.
The ID of the current user.
If the given user ID doesn't exist, a "Something went wrong" message will be displayed.
The ID of the conversation currently selected in the list.
Changing the value of this parameter will cause the ConversationList UI to update.
Also update your local selectedConversationId state in onSelectConversation so that it stays in sync with the UI's internals.
Note that updating the selected conversation through this parameter will not trigger the onSelectConversation callback.
A token to authenticate the session with.
See the Authentication guide and Token reference for details and examples.
Required when authentication is enabled, otherwise optional.
The base theme to start from.
Defaults to DEFAULT.
Path to a custom theme file, relative to the app's assets folder.
Arbitrary custom data passed down to the theme.
Whatever data you pass here will be made available for use in the theme module loaded via themePath.
Path to a custom CSS file, relative to the app's assets folder.
Callback that triggers when the user selects a conversation from the list.
event is the event object containing the selected conversation.
Note that updating the selected conversation through the selectedConversationId parameter will not trigger this callback.
Use this callback to have an adjacent chatbox show the correct conversation. See Respond to a select conversation event.
A reference to the ConversationListController.
Returns
fun rememberConversationListController(): ConversationListController
Returns
A collection of actions available on the ConversationList UI.
| constructor | |
| selectConversation | Selects a conversation with the given ID, or de-select a conversation by passing |
ConversationListController()
suspend fun conversationListController.selectConversation(conversationId, focusedMessageId)
Selects a conversation with the given ID, or de-select a conversation by passing null.
This method is called from the theme's ConversationListItem component when the user clicks on the given conversation. You can also call it programmatically from elsewhere to simulate a user click.
This method will trigger the onSelectConversation callback of ConversationList. In most cases, if you want to change the selected conversation programmatically from outside the conversation list, it's better to pass a different value to the selectedConversationId parameter of ConversationList instead, as that lets you keep your local state in sync with the parameters passed to the conversation list.
You can also optionally pass a focusedMessageId to signify the intent of scrolling to a particular message within that conversation.
Returns
The following examples show how to use the ConversationList component.
To create a conversation list and pass it an existing user, use the following:
ConversationList(
appId = "<APP_ID>",
userId = "sample_user_alice",
)The Kotlin Data API, which lets you read, subscribe to, and update your chat data, is included with Android Components.
Import getTalkSession into the file where you want to use it:
import com.talkjs.core.getTalkSessionThen create the session and user:
class ChatViewModel : ViewModel() {
val appId = "<APP_ID>"
val userId = "sebastian"
init {
val session = getTalkSession(appId = appId, userId = userId)
viewModelScope.launch {
session.currentUser.createIfNotExists(name = "Sebastian")
}
}
}Pass the new user to your conversation list:
val viewModel = viewModel<ChatViewModel>()
ConversationList(
appId = viewModel.appId,
userId = viewModel.userId,
)Respond to a select conversation event by switching the chatbox to show the selected conversation.
When a user selects a conversation from the list, the component calls the onSelectConversation callback with the selected conversation’s details. You can handle this event to update a chatbox or perform another action.
var conversationId by remember { mutableStateOf<String?>("sample_conversation") }
Column {
ConversationList(
modifier = Modifier.weight(1f),
appId = "<APP_ID>",
userId = "sample_user_alice",
selectedConversationId = conversationId,
onSelectConversation = { event ->
conversationId = event.conversation?.id
},
)
Chatbox(
modifier = Modifier.weight(1f),
appId = "<APP_ID>",
userId = "sample_user_alice",
conversationId = conversationId,
)
}