Chat Provider for PubNub Chat Components for Android
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 using 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.
| Parameter | Default value | Description |
|---|---|---|
pubNubType: PubNub | n/a | PubNub instance |
databaseType: PubNubDatabase<MessageDao<DBMessage, DBMessageWithActions>, MessageActionDao<DBMessageAction>, ChannelDao<DBChannel, DBChannelWithMembers>, MemberDao<DBMember, DBMemberWithChannels>, MembershipDao<DBMembership>, RemoteTimetokenDao<DBRemoteTimetoken>> | Database.initialize(LocalContext.current) | Database object. It's used by all the repositories for CRUD operations (Create, Read, Update, Delete). |
channelType: ChannelId | "channel.lobby" | ID of the channel to subscribe to. |
synchronizeType: Boolean | true | Parameter for data synchronization. If set to true, it will synchronize data in MessageService and OccupancyService. |
contentType: @Composable() () -> Unit | n/a | Composable 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 predefined data access objects (DAOs).
The following DAOs are declared:
MessageDaocontains all the chat messages.MessageActionDaocontains all message reactions.ChannelDaocontains all the channels.MemberDaocontains user objects.MembershipDaocontains the relation between members and channels.RemoteTimetokenDaocontains all the synchronization time windows for channels.
1interface PubNubDatabase<Message : MessageDao<*, *>, Action : MessageActionDao<*>, Channel : ChannelDao<*, *>, Member : MemberDao<*, *>, Membership : MembershipDao<*>, RemoteKey : RemoteTimetokenDao<*>> {
2 fun messageDao(): Message
3 fun actionDao(): Action
4 fun channelDao(): Channel
5 fun memberDao(): Member
6 fun membershipDao(): Membership
7 fun remoteKeyDao(): RemoteKey
8}
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.
| Parameter | Default value | Description |
|---|---|---|
applicationContextType: Context | n/a | Application context needed to create a database. |
builderType: (RoomDatabase.Builder<DefaultDatabase>) -> RoomDatabase.Builder<DefaultDatabase> | { it } | Room database builder. You can use it to add callbacks or initialize data. |
1fun initialize(
2 applicationContext: Context,
3 builder: (RoomDatabase.Builder<DefaultDatabase>) -> RoomDatabase.Builder<DefaultDatabase> = { it }
4): DefaultDatabase
Basic example
Call initialize() from the onCreate() method in Application().
1class ChatComponentsApplication : Application() {
2
3 override fun onCreate() {
4 super.onCreate()
5 Database.initialize(this)
6 }
7}
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.
1class ChatComponentsApplication : Application() {
2
3 override fun onCreate() {
4 super.onCreate()
5 Database.initialize(applicationContext) { database ->
6 database.addCallback(
7 object : RoomDatabase.Callback() {
8 override fun onOpen(db: SupportSQLiteDatabase) {
9 super.onOpen(db)
10 Log.d("Database", "onOpen called")
11 }
12
13 override fun onCreate(db: SupportSQLiteDatabase) {
14 super.onCreate(db)
15 Log.d("Database", "onCreate called")
show all 21 linesStandalone usage
PubNub Chat Components for Android offer an option to use a local database and access repositories without the Chat Provider dependency and the need to create a PubNub instance. To access the repositories this way, use the RepositoryProvider method. This method sets the default values for all the repositories. This functionality is helpful for login screens when you cannot initialize a PubNub instance because the user is yet unknown.
1RepositoryProvider(ChatComponentsApplication.database.asPubNub()) {
2 Login.View()
3}
Local providers
Providers created in ChatProvider can be grouped by one of these types:
Common
Providers responsible for storing unspecified data:
LocalPubNubholds aPubNubinstance.LocalChannelholds the current chat channel.LocalUserholds the current user object.
Utils
Providers responsible for formatting and error handling:
LocalMemberFormattertransforms a user ID into amemberobject.LocalLoggercan be used for handling logs across all the components.
Themes
List of providers used for styling components:
LocalMessageInputThemedescribes the style for theMessageInputcomponent.LocalTypingIndicatorThemedescribes the style for theTypingIndicatorcomponent.LocalChannelListThemedescribes the style for theChannelListcomponent.LocalMemberListThemedescribes the style for theMemberListcomponent.LocalMessageListThemedescribes the style for theMessageListcomponent.LocalMessageThemedescribes the style for theMessageobject.LocalReactionThemedescribes the style for theMessageActionobject.LocalIndicatorThemedescribes the style for thePresenceIndicatorstate.LocalProfileImageThemedescribes the style for theProfileImagecomponent.LocalMenuItemThemedescribes the style for theMenuItemobject.
Repositories
List of repository providers:
LocalChannelRepositoryprovides an instance ofChannelRepository.LocalMessageRepositoryprovides an instance ofMessageRepository.LocalMessageActionRepositoryprovides an instance ofMessageActionRepository.LocalMemberRepositoryprovides an instance ofMemberRepository.LocalMembershipRepositoryprovides an instance ofMembershipRepository.LocalRemoteTimetokenRepositoryprovides an instance ofRemoteTimetokenRepository.
Services
List of service providers:
LocalChannelServiceprovides theChannelServiceimplementation.LocalMessageServiceprovides theMessageServiceimplementation.LocalOccupancyServiceprovides theOccupancyServiceimplementation.LocalActionServiceprovides theActionServiceimplementation.LocalMessageReactionServiceprovides theMessageActionServiceimplementation.LocalTypingServiceprovides theTypingServiceimplementation.