Message Input View Model

Last Edit: Oct 28 2020

MessageInputViewModel works with MessageInputView and just like other Stream's ViewModels - it uses StreamChatAndroidOffline library to communicate with Stream API.

MessageInputView gives users the possibility to enter a new chat message. Therefore, the corresponding view model contains methods that allow communicating with API and changing the view's state:

  • getActiveThread

  • setActiveThread

  • resetThread

  • sendMessage

  • sendMessageWithAttachments

  • editMessage

  • keystroke

  • stopTyping

Moreover, MessageInputViewModel exposes multiple LiveData objects which also might be helpful for updating the view's state:

  • commands

  • members

  • editMessage

Creating Message Input View Model

MessageInputViewModel can be instantiated using ChannelViewModelFactory:


ChannelViewModelFactory factory = new ChannelViewModelFactory(channel.getCid());
MessageInputViewModel viewModel = new ViewModelProvider(this, factory)
        .get(MessageInputViewModel.class);
                    

private val viewModel: MessageInputViewModel by viewModels {
    ChannelViewModelFactory(channel.cid)
}
                    

Binding with MessageInputView

MessageInputViewModel can be bound with MessageInputView using MessageInputViewModelBinding:


MessageInputViewModelBinding.bind(viewModel, messageInputView, lifecycle);
                    

viewModel.bindView(messageInputView, viewLifecycleOwner)
                    

bind method is responsible for configuring view's:

  • Message send handler

  • Available commands

  • Channel members

  • Different modes (normal, thread, edit)

  • Typing listener