Package 

Class MessageComposerController


  • 
    public final class MessageComposerController
    
                        

    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.

    • Constructor Detail

      • MessageComposerController

        MessageComposerController(String channelCid, ChatClient chatClient, StateFlow<ChannelState> channelState, StreamMediaRecorder mediaRecorder, UserLookupHandler userLookupHandler, Function1<File, String> fileToUri, MessageComposerController.Config config, Flow<GlobalState> globalState, SavedStateHandle savedStateHandle, String initialParentMessageId)
        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.
        config - The configuration for the message composer.
        globalState - A flow emitting the current GlobalState.
        savedStateHandle - Handle used to persist and restore picker selections and edit-mode state across process death (e.g.
        initialParentMessageId - The ID of the parent Message to open the composer directly in a thread, starting it in thread mode.
    • Method Detail

      • getInputFocusEvents

         final SharedFlow<Unit> getInputFocusEvents()

        Emits each time the message input field should request focus (e.g. after a command is selected).

      • getEvents

         final SharedFlow<MessageComposerViewEvent> getEvents()

        One-shot events emitted by the composer, such as feedback for unavailable commands. UI layers collect this flow and react with transient UI (e.g. a snackbar).

      • getMessageActions

         final StateFlow<Set<MessageAction>> getMessageActions()

        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.

      • setMessageInput

         final Unit setMessageInput(String value)

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

        Parameters:
        value - Current state value.
      • setMessageMode

         final Unit setMessageMode(MessageMode messageMode)

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

        This affects the business logic.

        Parameters:
        messageMode - The current message mode.
      • setAlsoSendToChannel

         final Unit setAlsoSendToChannel(Boolean alsoSendToChannel)

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

        Parameters:
        alsoSendToChannel - If the message will be shown in the channel after it is sent.
      • performMessageAction

         final Unit performMessageAction(MessageAction messageAction)

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

        • ThreadReply - We change the message mode so we can send the message to a thread.

        • Reply - We need to reply to a message and set up the reply UI.

        • Edit - We need to change the messageInput to the message we want to edit and change the UI to match the editing action.

        Parameters:
        messageAction - The newly selected action.
      • dismissMessageActions

         final Unit dismissMessageActions()

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

      • addAttachments

         final Unit addAttachments(List<Attachment> attachments)

        Adds attachments to the staged list, preserving insertion order.

        Each attachment is keyed by its EXTRA_SOURCE_URI if present, or by a deterministic fallback derived from Attachment.name and Attachment.mimeType. If a key is already present, its value is updated in place without changing its position.

        Parameters:
        attachments - The attachments to stage.
      • removeAttachment

         final Unit removeAttachment(Attachment attachment)

        Removes attachment from the staged list.

        Searches picker selections first (by key), then edit-mode attachments, then the recording attachment. Removes from the first list that contains it.

        Parameters:
        attachment - The attachment to remove.
      • removeAttachmentsByUris

         final Unit removeAttachmentsByUris(Set<String> uris)

        Removes all staged attachments whose URI string key is contained in uris.

        Parameters:
        uris - The URI string keys to remove.
      • clearAttachments

         final Unit clearAttachments()

        Removes all staged attachments and updates the composer state. Clears picker selections, edit-mode base attachments, and any completed recording attachment.

      • createPoll

         final Unit createPoll(CreatePollParams createPollParams, Function1<Result<Message>, Unit> onResult)

        Creates a poll with the given createPollParams.

        Parameters:
        createPollParams - Configuration for creating a poll.
      • clearData

         final Unit clearData()

        Clears all the data from the input — text, attachments, validation errors, actions, and active command. In thread mode, MessageComposerState.alsoSendToChannel is preserved until the user toggles it; otherwise it is reset.

      • sendMessage

         final Unit sendMessage(Message message, Call.Callback<Message> callback, SuspendFunction1<List<Attachment>, List<Attachment>> resolveAttachments)

        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.

        It also dismisses any current message actions.

        Parameters:
        message - The message to send.
      • buildNewMessage

         final Message buildNewMessage(String message, List<Attachment> attachments)

        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.

        If we're not editing a message, we fill in the required data for the message.

        Parameters:
        message - Message text.
        attachments - Message attachments.
      • onCleared

         final Unit onCleared()

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

      • selectMention

         final Unit selectMention(User user)

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

        Parameters:
        user - The user that is used to autocomplete the mention.
      • selectMention

         final Unit selectMention(Mention mention)

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

        Built-in Mention subclasses are submitted to the server on send via the corresponding fields on io.getstream.chat.android.models.Message.

        Parameters:
        mention - The mention that is used for the autocomplete.
      • selectCommand

         final Unit selectCommand(Command command)

        Switches the message composer to the command input mode.

        Sets MessageComposerState.activeCommand and clears the text input so the user can type the command arguments. The full /command args string is assembled in buildNewMessage.

        When Config.activeCommandEnabled is true, any pre-command input value, picker-selected attachments, and mention selections are stashed so clearActiveCommand can restore them if the user dismisses the command. Re-selecting a command while one is already active does not overwrite an existing stash (in-command input is command-specific and not preserved across command switches).

        When Config.activeCommandEnabled is true and the command is not available for the current composer action (edit mode or a moderation command during reply), emits a MessageComposerViewEvent.CommandUnavailable on events and returns without changing the active command. Legacy mode skips this check and sets the command unconditionally.

        Parameters:
        command - The command that was selected.
      • completeRecording

         final Unit completeRecording(Function1<Result<Attachment>, Unit> onComplete)

        Completes audio recording and updates the MessageComposerState.attachments list.

        Parameters:
        onComplete - Optional callback invoked with the result of the recording once the recording has been finalized.
      • sendRecording

         final Unit sendRecording()

        Completes the active audio recording and sends the recorded audio as an attachment.

      • cancelLinkPreview

         final Unit cancelLinkPreview()

        Dismisses the current link preview and marks enrichment as skipped. When a message is sent after dismissal, the backend will not enrich its URLs unless the detected URL in the input changes (e.g. the user replaces the link).