LiveData & ViewModel

Last Edit: Mar 03 2020

While you can build any type of chat using the low level client it takes quite a bit of work. Stream also exposes LiveData objects to help accelerate your development. We currently expose the LiveData objects using Google's ViewModel architecture component. It's on our roadmap to add RXJava support. So if you're interested in that be sure to contact us or leave some feedback on these docs.

Stream provides 2 ViewModels. The ChannelViewModel and the ChannelListViewModel.

ChannelViewModel

Here's how you can instantiate a channellist viewModel:


val channel = client.channel(channelType, channelID)
val viewModelFactory = ChannelViewModelFactory(application, channel)
val viewModel = ViewModelProvider(this, viewModelFactory).get(ChannelViewModel::class.java)
                    

Channel channel = client.channel(channelType, channelID);
ChannelViewModelFactory viewModelFactory = new ChannelViewModelFactory(this.getApplication(), channel);
viewModel = new ViewModelProvider(this, viewModelFactory).get(ChannelViewModel.class);
                    

The ChannelViewModel exposes the following livedata objects:

messages

The messages livedata object shows the list of messages for this channel. It's updated when new messages are added, removed, edited or when reactions are added/removed.

watcherCount

The watcher count tells you how many people are currently watching this channel.

typingUsers

The typing users livedata object returns a list of user objects who are currently typing.

messageInputText

The text the current user typed into the message input. This is connected using two way databinding.

reads

Read state for all the users in the channel. IE. User Alex read all messages up to a certain timestamp. This enables you to implement read state indicators like you see in Facebook messenger.

hasNewMessages

hasNewMessages is true if the there are new messages and the users has scrolled up. In that scenario most messaging apps show a little icon/bar that there are new messages.

The full java docs for the ChannelViewModel can be found here.

ChannelListViewModel

This is how you create an instance of the channel List ViewModel.


val viewModel = ViewModelProvider(this).get(ChannelListViewModel::class.java)
// query all channels of type messaging
val filter = and(eq("type", "messaging"), `in`("members", "summer-brook-2"))
viewModel.setChannelFilter(filter)
                    

ChannelListViewModel viewModel = new ViewModelProvider(this).get(ChannelListViewModel.class);
// query all channels of type messaging
FilterObject filter = and(eq("type", "messaging"), in("members", "twilight-lab-0"));
viewModel.setChannelFilter(filter);
                    

The ChannelListViewModel exposes the following livedata objects:

channels

The list of channels. The channel list is automatically updated when:

  • The current user is added as a member to the new channel

  • The channel is hidden or shown

  • There is a new message in the channel or a message is updated

  • The channel read state changes

  • The channel is updated or deleted

The full java docs for the ChannelListViewModel are here.