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 | Type | Default value | Description |
---|---|---|---|
pubNub | PubNub | n/a | PubNub instance |
database | 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). |
channel | ChannelId | "channel.lobby" | ID of the channel to subscribe to. |
synchronize | Boolean | true | Parameter for data synchronization. If set to true , it will synchronize data in MessageService and OccupancyService . |
content | @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:
MessageDao
contains all the chat messages.MessageActionDao
contains all message reactions.ChannelDao
contains all the channels.MemberDao
contains user objects.MembershipDao
contains the relation between members and channels.RemoteTimetokenDao
contains all the synchronization time windows for channels.
interface PubNubDatabase<Message : MessageDao<*, *>, Action : MessageActionDao<*>, Channel : ChannelDao<*, *>, Member : MemberDao<*, *>, Membership : MembershipDao<*>, RemoteKey : RemoteTimetokenDao<*>> {
fun messageDao(): Message
fun actionDao(): Action
fun channelDao(): Channel
fun memberDao(): Member
fun membershipDao(): Membership
fun remoteKeyDao(): RemoteKey
}
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 | Type | Default value | Description |
---|---|---|---|
applicationContext | Context | n/a | Application 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 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.
RepositoryProvider(ChatComponentsApplication.database.asPubNub()) {
Login.View()
}
Local providers
Providers created in ChatProvider
can be grouped by one of these types:
Common
Providers responsible for storing unspecified data:
LocalPubNub
holds aPubNub
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 amember
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 theMessageInput
component.LocalTypingIndicatorTheme
describes the style for theTypingIndicator
component.LocalChannelListTheme
describes the style for theChannelList
component.LocalMemberListTheme
describes the style for theMemberList
component.LocalMessageListTheme
describes the style for theMessageList
component.LocalMessageTheme
describes the style for theMessage
object.LocalReactionTheme
describes the style for theMessageAction
object.LocalIndicatorTheme
describes the style for thePresenceIndicator
state.LocalProfileImageTheme
describes the style for theProfileImage
component.LocalMenuItemTheme
describes the style for theMenuItem
object.
Repositories
List of repository providers:
LocalChannelRepository
provides an instance ofChannelRepository
.LocalMessageRepository
provides an instance ofMessageRepository
.LocalMessageActionRepository
provides an instance ofMessageActionRepository
.LocalMemberRepository
provides an instance ofMemberRepository
.LocalMembershipRepository
provides an instance ofMembershipRepository
.LocalRemoteTimetokenRepository
provides an instance ofRemoteTimetokenRepository
.
Services
List of service providers:
LocalChannelService
provides theChannelService
implementation.LocalMessageService
provides theMessageService
implementation.LocalOccupancyService
provides theOccupancyService
implementation.LocalActionService
provides theActionService
implementation.LocalMessageReactionService
provides theMessageActionService
implementation.LocalTypingService
provides theTypingService
implementation.