Message Input View Model

LAST EDIT Feb 16 2021

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

Copied!

MessageInputViewModel can be instantiated using ChannelViewModelFactory:

1
2
3
ChannelViewModelFactory factory = new ChannelViewModelFactory(channel.getCid()); 
MessageInputViewModel viewModel = new ViewModelProvider(this, factory) 
        .get(MessageInputViewModel.class);
1
2
3
private val viewModel: MessageInputViewModel by viewModels { 
    ChannelViewModelFactory(channel.cid) 
}

Binding with MessageInputView

Copied!

MessageInputViewModel can be bound with MessageInputView using MessageInputViewModelBinding:

1
MessageInputViewModelBinding.bind(viewModel, messageInputView, lifecycle);
1
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