Skip to content

Instantly share code, notes, and snippets.

@cardoso
Last active February 23, 2021 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cardoso/ef9c3e35fd58aee200baae72bc97020d to your computer and use it in GitHub Desktop.
Save cardoso/ef9c3e35fd58aee200baae72bc97020d to your computer and use it in GitHub Desktop.
import SwiftUI
import StreamChat
struct ChannelsView: View {
@State
var channels: [ChatChannel] = []
@State
var createTrigger = false
@State
var searchTerm = ""
var body: some View {
VStack {
// createChannelView
// searchView
List(channels, id: \.self) { channel in
NavigationLink(destination: chatView(id: channel.cid)) {
Text(channel.name ?? channel.cid.id)
}
}.onAppear(perform: loadChannels)
}
.navigationBarItems(trailing:
Button(action: { self.createTrigger = true }) {
Text("Create")
}.disabled(self.createTrigger || !self.searchTerm.isEmpty)
)
.navigationBarTitle("Channels")
}
func chatView(id: ChannelId) -> ChatView {
return ChatView(
channel: ChatClient.shared.channelController(
for: id
).observableObject
)
}
func loadChannels() {
let filter: Filter<ChannelListFilterScope>
if searchTerm.isEmpty {
filter = .and([.in("members", values: [ChatClient.shared.currentUserId!]),
.equal("type", to: "messaging")])
} else {
filter = .and([.equal("type", to: "messaging")])
}
let controller = ChatClient.shared.channelListController(query: .init(filter: filter))
controller.synchronize { error in
if let error = error {
print(error)
return
}
self.channels = controller.channels
.filter {
if self.searchTerm.isEmpty {
return true
} else {
return $0.cid.id.contains(self.searchTerm)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment