Skip to main content
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.
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:
StateFlowTypeDescription
uiStateStateFlow<UIState>Current screen state: Loading, Empty, Error(exception), Content(list)
conversationsStateFlow<List<Conversation>>The current list of conversations
typingIndicatorsStateFlow<Map<String, TypingIndicator>>Active typing indicators by conversation ID
deleteStateStateFlow<DeleteState>Delete operation state: Idle, InProgress, Success, Failure(exception)
playSoundEventSharedFlow<Boolean>Emits when a message sound should play
scrollToTopEventSharedFlow<Unit>Emits when the list should scroll to top

UIState

Observing State


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:
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

MethodDescription
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:

Lifecycle Callbacks

Intercept data loading events on the View: