Members of a group or supergroup channel can reply to each others' messages in a thread using the SBUMessageThreadViewController class. When a user taps Reply in thread in the Message menu of a user or file message in the SBUGroupChannelViewController class, the thread view appears. In the new thread, users can start a separate conversation in response to the parent message.
Note : Threads is currently a beta feature that's still undergoing testing and development for improvement. Some inadvertent issues may arise while implementing this key function. If you encounter any bugs or if you have any helpful feedback, contact our support team.
You can start building a thread view through the SBUMessageThreadViewController class. Use the init(channel:channelURL:parentMessage:parentMessageId:delegate:threadedMessageListParams:startingPoint:) initializer to create the instance and display the view as shown below.
// Case 1: Use `channelURL` and `parentMessageId`.
let messageThreadVC = SBUViewControllerSet.MessageThreadViewController.init(
channelURL: {CHANNEL_URL},
parentMessageId: {PARENT_MESSAGE_ID},
delegate: self,
startingPoint: {STARTING_POINT}
)
let navigationController = UINavigationController(rootViewController: messageThreadVC)
present(navigationController, animated: true)
// Case 1-1: Add the `threadedMessageListParams` object.
let messageThreadVC = SBUViewControllerSet.MessageThreadViewController.init(
channelURL: {CHANNEL_URL},
parentMessageId: {PARENT_MESSAGE_ID},
delegate: self,
threadedMessageListParams: {PARAMS},
startingPoint: {STARTING_POINT}
)
let navigationController = UINavigationController(rootViewController: messageThreadVC)
present(navigationController, animated: true)
// Case 2: Use the `channel` and `parentMessage` objects.
let messageThreadVC = SBUViewControllerSet.MessageThreadViewController.init(
channel: {CHANNEL},
parentMessage: {PARENT_MESSAGE},
delegate: self,
startingPoint: {STARTING_POINT}
)
let navigationController = UINavigationController(rootViewController: messageThreadVC)
present(navigationController, animated: true)
In the SBUMessageThreadViewController class, SBUMessageThreadModule and its components are used to create and display the thread view. The module is composed of three components: headerComponent, listComponent, and inputComponent. These components are created based on the SBUBaseChannelModule class.
Property name
Type
Default value
headerComponent
SBUMessageThreadModule.Header
SBUModuleSet.messageThreadModule.headerComponent
listComponent
SBUMessageThreadModule.List
SBUModuleSet.messageThreadModule.listComponent
inputComponent
SBUMessageThreadModule.Input
SBUModuleSet.messageThreadModule.inputComponent
When the loadView() method of the view controller is called, the module components get added to the view in the setupView() method of the Sendbird UIKit's view life cycle. Then, the configure method of each module component is called to set the property values and display the view.
The headerComponent in the thread view shows a title that says Thread by default, a back button that takes the user to the previous view, and a text button of the channel title in the titleView area. When a user taps on the text button, the group channel view appears. Each property corresponds to the elements in NavigationBar of SBUMessageThreadViewController.
The following table shows the parameters of the configure method of headerComponent.
Parameter name
Type
delegate
SBUMessageThreadModuleHeaderDelegate
theme
SBUChannelTheme
Note: To learn more about the delegate and the properties of headerComponent, go to the API reference page.
The listComponent shows a parent message and a list of all its replies, both text and file messages, in a chronological order. Within this component, the parent message is displayed through SBUParentMessageInfoView located above the thread replies. Replies sent by the current user are differentiated from those sent by other channel members by the color and location of the message bubble. The following table shows the parameters of the configure method of listComponent.
Parameter name
Type
delegate
SBUMessageThreadModuleListDelegate
dataSource
SBUMessageThreadModuleListDataSource
theme
SBUChannelTheme
Note: To learn more about the delegate, data source, and the properties of listComponent, go to the API reference page.
The inputComponent shows the input field of the thread where users can type and send their replies. The following table shows the parameters of the configure method of inputComponent.
Parameter name
Type
delegate
SBUMessageThreadModuleInputDelegate
dataSource
SBUMessageThreadModuleInputDataSource
theme
SBUChannelTheme
Note: To learn more about the delegate, data source, and the properties of inputComponent, go to the API reference page.
The SBUMessageThreadViewController class uses a view model that is a type of the SBUMessageThreadViewModel class. The view model is created in the initializer of the view controller through the createViewModel(channel:) method.
The following table shows the parameter of the createViewModel method.
Parameter name
Type
Description
channel
BaseChannel
Specifies the channel value. (Default: nil)
channelURL
String
Specifies the URL of the channel. (Default: nil)
parentMessage
BaseMessage
Specifies the parent message object. (Default: nil)
parentMessageId
Int64
Specifies the ID of the parent message. (Default: nil)
threadedMessageListParams
ThreadedMessageListParams
Specifies a parameter needed to retrieve threaded message list data from the Chat SDK. (Default: nil)
startingPoint
Int64
Specifies the timestamp value that marks the starting point of the thread list. (Default: nil)
Note: To learn more about the methods and the event delegates of the view model, go to this API reference page.
There are two ways to customize the view controller: change the default view controller value in the global SBUViewControllerSet class or set a single-use custom view controller in the key function.
The custom view controller in the code below is used in the following customization examples.
class CustomViewController: SBUMessageThreadViewController {
// Implement custom code here.
}
Change the value of SBUViewControllerSet.MessageThreadViewController.
SBUViewControllerSet.MessageThreadViewController = CustomViewController.self
// The view controller will now use `CustomViewController` as the default value.
Use a one-time custom view controller in the thread view.
let messageThreadVC = CustomViewController(channelURL: {CHANNEL_URL}, parentMessageId: {PARENT_MESSAGE_ID}, delegate: self, startingPoint: {STARTING_POINT}))
let navigationController = UINavigationController(rootViewController: messageThreadVC)
present(navigationController, animated: true)
There are two ways to customize a module component: change the default module component value in the global SBUModuleSet.messageThreadModule class or set a single-use custom module component in the view controller.
The custom header component in the code below is used in the following customization examples.
class CustomHeader: SBUMessageThreadModule.Header {
// Implement custom code here.
}
Change the value of SBUModuleSet.messageThreadModule.headerComponent.
SBUModuleSet.messageThreadModule.headerComponent = CustomHeader()
let messageThreadVC = SBUViewControllerSet.MessageThreadViewController(channelURL: {CHANNEL_URL}, parentMessageId: {PARENT_MESSAGE_ID}, delegate: self, startingPoint: {STARTING_POINT}))
// The `headerComponent` in `SBUMessageThreadViewController` will now use `customHeader` as the default value.
Change the module component in SBUMessageThreadViewController.