Class MessageComposerController
-
- All Implemented Interfaces:
public final class MessageComposerControllerController 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.
-
-
Field Summary
Fields Modifier and Type Field Description private TypingUpdatesBuffertypingUpdatesBufferprivate final Flow<ChannelState>channelStateprivate final StateFlow<Set<String>>ownCapabilitiesprivate final MutableStateFlow<MessageComposerState>stateprivate final MutableStateFlow<MessageInput>messageInputprivate final MutableStateFlow<String>inputprivate final MutableStateFlow<Boolean>alsoSendToChannelprivate final MutableStateFlow<Integer>cooldownTimerprivate final MutableStateFlow<List<Attachment>>selectedAttachmentsprivate final MutableStateFlow<List<ValidationError>>validationErrorsprivate final MutableStateFlow<List<User>>mentionSuggestionsprivate final MutableStateFlow<List<Command>>commandSuggestionsprivate final MutableStateFlow<List<LinkPreview>>linkPreviewsprivate final MutableStateFlow<MessageMode>messageModeprivate final MutableStateFlow<Set<MessageAction>>messageActionsprivate final Flow<MessageAction>lastActiveAction
-
Constructor Summary
Constructors Constructor Description MessageComposerController(String channelCid, ChatClient chatClient, StreamMediaRecorder mediaRecorder, UserLookupHandler userLookupHandler, Function1<File, String> fileToUri, Integer messageLimit, Integer maxAttachmentCount, String messageId, Boolean isLinkPreviewEnabled)
-
Method Summary
Modifier and Type Method Description final TypingUpdatesBuffergetTypingUpdatesBuffer()Buffers typing updates. final UnitsetTypingUpdatesBuffer(TypingUpdatesBuffer typingUpdatesBuffer)Buffers typing updates. final Flow<ChannelState>getChannelState()Holds information about the current state of the Channel. final StateFlow<Set<String>>getOwnCapabilities()Holds information about the abilities the current user is able to exercise in the given channel. final MutableStateFlow<MessageComposerState>getState()Full message composer state holding all the required information. final MutableStateFlow<MessageInput>getMessageInput()UI state of the current composer input. final MutableStateFlow<String>getInput()UI state of the current composer input. final MutableStateFlow<Boolean>getAlsoSendToChannel()If the message will be shown in the channel after it is sent. final MutableStateFlow<Integer>getCooldownTimer()Represents the remaining time until the user is allowed to send the next message. final MutableStateFlow<List<Attachment>>getSelectedAttachments()Represents the currently selected attachments, that are shown within the composer UI. final MutableStateFlow<List<ValidationError>>getValidationErrors()Represents the list of validation errors for the current text input and the currently selected attachments. final MutableStateFlow<List<User>>getMentionSuggestions()Represents the list of users that can be used to autocomplete the current mention input. final MutableStateFlow<List<Command>>getCommandSuggestions()Represents the list of commands that can be executed for the channel. final MutableStateFlow<List<LinkPreview>>getLinkPreviews()Represents the list of links that can be previewed. final MutableStateFlow<MessageMode>getMessageMode()Current message mode, either MessageMode.Normal or MessageMode.MessageThread. final MutableStateFlow<Set<MessageAction>>getMessageActions()Set of currently active message actions. final Flow<MessageAction>getLastActiveAction()final UnitsetMessageInput(String value)Called when the input changes and the internal state needs to be updated. final UnitsetMessageMode(MessageMode messageMode)Called when the message mode 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 UnitperformMessageAction(MessageAction messageAction)Handles selected messageAction. final UnitdismissMessageActions()Dismisses all message actions from the UI and clears the input if isInEditMode is true. final UnitaddSelectedAttachments(List<Attachment> attachments)Stores the selected attachments from the attachment picker. final UnitremoveSelectedAttachment(Attachment attachment)Removes a selected attachment from the list, when the user taps on the cancel/delete button. final UnitclearData()Clears all the data from the input - both the current input value and the selectedAttachments. 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, by calling setMessageMode. final UnitonCleared()Cancels any pending work when the parent ViewModel is about to be destroyed. final UnitselectMention(User user)Autocompletes the current text input with the mention from the selected user. final UnitselectCommand(Command command)Switches the message composer to the command input mode. final UnittoggleCommandsVisibility()Toggles the visibility of the command suggestion list popup. final UnitdismissSuggestionsPopup()Dismisses the suggestions popup above the message composer. final UnitstartRecording()Starts audio recording and moves MessageComposerState.recording state from RecordingState.Idle to RecordingState.Hold. final UnitlockRecording()Moves MessageComposerState.recording state to RecordingState.Locked. final UnitcancelRecording()Cancels audio recording and moves MessageComposerState.recording state to RecordingState.Idle. final UnittoggleRecordingPlayback()Toggles audio recording playback if MessageComposerState.recording is instance of RecordingState.Overview. final UnitstopRecording()Stops audio recording and moves MessageComposerState.recording state to RecordingState.Overview. final UnitcompleteRecording()Completes audio recording and moves MessageComposerState.recording state to RecordingState.Complete. final UnitpauseRecording()Pauses audio recording and sets RecordingState.Overview.isPlaying to false. final UnitseekRecordingTo(Float progress)Pauses audio recording and seeks to the given progress. -
-
Constructor Detail
-
MessageComposerController
MessageComposerController(String channelCid, ChatClient chatClient, StreamMediaRecorder mediaRecorder, UserLookupHandler userLookupHandler, Function1<File, String> fileToUri, Integer messageLimit, Integer maxAttachmentCount, String messageId, Boolean isLinkPreviewEnabled)
- Parameters:
channelCid- The CID of the channel we're chatting in.chatClient- The client used to communicate to the API.maxAttachmentCount- The maximum number of attachments that can be sent in a single message.messageId- The id of a message we wish to scroll to in messages list.
-
-
Method Detail
-
getTypingUpdatesBuffer
final TypingUpdatesBuffer getTypingUpdatesBuffer()
Buffers typing updates.
-
setTypingUpdatesBuffer
final Unit setTypingUpdatesBuffer(TypingUpdatesBuffer typingUpdatesBuffer)
Buffers typing updates.
-
getChannelState
final Flow<ChannelState> getChannelState()
Holds information about the current state of the Channel.
-
getOwnCapabilities
final StateFlow<Set<String>> getOwnCapabilities()
Holds information about the abilities the current user is able to exercise in the given channel.
e.g. send messages, delete messages, etc... For a full list @see ChannelCapabilities.
-
getState
final MutableStateFlow<MessageComposerState> getState()
Full message composer state holding all the required information.
-
getMessageInput
final MutableStateFlow<MessageInput> getMessageInput()
UI state of the current composer input.
-
getAlsoSendToChannel
final MutableStateFlow<Boolean> getAlsoSendToChannel()
If the message will be shown in the channel after it is sent.
-
getCooldownTimer
final MutableStateFlow<Integer> getCooldownTimer()
Represents the remaining time until the user is allowed to send the next message.
-
getSelectedAttachments
final MutableStateFlow<List<Attachment>> getSelectedAttachments()
Represents the currently selected attachments, that are shown within the composer UI.
-
getValidationErrors
final MutableStateFlow<List<ValidationError>> getValidationErrors()
Represents the list of validation errors for the current text input and the currently selected attachments.
-
getMentionSuggestions
final MutableStateFlow<List<User>> getMentionSuggestions()
Represents the list of users that can be used to autocomplete the current mention input.
-
getCommandSuggestions
final MutableStateFlow<List<Command>> getCommandSuggestions()
Represents the list of commands that can be executed for the channel.
-
getLinkPreviews
final MutableStateFlow<List<LinkPreview>> getLinkPreviews()
Represents the list of links that can be previewed.
-
getMessageMode
final MutableStateFlow<MessageMode> getMessageMode()
Current message mode, either MessageMode.Normal or MessageMode.MessageThread. Used to determine if we're sending a thread reply or a regular message.
-
getMessageActions
final MutableStateFlow<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.
-
getLastActiveAction
final Flow<MessageAction> getLastActiveAction()
-
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 messageMode 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 input 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.
-
addSelectedAttachments
final Unit addSelectedAttachments(List<Attachment> attachments)
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.
- Parameters:
attachments- The attachments to store and show in the composer.
-
removeSelectedAttachment
final Unit removeSelectedAttachment(Attachment attachment)
Removes a selected attachment from the list, when the user taps on the cancel/delete button.
This will update the UI to remove it from the composer component.
- Parameters:
attachment- The attachment to remove.
-
clearData
final Unit clearData()
Clears all the data from the input - both the current input value and the selectedAttachments.
-
sendMessage
final Unit sendMessage(Message message, Call.Callback<Message> callback)
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.- Returns:
Message object, with all the data required to send it to the API.
-
leaveThread
final Unit leaveThread()
Updates the UI state when leaving the thread, to switch back to the MessageMode.Normal, by calling setMessageMode.
It also dismisses any currently active message actions, such as Edit and Reply, as the user left the relevant thread.
-
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.
-
selectCommand
final Unit selectCommand(Command command)
Switches the message composer to the command input mode.
- Parameters:
command- The command that was selected.
-
toggleCommandsVisibility
final Unit toggleCommandsVisibility()
Toggles the visibility of the command suggestion list popup.
-
dismissSuggestionsPopup
final Unit dismissSuggestionsPopup()
Dismisses the suggestions popup above the message composer.
-
startRecording
final Unit startRecording()
Starts audio recording and moves MessageComposerState.recording state from RecordingState.Idle to RecordingState.Hold.
-
lockRecording
final Unit lockRecording()
Moves MessageComposerState.recording state to RecordingState.Locked.
-
cancelRecording
final Unit cancelRecording()
Cancels audio recording and moves MessageComposerState.recording state to RecordingState.Idle.
-
toggleRecordingPlayback
final Unit toggleRecordingPlayback()
Toggles audio recording playback if MessageComposerState.recording is instance of RecordingState.Overview.
-
stopRecording
final Unit stopRecording()
Stops audio recording and moves MessageComposerState.recording state to RecordingState.Overview.
-
completeRecording
final Unit completeRecording()
Completes audio recording and moves MessageComposerState.recording state to RecordingState.Complete. Also, it wil update MessageComposerState.attachments list.
-
pauseRecording
final Unit pauseRecording()
Pauses audio recording and sets RecordingState.Overview.isPlaying to false.
-
seekRecordingTo
final Unit seekRecordingTo(Float progress)
Pauses audio recording and seeks to the given progress. Sets RecordingState.Overview.isPlaying to false. Sets RecordingState.Overview.playingProgress to the given progress.
-
-
-
-