-
public final class MessageListViewModel extends ViewModel
ViewModel responsible for handling all the business logic & state for the list of messages.
-
-
Field Summary
Fields Modifier and Type Field Description private final State<MessageListState>currentMessagesStateprivate final MessageModemessageModeprivate final PollStatepollStateprivate final Channelchannelprivate final StateFlow<ChannelState>channelStateprivate final List<User>typingUsersprivate final Set<MessageAction>messageActionsprivate final BooleanisStartedForThreadprivate final BooleanisInThreadprivate final BooleanisShowingOverlayprivate final BooleanisShowingPollOptionDetailsprivate final StateFlow<ConnectionState>connectionStateprivate final Flow<Boolean>isOnlineprivate final StateFlow<User>userprivate final BooleanshowSystemMessagesStateprivate final MessageFooterVisibilitymessageFooterVisibilityStateprivate final DeletedMessageVisibilitydeletedMessageVisibilityState
-
Constructor Summary
Constructors Constructor Description MessageListViewModel(MessageListController messageListController)
-
Method Summary
Modifier and Type Method Description final State<MessageListState>getCurrentMessagesState()State handler for the UI, which holds all the information the UI needs to render messages. final MessageModegetMessageMode()Holds the current MessageMode that's used for the messages list. final PollStategetPollState()Holds the current PollState that's used for the messages list. final ChannelgetChannel()The information for the current Channel. final StateFlow<ChannelState>getChannelState()Holds information about the current channel and is actively updated. final List<User>getTypingUsers()The list of typing users. final Set<MessageAction>getMessageActions()Set of currently active MessageActions. final BooleangetIsStartedForThread()final BooleangetIsInThread()final BooleangetIsShowingOverlay()final BooleangetIsShowingPollOptionDetails()final StateFlow<ConnectionState>getConnectionState()Gives us information about the online state of the device. final Flow<Boolean>getIsOnline()Gives us information about the online state of the device. final StateFlow<User>getUser()Gives us information about the logged in user state. final BooleangetShowSystemMessagesState()Determines whether we should show system messages or not. final MessageFooterVisibilitygetMessageFooterVisibilityState()Regulates the message footer visibility. final DeletedMessageVisibilitygetDeletedMessageVisibilityState()Regulates the visibility of deleted messages. final UnitupdateLastSeenMessage(Message message)Attempts to update the last seen message in the channel or thread. final UnitloadNewerMessages(String messageId, Integer messageLimit)Loads newer messages of a channel following the currently newest loaded message. final UnitloadOlderMessages(Integer messageLimit)Loads older messages of a channel following the currently oldest loaded message. final UnitselectMessage(Message message)Triggered when the user long taps on and selects a message. final UnitdisplayPollMoreOptions(SelectedPoll selectedPoll)Triggered when the user taps the show more options button on the poll message. final UnitupdatePollState(Poll poll, Message message, PollSelectionType pollSelectionType)Triggered when the poll information has been changed and need to sync on the poll states. final UnitselectReactions(Message message)Triggered when the user taps on and selects message reactions. final UnitselectExtendedReactions(Message message)Triggered when the user taps the show more reactions button. final UnitopenMessageThread(Message message)Changes the current messageMode to be Thread with ThreadState and Loads thread data using ChatClient directly. final UnitdismissMessageAction(MessageAction messageAction)Used to dismiss a specific message action, such as delete, reply, edit or something similar. final UnitdismissAllMessageActions()Dismisses all message actions, when we cancel them in the rest of the UI. final UnitperformMessageAction(MessageAction messageAction)Triggered when the user selects a new message action, in the message overlay. final UnitdeleteMessage(Message message, Boolean hard)Removes the delete actions from our messageActions, as well as the overlay, before deleting the selected message. final UnitdeleteMessage(Message message)Removes the delete actions from our messageActions, as well as the overlay, before deleting the selected message. final UnitflagMessage(Message message, String reason, Map<String, String> customData)Removes the flag actions from our messageActions, as well as the overlay, before flagging the selected message. final UnitmuteUser(String userId, Integer timeout)Mutes the given user inside this channel. final UnitunmuteUser(String userId)Unmutes the given user inside this channel. final UnitbanUser(String userId, String reason, Integer timeout)Bans the given user inside this channel. final UnitunbanUser(String userId)Unbans the given user inside this channel. final UnitshadowBanUser(String userId, String reason, Integer timeout)Shadow bans the given user inside this channel. final UnitremoveShadowBanFromUser(String userId)Removes the shadow ban for the given user inside this channel. final UnitflagUser(String userId, String reason, Map<String, String> customData)Flags a user identified by the provided ID. final UnitunflagUser(String userId)Un-flags a user identified by the provided ID. final UnitleaveThread()Leaves the thread we're in and resets the state of the messageMode and both of the MessageListStates. final UnitremoveOverlay()Resets the MessageListStates, to remove the message overlay, by setting 'selectedMessage' to null. final UnitclearNewMessageState()Clears the NewMessageState from our UI state, after the user taps on the "Scroll to bottom" or "New Message" actions in the list or simply scrolls to the bottom. final MessagegetMessageById(String messageId)Returns a message with the given ID from the currentMessagesState. final UnitperformGiphyAction(GiphyAction action)Executes one of the actions for the given ephemeral giphy message. final UnitcastVote(Message message, Poll poll, Option option)Cast a vote for a poll in a message. final UnitcastAnswer(Message message, Poll poll, String answer)Cast an answer for a poll in a message. final UnitremoveVote(Message message, Poll poll, Vote vote)Remove a vote for a poll in a message. final UnitclosePoll(String pollId)Close a poll in a message. final UnitscrollToMessage(String messageId, String parentMessageId)Scrolls to message if in list otherwise get the message from backend. final UnitscrollToBottom(Integer messageLimit, Function0<Unit> scrollToBottom)Requests that the list scrolls to the bottom to the newest messages. final UnitscrollToFirstUnreadMessage()Scrolls to the first unread message in the list. final UnithideUnreadSeparator()Hides the unread label in the messages list (if already visible). final UnitsetMessagePositionHandler(MessagePositionHandler messagePositionHandler)Sets a handler which determines the position of a message inside a group. final UnitsetDateSeparatorHandler(DateSeparatorHandler dateSeparatorHandler)Sets the date separator handler which determines when to add date separators. final UnitsetThreadDateSeparatorHandler(DateSeparatorHandler threadDateSeparatorHandler)Sets the thread date separator handler which determines when to add date separators inside the thread. final UnitsetMessageFooterVisibility(MessageFooterVisibility messageFooterVisibility)Sets the value used to determine if message footer content is shown. final UnitsetDeletedMessageVisibility(DeletedMessageVisibility deletedMessageVisibility)Sets the value used to filter deleted messages. final UnitsetSystemMessageVisibility(Boolean areSystemMessagesVisible)Sets whether the system messages should be visible. final UnitaddPollOption(Poll poll, String option)Adds a new poll option to the given poll. final UnitpauseAudioRecordingAttachments()Pauses all running audio recording attachments. final UnittoggleOriginalText(String messageId)Toggles between the translated and the original text of the message (if the message was auto-translated). -
-
Method Detail
-
getCurrentMessagesState
final State<MessageListState> getCurrentMessagesState()
State handler for the UI, which holds all the information the UI needs to render messages.
It chooses between threadListState and messageListState based on if we're in a thread or not.
-
getMessageMode
final MessageMode getMessageMode()
Holds the current MessageMode that's used for the messages list. MessageMode.Normal by default.
-
getPollState
final PollState getPollState()
Holds the current PollState that's used for the messages list.
-
getChannel
final Channel getChannel()
The information for the current Channel.
-
getChannelState
final StateFlow<ChannelState> getChannelState()
Holds information about the current channel and is actively updated.
-
getTypingUsers
final List<User> getTypingUsers()
The list of typing users.
-
getMessageActions
final Set<MessageAction> getMessageActions()
Set of currently active MessageActions. Used to show things like edit, reply, delete and similar actions.
-
getIsStartedForThread
final Boolean getIsStartedForThread()
-
getIsInThread
final Boolean getIsInThread()
-
getIsShowingOverlay
final Boolean getIsShowingOverlay()
-
getIsShowingPollOptionDetails
final Boolean getIsShowingPollOptionDetails()
-
getConnectionState
final StateFlow<ConnectionState> getConnectionState()
Gives us information about the online state of the device.
-
getIsOnline
final Flow<Boolean> getIsOnline()
Gives us information about the online state of the device.
-
getUser
final StateFlow<User> getUser()
Gives us information about the logged in user state.
-
getShowSystemMessagesState
final Boolean getShowSystemMessagesState()
Determines whether we should show system messages or not.
-
getMessageFooterVisibilityState
final MessageFooterVisibility getMessageFooterVisibilityState()
Regulates the message footer visibility.
-
getDeletedMessageVisibilityState
final DeletedMessageVisibility getDeletedMessageVisibilityState()
Regulates the visibility of deleted messages.
-
updateLastSeenMessage
final Unit updateLastSeenMessage(Message message)
Attempts to update the last seen message in the channel or thread. We only update the last seen message the first time the data loads and whenever we see a message that's newer than the current last seen message.
- Parameters:
message- The message that is currently seen by the user.
-
loadNewerMessages
final Unit loadNewerMessages(String messageId, Integer messageLimit)
Loads newer messages of a channel following the currently newest loaded message. In case of threads this will do nothing.
- Parameters:
messageId- The id of the newest Message inside the messages list.messageLimit- The limit of messages to be loaded in the page.
-
loadOlderMessages
final Unit loadOlderMessages(Integer messageLimit)
Loads older messages of a channel following the currently oldest loaded message. Also will load older messages of a thread.
- Parameters:
messageLimit- The limit of messages to be loaded in the page.
-
selectMessage
final Unit selectMessage(Message message)
Triggered when the user long taps on and selects a message.
- Parameters:
message- The selected message.
-
displayPollMoreOptions
final Unit displayPollMoreOptions(SelectedPoll selectedPoll)
Triggered when the user taps the show more options button on the poll message.
- Parameters:
selectedPoll- The poll that holds the details to be drawn on the more options screen.
-
updatePollState
final Unit updatePollState(Poll poll, Message message, PollSelectionType pollSelectionType)
Triggered when the poll information has been changed and need to sync on the poll states.
- Parameters:
poll- The poll that holds the details to be drawn on the more options screen.message- The message that contains the poll information.
-
selectReactions
final Unit selectReactions(Message message)
Triggered when the user taps on and selects message reactions.
- Parameters:
message- The message that contains the reactions.
-
selectExtendedReactions
final Unit selectExtendedReactions(Message message)
Triggered when the user taps the show more reactions button.
- Parameters:
message- The selected message.
-
openMessageThread
final Unit openMessageThread(Message message)
Changes the current messageMode to be Thread with ThreadState and Loads thread data using ChatClient directly. The data is observed by using ThreadState.
- Parameters:
message- The selected message with a thread.
-
dismissMessageAction
final Unit dismissMessageAction(MessageAction messageAction)
Used to dismiss a specific message action, such as delete, reply, edit or something similar.
- Parameters:
messageAction- The action to dismiss.
-
dismissAllMessageActions
final Unit dismissAllMessageActions()
Dismisses all message actions, when we cancel them in the rest of the UI.
-
performMessageAction
final Unit performMessageAction(MessageAction messageAction)
Triggered when the user selects a new message action, in the message overlay.
We first remove the overlay, after which we consume the event and based on the type of the event, we do different things, such as starting a thread & loading thread data, showing delete or flag events and dialogs, copying the message, muting users and more.
- Parameters:
messageAction- The action the user chose.
-
deleteMessage
@JvmOverloads() final Unit deleteMessage(Message message, Boolean hard)
Removes the delete actions from our messageActions, as well as the overlay, before deleting the selected message.
- Parameters:
message- Message to delete.
-
deleteMessage
@JvmOverloads() final Unit deleteMessage(Message message)
Removes the delete actions from our messageActions, as well as the overlay, before deleting the selected message.
- Parameters:
message- Message to delete.
-
flagMessage
final Unit flagMessage(Message message, String reason, Map<String, String> customData)
Removes the flag actions from our messageActions, as well as the overlay, before flagging the selected message.
- Parameters:
message- Message to delete.
-
muteUser
final Unit muteUser(String userId, Integer timeout)
Mutes the given user inside this channel.
- Parameters:
userId- The ID of the user to be muted.timeout- The period of time for which the user will be muted, expressed in minutes.
-
unmuteUser
final Unit unmuteUser(String userId)
Unmutes the given user inside this channel.
- Parameters:
userId- The ID of the user to be unmuted.
-
banUser
final Unit banUser(String userId, String reason, Integer timeout)
Bans the given user inside this channel.
- Parameters:
userId- The ID of the user to be banned.reason- The reason for banning the user.timeout- The period of time for which the user will be banned, expressed in minutes.
-
unbanUser
final Unit unbanUser(String userId)
Unbans the given user inside this channel.
- Parameters:
userId- The ID of the user to be unbanned.
-
shadowBanUser
final Unit shadowBanUser(String userId, String reason, Integer timeout)
Shadow bans the given user inside this channel.
- Parameters:
userId- The ID of the user to be shadow banned.reason- The reason for shadow banning the user.timeout- The period of time for which the user will be shadow banned, expressed in minutes.
-
removeShadowBanFromUser
final Unit removeShadowBanFromUser(String userId)
Removes the shadow ban for the given user inside this channel.
- Parameters:
userId- The ID of the user for which the shadow ban is removed.
-
flagUser
final Unit flagUser(String userId, String reason, Map<String, String> customData)
Flags a user identified by the provided ID.
- Parameters:
userId- The ID of the user to flag.reason- The reason for flagging the user.customData- Additional key-value data submitted with the request.
-
unflagUser
final Unit unflagUser(String userId)
Un-flags a user identified by the provided ID.
- Parameters:
userId- The ID of the user to un-flag.
-
leaveThread
final Unit leaveThread()
Leaves the thread we're in and resets the state of the messageMode and both of the MessageListStates.
-
removeOverlay
final Unit removeOverlay()
Resets the MessageListStates, to remove the message overlay, by setting 'selectedMessage' to null.
-
clearNewMessageState
final Unit clearNewMessageState()
Clears the NewMessageState from our UI state, after the user taps on the "Scroll to bottom" or "New Message" actions in the list or simply scrolls to the bottom.
-
getMessageById
final Message getMessageById(String messageId)
Returns a message with the given ID from the currentMessagesState.
- Parameters:
messageId- The ID of the selected message.
-
performGiphyAction
final Unit performGiphyAction(GiphyAction action)
Executes one of the actions for the given ephemeral giphy message.
- Parameters:
action- The action to be executed.
-
castVote
final Unit castVote(Message message, Poll poll, Option option)
Cast a vote for a poll in a message.
- Parameters:
message- The message where the poll is.poll- The poll that want to be casted a vote.option- The option to vote for.
-
castAnswer
final Unit castAnswer(Message message, Poll poll, String answer)
Cast an answer for a poll in a message.
- Parameters:
message- The message where the poll is.poll- The poll that want to be casted an answer.answer- The answer that should be casted.
-
removeVote
final Unit removeVote(Message message, Poll poll, Vote vote)
Remove a vote for a poll in a message.
- Parameters:
message- The message where the poll is.poll- The poll that want to be casted a vote.vote- The vote that should be removed.
-
closePoll
final Unit closePoll(String pollId)
Close a poll in a message.
- Parameters:
pollId- The poll id.
-
scrollToMessage
final Unit scrollToMessage(String messageId, String parentMessageId)
Scrolls to message if in list otherwise get the message from backend. Does not work for threads.
- Parameters:
messageId- The Message id we wish to scroll to.parentMessageId- The ID of the parent Message if the message we want to scroll to is in a thread.
-
scrollToBottom
final Unit scrollToBottom(Integer messageLimit, Function0<Unit> scrollToBottom)
Requests that the list scrolls to the bottom to the newest messages. If the newest messages are loaded will set scroll the list to the bottom. If they are not loaded will request the newest data and once loaded will scroll to the bottom of the list.
- Parameters:
messageLimit- The message count we wish to load from the API when loading new messages.scrollToBottom- Notifies the ui to scroll to the bottom if the newest messages are in the list or have been loaded from the API.
-
scrollToFirstUnreadMessage
final Unit scrollToFirstUnreadMessage()
Scrolls to the first unread message in the list. If there are no unread messages, this method does nothing.
Important: It is possible that the first unread message is not loaded in the list yet. In this case, the method will first try to load that message, and then scroll to it.
-
hideUnreadSeparator
final Unit hideUnreadSeparator()
Hides the unread label in the messages list (if already visible).
-
setMessagePositionHandler
final Unit setMessagePositionHandler(MessagePositionHandler messagePositionHandler)
Sets a handler which determines the position of a message inside a group.
- Parameters:
messagePositionHandler- The handler to use.
-
setDateSeparatorHandler
final Unit setDateSeparatorHandler(DateSeparatorHandler dateSeparatorHandler)
Sets the date separator handler which determines when to add date separators. By default, a date separator will be added if the difference between two messages' dates is greater than 4h.
- Parameters:
dateSeparatorHandler- The handler to use.
-
setThreadDateSeparatorHandler
final Unit setThreadDateSeparatorHandler(DateSeparatorHandler threadDateSeparatorHandler)
Sets the thread date separator handler which determines when to add date separators inside the thread.
- Parameters:
threadDateSeparatorHandler- The handler to use.
-
setMessageFooterVisibility
final Unit setMessageFooterVisibility(MessageFooterVisibility messageFooterVisibility)
Sets the value used to determine if message footer content is shown.
- Parameters:
messageFooterVisibility- Changes the visibility of message footers.
-
setDeletedMessageVisibility
final Unit setDeletedMessageVisibility(DeletedMessageVisibility deletedMessageVisibility)
Sets the value used to filter deleted messages.
- Parameters:
deletedMessageVisibility- Changes the visibility of deleted messages.
-
setSystemMessageVisibility
final Unit setSystemMessageVisibility(Boolean areSystemMessagesVisible)
Sets whether the system messages should be visible.
- Parameters:
areSystemMessagesVisible- Whether system messages should be visible or not.
-
addPollOption
final Unit addPollOption(Poll poll, String option)
Adds a new poll option to the given poll.
- Parameters:
poll- The poll to add the option to.option- The option to add.
-
pauseAudioRecordingAttachments
final Unit pauseAudioRecordingAttachments()
Pauses all running audio recording attachments.
-
toggleOriginalText
final Unit toggleOriginalText(String messageId)
Toggles between the translated and the original text of the message (if the message was auto-translated).
- Parameters:
messageId- The ID of the message for which to toggle the original text.
-
-
-
-