Chat Provider

ChatProvider is the general interface for UI and data operations across the component library. It provides default implementations of themes, repositories, services, and utils for the composition tree through CompositionLocalProvider.

You can easily configure ChatProvider to synchronize data from different services, including messages and channel occupancy, by setting its synchronize parameter to true.

Optimized data flow

Jetpack Compose passes data through the composition tree explicitly by means of parameters for composable functions. Using a tree is most often the simplest way to implement a data flow between services in your application.

Configuration

You can configure ChatProvider using the following parameters.

Required parameters

Required parameters don't have default values. If a parameter has a default value, it's optional.

ParameterTypeDefault valueDescription
pubNubPubNubn/aPubNub instance
databasePubNubDatabase<MessageDao<DBMessage, DBMessageWithActions>, MessageActionDao<DBMessageAction>, ChannelDao<DBChannel, DBChannelWithMembers>, MemberDao<DBMember, DBMemberWithChannels>, MembershipDao<DBMembership>>Database.initialize(LocalContext.current)Database object. It's used by all the repositories for CRUD operations (Create, Read, Update, Delete).
channelChannelId"channel.lobby"ID of the channel to subscribe to.
synchronizeBooleantrueParameter for data synchronization. If set to true, it will synchronize data in MessageService and OccupancyService.
content@Composable() () -> Unitn/aComposable content to draw.

Persistent database

For the repositories to work, ChatProvider creates the SQLite database. Additionally, it uses the Room library to provide an abstraction layer over SQLite and allow more robust data access.

To learn more about Room, refer to the official Android docs.

Structure

To be able to use repositories, ChatProvider creates a default database structure. The database object must implement the PubNubDatabase interface. This interface allows repositories to operate on the predefined data access objects (DAOs).

The following DAOs are declared:

  • MessageDao contains all the chat messages.
  • MessageActionDao contains all message actions (including message reactions).
  • ChannelDao contains all the channels.
  • MemberDao contains user objects.
  • MembershipDao contains the relation between members and channels.
interface PubNubDatabase<Message : MessageDao<*, *>, Action : MessageActionDao<*>, Channel : ChannelDao<*, *>, Member : MemberDao<*, *>, Membership : MembershipDao<*>> {    fun messageDao(): Message    fun actionDao(): Action    fun channelDao(): Channel    fun memberDao(): Member    fun membershipDao(): Membership}

Default initialization

To initialize the database with the default structure, you can use the Database.initialize(context) helper method. You must call this method with the application context as a parameter.

ParameterTypeDefault valueDescription
applicationContextContextn/aApplication context needed to create a database.
builder(RoomDatabase.Builder<DefaultDatabase>) -> RoomDatabase.Builder<DefaultDatabase>{ it }Room database builder. You can use it to add callbacks or initialize data.
fun initialize(  applicationContext: Context,  builder: (RoomDatabase.Builder<DefaultDatabase>) -> RoomDatabase.Builder<DefaultDatabase> = { it }): DefaultDatabase

Basic example

Call initialize() from the onCreate() method in Application().

class ChatComponentsApplication : Application() {    override fun onCreate() {        super.onCreate()        Database.initialize(this)    }}

Callback example

To use the Room callback, you must pass it as a parameter. In the example, the message is logged after creating and opening the database.

class ChatComponentsApplication : Application() {    override fun onCreate() {        super.onCreate()        Database.initialize(applicationContext) { database ->            database.addCallback(                object : RoomDatabase.Callback() {                    override fun onOpen(db: SupportSQLiteDatabase) {                        super.onOpen(db)                        Log.d("Database", "onOpen called")                    }                    override fun onCreate(db: SupportSQLiteDatabase) {                        super.onCreate(db)                        Log.d("Database", "onCreate called")
show all 21 lines

Local providers

Providers created in ChatProvider can be grouped by one of these types:

Common

Providers responsible for storing unspecified data:

  • LocalPubNub holds a PubNub instance.
  • LocalChannel holds the current chat channel.
  • LocalUser holds the current user object.

Utils

Providers responsible for formatting and error handling:

  • LocalMemberFormatter transforms a user ID into a member object.
  • LocalLogger can be used for handling logs across all the components.

Themes

List of providers used for styling components:

  • LocalMessageInputTheme describes the style for the MessageInput component.
  • LocalTypingIndicatorTheme describes the style for the TypingIndicator component.
  • LocalChannelListTheme describes the style for the ChannelList component.
  • LocalMemberListTheme describes the style for the MemberList component.
  • LocalMessageListTheme describes the style for the MessageList component.
  • LocalMessageTheme describes the style for the Message object.
  • LocalReactionTheme describes the style for the MessageAction object.
  • LocalIndicatorTheme describes the style for the PresenceIndicator state.
  • LocalProfileImageTheme describes the style for the ProfileImage component.
  • LocalMenuItemTheme describes the style for the MenuItem object.

Repositories

List of repository providers:

  • LocalChannelRepository provides an instance of ChannelRepository.
  • LocalMessageRepository provides an instance of MessageRepository.
  • LocalMessageActionRepository provides an instance of MessageActionRepository.
  • LocalMemberRepository provides an instance of MemberRepository.
  • LocalMembershipRepository provides an instance of MembershipRepository.

Services

List of service providers:

  • LocalChannelService provides the ChannelService implementation.
  • LocalMessageService provides the MessageService implementation.
  • LocalOccupancyService provides the OccupancyService implementation.
  • LocalActionService provides the ActionService implementation.
  • LocalMessageReactionService provides the MessageActionService implementation.
  • LocalTypingService provides the TypingService implementation.