MessageComposerController

class MessageComposerController(channelCid: String, chatClient: ChatClient = ChatClient.instance(), val channelState: StateFlow<ChannelState?>, mediaRecorder: StreamMediaRecorder, userLookupHandler: UserLookupHandler, fileToUri: (File) -> String, maxAttachmentCount: Int = AttachmentConstants.MAX_ATTACHMENTS_COUNT, isLinkPreviewEnabled: Boolean = false)

Controller responsible for handling the composing and sending of messages.

It acts as a central place for both the core business logic and state required to create and send messages, handle attachments, message actions and more.

If you require more state and business logic, compose this Controller with your code and apply the necessary changes.

Parameters

channelCid

The CID of the channel we're chatting in.

chatClient

The client used to communicate to the API.

channelState

The current state of the channel.

mediaRecorder

The media recorder used to record audio messages.

userLookupHandler

The handler used to lookup users for mentions.

fileToUri

The function used to convert a file to a URI.

maxAttachmentCount

The maximum number of attachments that can be sent in a single message.

isLinkPreviewEnabled

If the link preview is enabled in the channel.

Constructors

Link copied to clipboard
constructor(channelCid: String, chatClient: ChatClient = ChatClient.instance(), channelState: StateFlow<ChannelState?>, mediaRecorder: StreamMediaRecorder, userLookupHandler: UserLookupHandler, fileToUri: (File) -> String, maxAttachmentCount: Int = AttachmentConstants.MAX_ATTACHMENTS_COUNT, isLinkPreviewEnabled: Boolean = false)

Properties

Link copied to clipboard
val alsoSendToChannel: MutableStateFlow<Boolean>

If the message will be shown in the channel after it is sent.

Link copied to clipboard
val channelState: StateFlow<ChannelState?>
Link copied to clipboard
val commandSuggestions: MutableStateFlow<List<Command>>

Represents the list of commands that can be executed for the channel.

Link copied to clipboard
val cooldownTimer: MutableStateFlow<Int>

Represents the remaining time until the user is allowed to send the next message.

Link copied to clipboard
val input: MutableStateFlow<String>

UI state of the current composer input.

Link copied to clipboard

Represents a Flow that holds the last active MessageAction that is either the Edit, Reply.

Link copied to clipboard
val linkPreviews: MutableStateFlow<List<LinkPreview>>

Represents the list of links that can be previewed.

Link copied to clipboard
val mentionSuggestions: MutableStateFlow<List<User>>

Represents the list of users that can be used to autocomplete the current mention input.

Link copied to clipboard
val messageActions: MutableStateFlow<Set<MessageAction>>

Set of currently active message actions. These are used to display different UI in the composer, as well as help us decorate the message with information, such as the quoted message id.

Link copied to clipboard
val messageInput: MutableStateFlow<MessageInput>

UI state of the current composer input.

Link copied to clipboard
val messageMode: MutableStateFlow<MessageMode>

Current message mode, either MessageMode.Normal or MessageMode.MessageThread. Used to determine if we're sending a thread reply or a regular message.

Link copied to clipboard
val ownCapabilities: StateFlow<Set<String>>

Holds information about the abilities the current user is able to exercise in the given channel.

Link copied to clipboard
val selectedAttachments: MutableStateFlow<List<Attachment>>

Represents the currently selected attachments, that are shown within the composer UI.

Link copied to clipboard
val state: MutableStateFlow<MessageComposerState>

Full message composer state holding all the required information.

Link copied to clipboard

Buffers typing updates.

Link copied to clipboard
val validationErrors: MutableStateFlow<List<ValidationError>>

Represents the list of validation errors for the current text input and the currently selected attachments.

Functions

Link copied to clipboard

Stores the selected attachments from the attachment picker. These will be shown in the UI, within the composer component. We upload and send these attachments once the user taps on the send button.

Link copied to clipboard
fun buildNewMessage(message: String, attachments: List<Attachment> = emptyList()): Message

Builds a new Message to send to our API. If isInEditMode is true, we use the current action's message and apply the given changes.

Link copied to clipboard

Cancels audio recording and moves MessageComposerState.recording state to RecordingState.Idle.

Link copied to clipboard
fun clearData()

Clears all the data from the input - both the current input value and the selectedAttachments.

Link copied to clipboard

Completes audio recording and moves MessageComposerState.recording state to RecordingState.Complete. Also, it wil update MessageComposerState.attachments list.

Link copied to clipboard
fun createPoll(pollConfig: PollConfig, onResult: (Result<Message>) -> Unit = {})

Creates a poll with the given pollConfig.

Link copied to clipboard

Dismisses all message actions from the UI and clears the input if isInEditMode is true.

Link copied to clipboard

Dismisses the suggestions popup above the message composer.

Link copied to clipboard
fun holdRecording(offset: Pair<Float, Float>? = null)

Sends coordinates of the touch event to the AudioRecordingController.

Link copied to clipboard

Updates the UI state when leaving the thread, to switch back to the MessageMode.Normal, by calling setMessageMode.

Link copied to clipboard
fun onCleared()

Cancels any pending work when the parent ViewModel is about to be destroyed.

Link copied to clipboard

Pauses audio recording and sets RecordingState.Overview.isPlaying to false.

Link copied to clipboard

Handles selected messageAction. We only have three actions we can react to in the composer:

Link copied to clipboard

Removes a selected attachment from the list, when the user taps on the cancel/delete button.

Link copied to clipboard
fun seekRecordingTo(progress: Float)

Pauses audio recording and seeks to the given progress. Sets RecordingState.Overview.isPlaying to false. Sets RecordingState.Overview.playingProgress to the given progress.

Link copied to clipboard
fun selectCommand(command: Command)

Switches the message composer to the command input mode.

Link copied to clipboard
fun selectMention(user: User)

Autocompletes the current text input with the mention from the selected user.

Link copied to clipboard
fun sendMessage(message: Message, callback: Call.Callback<Message>)

Sends a given message using our Stream API. Based on isInEditMode, we either edit an existing message, or we send a new message, using ChatClient. In case the message is a moderated message the old one is deleted before the replacing one is sent.

Link copied to clipboard
fun setAlsoSendToChannel(alsoSendToChannel: Boolean)

Called when the "Also send as a direct message" checkbox is checked or unchecked.

Link copied to clipboard

Called when the input changes and the internal state needs to be updated.

Link copied to clipboard
fun setMessageMode(messageMode: MessageMode)

Called when the message mode changes and the internal state needs to be updated.

Link copied to clipboard
fun startRecording(offset: Pair<Float, Float>? = null)

Starts audio recording and moves MessageComposerState.recording state from RecordingState.Idle to RecordingState.Hold.

Link copied to clipboard

Stops audio recording and moves MessageComposerState.recording state to RecordingState.Overview.

Link copied to clipboard

Toggles the visibility of the command suggestion list popup.

Link copied to clipboard

Toggles audio recording playback if MessageComposerState.recording is instance of RecordingState.Overview.