/ UIKit / Android
UIKit
Chat UIKit Android v3
Chat UIKit Android
Chat UIKit
Android
Version 3

Customize ViewModels

Copy link

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

Copy link

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

Copy link

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

Copy link

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.

FragmentViewModel

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

Copy link

There are two ways to configure the custom ViewModels you've created.

Set globally across the application

Copy link

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.

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        ViewModelProviders.channel = ChannelViewModelProvider {  owner, channelUrl, params, config ->
            ViewModelProvider(
                owner,
                CustomViewModelFactory(channelUrl, params, config)
            )[channelUrl, CustomChannelViewModel::class.java]
        }
    }
}

class CustomViewModelFactory(
    private val channelUrl: String,
    private val params: MessageListParams?,
    private val config: ChannelConfig
) : ViewModelFactory(channelUrl, params, config) {
    @Suppress("UNCHECKED_CAST")
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(CustomChannelViewModel::class.java)) {
            return CustomChannelViewModel(channelUrl) as T
        }
        return super.create(modelClass)
    }
}

class CustomChannelViewModel(
    channelUrl: String
) : ChannelViewModel(channelUrl, null) {
    // Add a new API.
    fun leaveChannel(handler: OnCompleteHandler?) {
        channel?.leave { e -> handler?.onComplete(e) }
    }
}

Set in a specific fragment

Copy link

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.

class CustomChannelFragment : ChannelFragment() {
    override fun onCreateViewModel(): ChannelViewModel {
        return ViewModelProvider(
            this,
            CustomViewModelFactory(channelUrl)
        )[channelUrl, CustomChannelViewModel::class.java]
    }
}

For an in-depth practical demonstration, see our sample code.