Message List View Model

LAST EDIT Aug 02 2021

MessageListViewModel works with MessageListView and uses StreamChatAndroidOffline library to communicate with Stream API.

The view model exposes two LiveData objects, as well as two values:

  • mode - distincts if the view is in thread mode, can be one of:

    • Thread

    • Normal

  • state - can be one of:

    • Loading

    • Result

    • NavigateUp

  • currentUser

  • channel

You can inform the view model about new events using onEvent method. Currently, MessageListViewModel supports the following events:

  • BaskButtonPressed

  • EndRegionReached

  • LastMessageRead

  • ThreadModeEntered

  • DeleteMessage

  • FlagMessage

  • GiphyActionSelected

  • RetryMessage

Creating Message List View Model


MessageListViewModel can be instantiated using ChannelViewModelFactory:

Binding with MessageListView


MessageListViewModel can be bound with MessageListView using MessageListViewModelBinding:

Calling bind method results in:

  • Initializing view with the channel and current user

  • Displaying new messages

  • Setting:

    • EndRegionReachedHandler

    • LastMessageReadHandler

    • OnMessageDeleteHandler

    • OnStartThreadHandler

    • OnMessageFlagHandler

    • OnSendGiphyHandler

    • OnMessageRetryHandler

Handling back button press


MessageListViewModel contains a mechanism for handling back button behaviour when you're in a thread. If you want to use it, you need to override Activitiy's back button behaviour:

The view model will exit the thread if needed and emit MessageListViewModel.State.NavigateUp if user isn't inside a thread. You need to handle this state by yourself - for example, by finishing the current Activity: