Chat UIKit SwiftUI v3
Chat UIKit SwiftUI
Chat UIKit
SwiftUI
Version 3

Reply to messages

Copy link

Members of a group or supergroup channel can reply to each others' messages in a thread using the MessageThreadView structure. When a user taps Reply in thread in the Message menu of a user or file message in the GroupChannelView class, the thread view appears. In the new thread, users can start a separate conversation in response to the parent message.


Initialize

Copy link

You can start building a thread view by initializing the MessageThreadView structure.

// 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)

Init parameters

Copy link
Property nameTypeDefault 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.

HeaderComponent

Copy link

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 nameType

delegate

SBUMessageThreadModuleHeaderDelegate

theme

SBUChannelTheme

Note: To learn more about the delegate and the properties of HeaderComponent, go to the API reference page.

ListComponent

Copy link

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 nameType

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.

InputComponent

Copy link

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 nameType

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.

View model

Copy link

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 nameTypeDescription

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.

SBUMessageThreadViewController properties

Copy link

To learn more about the properties of SBUMessageThreadViewController, go to the API reference page.


Customization

Copy link

Sendbird Chat SwiftUI provides a View customization and DestinationViewBuilder.

  • View customization: Our SwiftUI SDK allows you to selectively customize view elements. To learn more about the customization and our SwiftUI is designed, see the customization guide.
  • DestinationViewBuilder: Use DestinationViewBuilder to customize the destination views that are navigatable from the group channel view.

Note : Visit our Github Sample to see the custom sample implementation for each item.

Partial customization

Copy link

You can easily customize a specific part of a View, which particularly comes in handy when changing only a certain area in the View. To do so, use the View Builders that Sendbird has predefined and its a ViewConfig. The ViewConfig contains the data needed to render the view and its parameters can be found in the table below.

Parameter

Copy link
class CustomViewController: SBUMessageThreadViewController {
    // Implement custom code here.
}
  1. Change the value of SBUViewControllerSet.MessageThreadViewController.
SBUViewControllerSet.MessageThreadViewController = CustomViewController.self

// The view controller will now use `CustomViewController` as the default value.
  1. 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)

Module component

Copy link

There are two ways to customize a module component: change the default module component type 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.
}
  1. Change the value of SBUModuleSet.MessageThreadModule.HeaderComponent.
SBUModuleSet.MessageThreadModule.HeaderComponent = CustomHeader.self

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.
  1. Change the module component in SBUMessageThreadViewController.
let messageThreadVC = SBUViewControllerSet.MessageThreadViewController(channelURL: {CHANNEL_URL}, parentMessageId: {PARENT_MESSAGE_ID}, delegate: self, startingPoint: {STARTING_POINT}))
messageThreadVC.headerComponent = CustomHeader()

Note: To learn more about the methods of SBUMessageThreadModule, go to the API reference page.

View model

Copy link

In order to use a customized view model or customize the existing view model's event delegate, you must override the view controller.

  1. Use a customized view model.
class CustomViewController: SBUMessageThreadViewController {
    override func createViewModel(
        channel: BaseChannel? = nil,
        channelURL: String? = nil,
        parentMessage: BaseMessage? = nil,
        parentMessageId: Int64? = nil,
        threadedMessageListParams: ThreadedMessageListParams? = nil,
        startingPoint: Int64? = nil
    ) {
        self.viewModel = SBUMessageThreadViewModel(
            channel: channel,
            channelURL: channelURL,
            parentMessage: parentMessage,
            parentMessageId: parentMessageId,
            threadedMessageListParams: threadedMessageListParams,
            startingPoint: startingPoint,
            delegate: self,
            dataSource: self
        )

        // Implement your code here.
    }
}
  1. Customize the view model's event delegate. The code snippet below shows one of the many customizable event delegates.
extension CustomViewController: SBUMessageThreadViewModelDelegate {
    override func messageThreadViewModel(
        _ viewModel: SBUMessageThreadViewModel,
        didUpdateParentMessage parentMessage: BaseMessage?
    ) {
        // Implement your code here.
    }
}

DestinationViewBuilder

Copy link

This screen has no DestinationViewBuilder because there's no navigational elements, such as buttons or links to other screens.