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.
Parameter | Type | Default value | Description |
---|---|---|---|
pubNub | PubNub | n/a | PubNub instance |
database | PubNubDatabase<MessageDao<DBMessage, DBMessage>, ChannelDao<DBChannel, DBChannelWithMembers>, MemberDao<DBMember, DBMemberWithChannels>, MembershipDao<DBMembership>> | Database.INSTANCE | 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 the predefined data access objects (DAOs).
The following DAOs are declared:
MessageDao
contains all the chat messages.ChannelDao
contains all the channels.MemberDao
contains user objects.MembershipDao
contains the relation between members and channels.
interface PubNubDatabase<Message : MessageDao<*, *>, Channel : ChannelDao<*, *>, Member : MemberDao<*, *>, Membership : MembershipDao<*>> {
fun messageDao(): Message
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.
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 }
)
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")
}
}
)
}
}
}
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.LocalErrorHandler
can be used for error handling 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.LocalIndicatorTheme
describes the style for thePresenceIndicator
state.LocalProfileImageTheme
describes the style for theProfileImage
component.
Repositories
List of repository providers:
LocalChannelRepository
provides an instance ofChannelRepository
.LocalMessageRepository
provides an instance ofMessageRepository
.LocalMemberRepository
provides an instance ofMemberRepository
.LocalMembershipRepository
provides an instance ofMembershipRepository
.
Services
List of service providers:
LocalMessageService
provides theMessageService
implementation.LocalChannelService
provides theChannelService
implementation.LocalMemberService
provides theMemberService
implementation.LocalTypingService
provides theTypingService
implementation.LocalOccupancyService
provides theOccupancyService
implementation.