Skip to content

How to update ChannelListView with a query? #586

@hyqshr

Description

@hyqshr

What did you do?

I try to build a chat type filter so user click each type picker and I can update ChannelListView with the query result.

image

I tried to build a dynamic controller and pass to ChatChannelListView:

struct ConversationView: View {
    @EnvironmentObject var userViewModel: UserViewModel
    @State private var showSelectSheet = false
    @State private var selectedChatType: String = "Team" // Add a state for the selected chat type
    @Injected(\.chatClient) private var chatClient: ChatClient // Inject chatClient

    var body: some View {
        VStack {
            ChatChannelListView(
                viewFactory: CustomChatUIFactory(userViewModel: userViewModel, selectedChatType: $selectedChatType),
                channelListController: makeCustomChannelListController(for: selectedChatType) // Pass the dynamic controller
            )
        }
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                Button(action: {
                    showSelectSheet = true
                }) {
                    Image(systemName: "plus.circle")
                        .foregroundColor(.primary)
                }
            }
        }
        .sheet(isPresented: $showSelectSheet) {
            SelectChatMembersView()
        }
    }
    
    private func makeCustomChannelListController(for chatType: String) -> ChatChannelListController {
        let filter: Filter<ChannelListFilterScope>
        switch chatType {
        case "Team":
            filter = .and([.equal(.type, to: .team), .containMembers(userIds: [chatClient.currentUserId!])])
        case "Tenants":
            filter = .equal(.type, to: .custom("tenant")) // Assuming "tenant" is a custom channel type
        case "Applicants":
            filter = .equal(.type, to: .custom("applicant")) // Assuming "applicant" is a custom channel type
        default:
            filter = .containMembers(userIds: [chatClient.currentUserId ?? ""])
        }

        let controller = chatClient.channelListController(
            query: .init(
                filter: filter,
                sort: [.init(key: .lastMessageAt, isAscending: true)],
                pageSize: 10
            )
        )
        print("Selected Chat Type: \(chatType)")
        print("Controller: \(controller)")
        print("Count \(controller.channels.count)")
      
        return controller
    }
}

What did you expect to happen?

The ChannelListView correctly updated when user tap the picker.

What happened instead?

The screen freeze after tap the filter, can not open any chat afterward.

GetStream Environment

**GetStream Chat version:4.60.0
GetStream Chat frameworks: StreamChat, StreamChatSwiftUI
iOS version:
Swift version:
**Xcode version:15.4
**Device:Simulator iPhone 15 Pro

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions