-
-
Save cardoso/ef9c3e35fd58aee200baae72bc97020d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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