-
public final class MessageComposerViewModel extends ViewModel
ViewModel responsible for handling the composing and sending of messages.
Delegates all state management and business logic to MessageComposerController, including persistence of picker selections and edit-mode state across process death.
-
-
Field Summary
Fields Modifier and Type Field Description private final SharedFlow<Unit>inputFocusEventsprivate final SharedFlow<MessageComposerViewEvent>eventsprivate final StateFlow<MessageComposerState>messageComposerStateprivate final StateFlow<MessageInput>messageInput
-
Constructor Summary
Constructors Constructor Description MessageComposerViewModel(MessageComposerController messageComposerController, AttachmentStorageHelper storageHelper)
-
Method Summary
Modifier and Type Method Description final SharedFlow<Unit>getInputFocusEvents()Emits each time the message input field should request focus (e.g. final SharedFlow<MessageComposerViewEvent>getEvents()One-shot events emitted by the composer, such as feedback for unavailable commands. final StateFlow<MessageComposerState>getMessageComposerState()The full UI state that has all the required data. final StateFlow<MessageInput>getMessageInput()UI state of the current composer input. final UnitsetMessageInput(String value)Called when the input changes and the internal state needs to be updated. final UnitsetAlsoSendToChannel(Boolean alsoSendToChannel)Called when the "Also send as a direct message" checkbox is checked or unchecked. final UnitsetMessageMode(MessageMode messageMode)Called when the message mode changes and the internal state needs to be updated. final UnitperformMessageAction(MessageAction messageAction)Handles the selected messageAction. final UnitdismissMessageActions()Dismisses all message actions from the UI and clears the input based on the internal state. final UnitaddAttachments(List<Attachment> attachments)Adds attachments to the staged attachment list. final UnitremoveAttachment(Attachment attachment)Removes attachment from the staged attachment list. final UnitclearAttachments()Removes all staged attachments. final UnitcreatePoll(CreatePollParams createPollParams)Creates a poll with the given createPollParams. final UnitsendMessage(Message message, Call.Callback<Message> callback)Sends a given message using our Stream API. final MessagebuildNewMessage(String message, List<Attachment> attachments)Builds a new Message to send to our API. final UnitleaveThread()Updates the UI state when leaving the thread, to switch back to the MessageMode.Normal message mode, by calling setMessageMode. final UnitselectMention(User user)Autocompletes the current text input with the mention from the selected user. final UnitselectMention(Mention mention)Autocompletes the current text input with the mention from the selected mention. final UnitselectCommand(Command command)Switches the message composer to the command input mode. final UnitclearActiveCommand()final UnittoggleCommandsVisibility()Toggles the visibility of the command suggestion list popup. final UnitsetTypingUpdatesBuffer(TypingUpdatesBuffer buffer)Sets the typing updates buffer. final UnitclearData()Clears the input and the current state of the composer. final UnitstartRecording()final UnitholdRecording(Pair<Float, Float> offset)final UnitlockRecording()final UnitcancelRecording()final UnitstopRecording()final UnittoggleRecordingPlayback()final UnitcompleteRecording(Function1<Result<Attachment>, Unit> onComplete)final UnitpauseRecording()final UnitseekRecordingTo(Float progress)final UnitsendRecording()final UnitcancelLinkPreview()-
-
Constructor Detail
-
MessageComposerViewModel
MessageComposerViewModel(MessageComposerController messageComposerController, AttachmentStorageHelper storageHelper)
- Parameters:
messageComposerController- The controller used to relay all the actions and fetch all the state.storageHelper- Resolves deferred attachment files before sending.
-
-
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.
-
getMessageComposerState
final StateFlow<MessageComposerState> getMessageComposerState()
The full UI state that has all the required data.
-
getMessageInput
final StateFlow<MessageInput> getMessageInput()
UI state of the current composer input.
-
setMessageInput
final Unit setMessageInput(String value)
Called when the input changes and the internal state needs to be updated.
- Parameters:
value- Current state value.
-
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.
-
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.
-
performMessageAction
final Unit performMessageAction(MessageAction messageAction)
Handles the selected messageAction.
- Parameters:
messageAction- The newly selected action.
-
dismissMessageActions
final Unit dismissMessageActions()
Dismisses all message actions from the UI and clears the input based on the internal state.
-
addAttachments
final Unit addAttachments(List<Attachment> attachments)
Adds attachments to the staged attachment list.
Attachments are keyed by URI string, preserving insertion order.
- Parameters:
attachments- The attachments to add.
-
removeAttachment
final Unit removeAttachment(Attachment attachment)
Removes attachment from the staged attachment list.
- Parameters:
attachment- The attachment to remove.
-
clearAttachments
final Unit clearAttachments()
Removes all staged attachments.
Call this when the attachments are consumed — for example, after a message is sent, a poll is created, or a command is selected.
-
createPoll
final Unit createPoll(CreatePollParams createPollParams)
Creates a poll with the given createPollParams.
- Parameters:
createPollParams- Configuration for creating a poll.
-
sendMessage
final Unit sendMessage(Message message, Call.Callback<Message> callback)
Sends a given message using our Stream API. Based on the internal state, we either edit an existing message, or we send a new message, using our API.
Deferred attachments (those without a local file) are resolved on a background thread before the message is handed off to the controller.
It also dismisses any current message actions.
- Parameters:
message- The message to send.callback- Invoked when the API call completes.
-
buildNewMessage
final Message buildNewMessage(String message, List<Attachment> attachments)
Builds a new Message to send to our API. Based on the internal state, we use the current action's message and apply the given changes.
If we're not editing a message, we'll fill in the required data for the message.
- Parameters:
message- Message text.attachments- Message attachments.
-
leaveThread
final Unit leaveThread()
Updates the UI state when leaving the thread, to switch back to the MessageMode.Normal message mode, by calling setMessageMode.
It also dismisses any currently active message actions, such as Edit and Reply, as the user left the relevant thread.
-
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.
IMPORTANT: The SDK supports only user mentions (see Mention.User). Custom mentions are purely visual, and will not be submitted to the server.
- 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.
- Parameters:
command- The command that was selected in the command suggestion list popup.
-
clearActiveCommand
final Unit clearActiveCommand()
-
toggleCommandsVisibility
final Unit toggleCommandsVisibility()
Toggles the visibility of the command suggestion list popup.
-
setTypingUpdatesBuffer
final Unit setTypingUpdatesBuffer(TypingUpdatesBuffer buffer)
Sets the typing updates buffer.
- Parameters:
buffer- The buffer to use for typing updates.
-
startRecording
final Unit startRecording()
-
holdRecording
final Unit holdRecording(Pair<Float, Float> offset)
-
lockRecording
final Unit lockRecording()
-
cancelRecording
final Unit cancelRecording()
-
stopRecording
final Unit stopRecording()
-
toggleRecordingPlayback
final Unit toggleRecordingPlayback()
-
completeRecording
final Unit completeRecording(Function1<Result<Attachment>, Unit> onComplete)
-
pauseRecording
final Unit pauseRecording()
-
seekRecordingTo
final Unit seekRecordingTo(Float progress)
-
sendRecording
final Unit sendRecording()
-
cancelLinkPreview
final Unit cancelLinkPreview()
-
-
-
-