Each component’s ViewModel lives in chatuikit-core and manages data fetching, state transitions, real-time listeners, and list operations via StateFlow. The same ViewModel is shared by both Kotlin XML Views and Jetpack Compose modules.
Creating and Providing a ViewModel
By default, each component creates its own ViewModel internally. To customize behavior, create the ViewModel externally using the factory and pass it to the component.
Kotlin (XML Views)
Jetpack Compose
This pattern applies to all components — CometChatUsers, CometChatGroups, CometChatMessageList, CometChatCallLogs, etc. Each has a corresponding factory class.
State Observation
ViewModels expose state via Kotlin StateFlow (not LiveData). The key state flows are:
| StateFlow | Type | Description |
|---|
uiState | StateFlow<UIState> | Current screen state: Loading, Empty, Error(exception), Content(list) |
conversations | StateFlow<List<Conversation>> | The current list of conversations |
typingIndicators | StateFlow<Map<String, TypingIndicator>> | Active typing indicators by conversation ID |
deleteState | StateFlow<DeleteState> | Delete operation state: Idle, InProgress, Success, Failure(exception) |
playSoundEvent | SharedFlow<Boolean> | Emits when a message sound should play |
scrollToTopEvent | SharedFlow<Unit> | Emits when the list should scroll to top |
UIState
Observing State
Kotlin (XML Views)
Jetpack Compose
Configuring Data Fetching
Pass a custom ConversationsRequestBuilder to control what data the component fetches. You can set it during ViewModel creation or directly on the component:
Kotlin (XML Views)
Jetpack Compose
Pass the builder object, not the result of .build(). The component calls .build() internally.
ListOperations API
All list-based ViewModels implement ListOperations<T>, giving you a consistent API to manipulate list data:
Batch Operations
Perform multiple operations in a single emission — critical for performance with rapid updates:
ViewModel Methods
| Method | Description |
|---|
fetchConversations() | Fetch the next page (pagination) |
refreshList() | Clear and re-fetch from the server (silent refresh) |
deleteConversation(conversation) | Delete a conversation via the SDK |
resetDeleteState() | Reset delete state to Idle |
setConversationsRequestBuilder(builder) | Set a custom request builder for filtering |
setDisableReceipt(disable) | Disable read receipts |
setDisableSoundForMessages(disable) | Disable message sounds |
setCustomSoundForMessage(rawRes) | Set a custom sound resource |
Providing a Custom ViewModel
Subclass the ViewModel to override behavior, then inject it:
Create it via the factory with a custom repository if needed:
Kotlin (XML Views)
Jetpack Compose
Lifecycle Callbacks
Intercept data loading events on the View:
Kotlin (XML Views)
Jetpack Compose