/ SDKs / Android
SDKs
Chat SDKs Android v4
Chat SDKs Android
Chat SDKs
Android
Version 4

Group channel collection

Copy link

A GroupChannelCollection instance allows you to swiftly create a channel list view that remains up to date on all channel-related events. This page explains how to draw a view using the collection.


Create a collection

Copy link

You can create a GroupChannelCollection instance through the createGroupChannelCollection() method.

First, create a GroupChannelListQuery instance through the createMyGroupChannelListQuery() method and its query setters. This determines which channel to include in the channel list and how to list channels in order.

Once the collection is created, you should call loadMore().

// First, create a GroupChannelListQuery instance in GroupChannelCollection.
val query = GroupChannel.createMyGroupChannelListQuery(
    GroupChannelListQueryParams().apply {
        includeEmpty = true
        order = GroupChannelListQueryOrder.CHRONOLOGICAL 
        // Acceptable values are CHRONOLOGICAL, LATEST_LAST_MESSAGE, CHANNEL_NAME_ALPHABETICAL,
        // and METADATA_VALUE_ALPHABETICAL.
        // You can add other params setters.
    }
)
// Create a GroupChannelCollection instance.
val collection: GroupChannelCollection = SendbirdChat.createGroupChannelCollection(
    GroupChannelCollectionCreateParams(query)
)

Pagination

Copy link

A GroupChannelCollection instance retrieves more channels to display in the view through the loadMore() method.

Whenever a scroll reaches the bottom of the channel list view, the loadMore() method is called and hasMore first checks if there are more channels to load. If so, loadMore() fetches them.

The loadMore() method should also be called after you've created a GroupChannelCollection instance.

KotlinKTX
// Check whether there are more channels to load before calling loadMore().
if (collection.hasMore) {
    collection.loadMore { channels, e ->
        if (e != null) {
            // Handle error.
            return@loadMore
        }

        // Add channels to your data source.
    }
}

Channel events

Copy link

Use the groupChannelCollectionHandler property in GroupChannelCollectionCreateParams to determine how the client app reacts to channel-related events.

This is called whenever a new channel is created as a real-time event or changelog sync is prompted when the client app is back online.

The following table shows possible cases where each event handler can be called.

EventCalled when

onChannelsAdded()

- A new group channel is created as a real-time event.
- New group channels are fetched through changelog sync.

onChannelsUpdated()

- The channel information that is included in the user's current chat view is updated as a real-time event.
- Updated channel information is fetched through changelog sync.

onChannelsDeleted()

- A group channel is deleted as a real-time event.
- A channel deletion event is fetched through changelog sync.

val collection: GroupChannelCollection = SendbirdChat.createGroupChannelCollection(
    GroupChannelCollectionCreateParams(query).apply {
        groupChannelCollectionHandler = object : GroupChannelCollectionHandler {
            override fun onChannelsAdded(context: GroupChannelContext, channels: List<GroupChannel>) {
                // Add new channels to your data source.
            }

            override fun onChannelsUpdated(context: GroupChannelContext, channels: List<GroupChannel>) {
                // Update the existing channels in your data source.
            }

            override fun onChannelsDeleted(context: GroupChannelContext, deletedChannelUrls: List<String>) {
                // Delete the channels with the matching deletedChannelUrls from your data source.
            }
        }
    }
)

Loaded channels

Copy link

The GroupChannelCollection holds list of channels that are loaded by loadMore() or by channel events, the channelList. It is recommended to use this list as the datasource of your RecyclerView as this list is the most updated list.

val channelList = collection.channelList

Dispose of the collection

Copy link

The dispose() method should be called when you destroy the channel list screen to notify the SDK that this collection is no longer visible.

collection.dispose()