Customize channel list data processing
The data processing mechanism supports the following customization operations for the channel list:
- Filter channels by channel type. Filtered channels are not displayed in the channel list.
- Filter channels by custom rules. The channel list page displays only the filtered data.
- Set aggregated display for specific channel types. Channels of the specified type are merged and displayed together.
Data processor (abstract class)
The data processor is provided as an abstract class. Supported versions include stable releases in the 5.1.3.x series, 5.1.5, and all later versions. For the data processing mechanism in other versions, see the Data processor interface section.
Data processor description
Name: BaseDataProcessor
This abstract class provides three overridable methods. You can selectively override them to implement custom functionality based on your business requirements.
| Return type | Method |
|---|---|
| ChannelType[] | supportedTypes() |
| List<T> | filtered(List<T> data) |
| boolean | isGathered(ChannelType type) |
| boolean | isGathered(ChannelIdentifier identifier) |
Method descriptions
-
supportedTypes()Array of channel types supported by the channel list page. By default, all channel types are supported.
-
filtered(List<T> data)Filters channel data. This method is called when channels are batch-loaded from the database and when new channels are created from incoming messages.
Parameter Type Description data List<T> Data to be filtered -
isGathered(ChannelType type)Sets whether a channel type is displayed in aggregated form. Return true to enable aggregated display; otherwise, return false.
When true is returned, all channels of this type are displayed as a single item in the channel list. Tapping this aggregated channel navigates to the aggregated channel list, which displays all channels of that type. For details, see Aggregate channels by type.
-
isGathered(ChannelIdentifier identifier)Parameter Type Description identifier ChannelIdentifier Contains ChannelTypeandchannelId(String).(Default method of the DataProcessor interface) Sets whether a channel type is displayed in aggregated form. Return true to enable aggregated display; otherwise, return false.
When true is returned, all channels of this type are displayed as a single item in the channel list. Tapping this aggregated channel navigates to the aggregated channel list, which displays all channels of that type. For details, see Aggregate channels by type.
Customize data processing
-
Create a custom data processor. Declare a custom data processor class that extends the SDK's
BaseDataProcessorand override the methods you need to customize.The following example uses a custom data processor class named
MyDataProcessor:Javapublic class MyDataProcessor extends BaseDataProcessor<BaseChannel> {
/**
* Customize the channel types supported by the channel list page.
* This example supports only direct channels and group channels.
*/
@Override
public ChannelType[] supportedTypes() {
ChannelType[] types = {ChannelType.DIRECT, ChannelType.GROUP};
return types;
}
/**
* Customize channels to be displayed in aggregated form.
* This example sets system channels to aggregated display.
*/
@Override
public boolean isGathered(ChannelType type) {
if (type.equals(ChannelType.SYSTEM)) {
return true;
} else {
return false;
}
}
/**
* Customize channels to be displayed in aggregated form.
* This example sets system channels to aggregated display.
*/
@Override
public boolean isGathered(ChannelIdentifier identifier) {
if (identifier.getChannelType().equals(ChannelType.SYSTEM)) {
return true;
} else {
return false;
}
}
} -
Set the data processor. Before opening the channel list page, call the following method to set your custom data processor.
JavaNCChatUIConfig.channelListConfig().setDataProcessor(new MyDataProcessor());MyDataProcessorin the example code above is the custom data processor from step 1.
Data processor interface
You can also intercept channels through the data processor interface.
When you implement the data processor interface in Android Studio, the default return value for some methods is null, which filters out all channel list data and displays an empty channel list. Make sure to modify the return value to either your custom processed data or the original data.
If you need to implement channel list filtering or sorting, we recommend using the data processor first.
Interface description
The data processor interface provides the following three methods:
public interface DataProcessor<T> {
/**
* Sets the channel types supported by the channel list page
* @return Supported channel types
*/
ChannelType[] supportedTypes();
/**
* Filters channel data.
* <p>This method is called when channels are batch-loaded from the database
* and when new channels are created from incoming messages</p>
* @param data Data to be filtered
* @return Filtered data.
*/
List<T> filtered(List<T> data);
/**
* Sets whether a channel type is displayed in aggregated form.
* @param type Channel type
*/
boolean isGathered(ChannelType type);
/**
* Sets whether a channel type is displayed in aggregated form.
*
* @param identifier Channel identifier
*/
default boolean isGathered(ChannelIdentifier identifier) {
if (identifier == null || identifier.getChannelType() == null) {
return false;
}
return isGathered(identifier.getChannelType());
}
}
Customize data processing
-
Declare a custom data processor class that implements the
DataProcessorinterface and implement each method according to your custom requirements.Javaprivate ChannelType[] supportedTypes = {ChannelType.DIRECT,
ChannelType.GROUP, ChannelType.SYSTEM};
public class MyDataProcessor implements DataProcessor<BaseChannel> {
@Override
public ChannelType[] supportedTypes() {
//Return the custom channel types supported by the channel list.
return supportedTypes;
}
@Override
public List<BaseChannel> filtered(List<BaseChannel> data) {
//Return the filtered data. This example does not filter and returns the original data.
return data; //Do not return null!!!!
}
@Override
public boolean isGathered(ChannelType type) {
//Customize system channels to aggregated display; other channels are not aggregated.
if (type.equals(ChannelType.SYSTEM)) {
return true;
}
return false;
}
@Override
public boolean isGathered(ChannelIdentifier identifier) {
//Customize system channels to aggregated display; other channels are not aggregated.
if (identifier.getChannelType().equals(ChannelType.SYSTEM)) {
return true;
}
return false;
}
} -
Use the
setDataProcessormethod to set your custom data processor class in the SDK. Call this method before opening the channel list page.JavaNCChatUIConfig.channelListConfig().setDataProcessor(new MyDataProcessor());