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

List open channels

Copy link

The OpenChannelListView is a struct that is used to build an open channel list view. When there is no connection between the client app and Sendbird server, this struct is automatically called to reconnect the two. Once the client app is connected to Sendbird server, the struct is called and a list of channels in the client app is displayed. OpenChannelListView also handles core features including pagination and real-time updates. In many cases, the channel list is a good place to start your chat service.


Features

Copy link

Refer to the table below to see what features are available in OpenChannelListView.

FeatureDescription

Enter a channel

Allows users to enter an open channel by selecting a channel row on the list.

Create channel

Creates a new open channel.

Go back to previous view or dismiss current view

Allows users to go back to the previous view from the navigation stack or dismisses the current view.


Initialize

Copy link

You can start building a channel list view by initializing the OpenChannelListView struct. If you want to use the default channel list query, you can simply call OpenChannelListView(). If you want to use a custom channel list query, you can set the channelListQuery object in OpenChannelListViewProvider and pass it into OpenChannelListView when initializing.

Note: You can set the channelListQuery object when initializing OpenChannelListView. Otherwise, the default value is used.

import SwiftUI
import SendbirdSwiftUI
import SendbirdChatSDK

struct ContentView: View {
    @StateObject var provider = OpenChannelListViewProvider()
    
    var body: some View {
        OpenChannelListView(provider: provider)
    }
}

Init parameters

Copy link
ParameterTypeRequired

provider

OpenChannelListViewProvider

x

View Provider Init parameters

Copy link
ParameterTypeRequired

channelListQuery

OpenChannelListQuery

x


Accessing data and methods

Copy link

OpenChannelListViewProvider provides various data and methods related to the OpenChannelListView that you can use to customize the view. The below are @Published properties that you can access from the OpenChannelListViewProvider.

ParameterTypeDescription

channels

[OpenChannel]

A list of open channels

isLoading

Bool

Indicates whether the list is loading or not

MethodDescription

showCreateChannel()

presents CreateOpenChannelView

reloadChannelList()

reloads channel list

showChannel(channelURL:messageListParams:)

shows an Open Channel View

The below is an example of using OpenChannelListViewProvider data and methods.

import SwiftUI
import SendbirdSwiftUI

struct ContentView: View {
    @StateObject var provider: OpenChannelListViewProvider
    
    init() {
        // Set your custom list query parameters
        let params = OpenChannelListQueryParams()
        params.limit = 30
        
        let channelListQuery = OpenChannel.createOpenChannelListQuery(params: params)
        _provider = StateObject(wrappedValue: OpenChannelListViewProvider(channelListQuery: channelListQuery))
    }
    
    var body: some View {
        OpenChannelListView(
            provider: provider,
            headerItem: {
                .init()
                .titleView { config in
                    Text("\(provider.channels.count) channels")
                }
            }
        )
    }
}

Event Handling

Copy link

You can handle events of OpenChannelListView by using the event interfaces provided by Sendbird Chat SwiftUI. The below is a list of event interfaces that you can use to handle events in OpenChannelListView. There are more available interfaces [here](TODO: replace with ref doc URL).

interfaceDescription

onSendbirdSelectRow(_:)

Lets you react when one of the rows of open channel list is tapped

onSendbirdConnectionStateChange(:)

Lets you react when connection state changes

The following is an example of how to handle events in the OpenChannelListView.

import SwiftUI
import SendbirdSwiftUI

struct ContentView: View {
    @StateObject var provider: OpenChannelListViewProvider
    
    init() {
        let params = OpenChannelListQueryParams()
        params.limit = 30
        
        let channelListQuery = OpenChannel.createMyOpenChannelListQuery(params: params)
        _provider = StateObject(wrappedValue: OpenChannelListViewProvider(channelListQuery: channelListQuery))
    }
    
    var body: some View {
        OpenChannelListView(provider: provider)
            .onSendbirdSelectRow { indexPath in
                // Do something when a row is selected
            }
            .onSendbirdConnectionStateChange { channel in
                // Do something when connection state changed
            }
    }
}

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 open channel view.

You can use data and methods from the View Provider when customize the 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
ParameterTypeView builders

headerItem

() -> OpenChannelListType.HeaderItem

leftView
titleView
headerView

listItem

() -> OpenChannelListType.ListItem

rowView
- channelName
- channelPreview
- coverImage

The following code demonstrates how to replace the view items using headerItem. All other {Component}Items can be used in the same way.

Note : When you customize a parent view, customizations in the child views will not be applied. For example, if you customize the titleView in the headerItem, the customizations of the coverImage or titleLabel in the lower view items will not be applied.

OpenChannelListView(
    headerItem: {
        .init()
        .titleView { viewConfig in
            Text("My Open Channels")
                .fontWeight(.bold)
        }
    }
)

Full customization

Copy link

At this moment, this screen does not support entire customization.

DestinationViewBuilder

Copy link

Sendbird Chat SwiftUI is designed to internally navigate from each view to its connected view. However, if you need to customize the destination view, you can do so by using the interface provided by the DestinationViewBuilder.

DestinationViewBuilder method

Copy link
MethodViewBuilder Type

createChannelView

CreateOpenChannelViewBuilder

openChannelView

OpenChannelViewBuilder

The following code demonstrates how to replace the channel settings view connected from the channel view.

OpenChannelListView()
.openChannelView { channelURL, messageListParams in
    // Customize the messageListParams.
    messageListParams?.previousResultSize = 30
    messageListParams?.replyType = .onlyReplyToChannel
    messageListParams?.includeParentMessageInfo = true
    
    return OpenChannelView(
        provider: OpenChannelViewProvider(
            channelURL: channelURL,
            messageListParams: messageListParams
        )
    )
    // you can also add view adaptors to OpenChannelView.
}

Note : If you've customized a child view of another view, you need to set the destination view for all the views from the top to the destination view.