Customize ViewModels
Sendbird UIKit's ViewModels are structured similarly to the basic implementation of Android's ViewModel. These ViewModels are primarily responsible for managing data communications with the Chat SDK. Developers can customize these ViewModels using various methods detailed in our API reference page.
Understanding ViewModels
In Sendbird UIKit, each fragment is associated with a unique ViewModel
. This ensures that the data and operation specific to that fragment are efficiently managed.
When to customize
The following are some example cases of when to consider customization:
- Integrating new APIs: To add new functionalities or data sources not available in the default implementation.
- Managing new data types: To introduce new data or change the way existing data is processed and presented.
- Modifying existing interfaces: To modify the default interfaces of the ViewModel or modify how certain methods behave.
UIKit's ViewModels
The following table shows a list of all fragments and its corresponding ViewModel.
When using ViewModels, the ViewModel
is created in the Fragment
through the ViewModelProviders
. The ViewModelProvider
then uses the ViewModelFactory
class to produce a ViewModel that is suitable for the specific Fragment.
Fragment | ViewModel |
---|---|
ChannelListFragment | ChannelListViewModel |
ChannelFragment | ChannelViewModel |
OpenChannelFragment | OpenChannelViewModel |
CreateChannelFragment | CreateChannelViewModel |
ChannelSettingsFragment | ChannelSettingsViewModel |
OpenChannelSettingsFragment | OpenChannelSettingsViewModel |
InviteUserFragment | InviteUserViewModel |
RegisterOperatorFragment | RegisterOperatorViewModel |
OpenChannelRegisterOperatorFragment | OpenChannelRegisterOperatorViewModel |
MemberListFragment | MemberListViewModel |
ParticipantsListFragment | ParticipantListViewModel |
BannedUserListFragment | BannedUserListViewModel |
OpenChannelBannedUserListFragment | OpenChannelBannedUserListViewModel |
MutedMemberListFragment | MutedMemberListViewModel |
OpenChannelMutedParticipantListFragment | OpenChannelMutedParticipantListViewModel |
OperatorListFragment | OperatorListViewModel |
OpenChannelOperatorListFragment | OpenChannelOperatorListViewModel |
ModerationFragment | ModerationViewModel |
OpenChannelModerationFragment | OpenChannelModerationViewModel |
MessageSearchFragment | MessageSearchViewModel |
MessageThreadFragment | MessageThreadViewModel |
Apply custom ViewModels
There are two ways to configure the custom ViewModels you've created.
Set globally across the application
The customized ViewModel should be provided through the ViewModelProvider
. Inherit from the UIKit's ViewModelProvider
and implement your custom ViewModel
within the onCreate()
function.
Each time a module for the same screen is created, it uses the same ViewModel
class. Additionally, each ViewModelProvider
provides the arguments needed for creation. It's recommended to set this up within the onCreate()
method of your Application as shown in the code below.
Set in a specific fragment
If you wish to configure a different UI for each fragment, you can customize directly in the fragment class as shown in the code below.
For an in-depth practical demonstration, see our sample code.