Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting searchType dynamically in ChatChannelListViewModel #693

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### ✅ Added
- Make `CreatePollView` public [#685](https://github.com/GetStream/stream-chat-swiftui/pull/685)
- Allow customizing channel and message search in the `ChatChannelListViewModel` [#XYZ](ADD)
- Make `ChatChannelListViewModel.searchType` public and observable [#693](https://github.com/GetStream/stream-chat-swiftui/pull/693)
- Allow customizing channel and message search in the `ChatChannelListViewModel` [#690](https://github.com/GetStream/stream-chat-swiftui/pull/690)
- Allow overriding `ChatChannelListViewModel.performChannelSearch` and `ChatChannelListViewModel.performMessageSearch`
- Make `ChatChannelListViewModel.channelListSearchController` and `ChatChannelListViewModel.messageSearchController` public
### 🐞 Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,16 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
}
}

private let searchType: ChannelListSearchType
/// The type for search results.
///
/// Setting a new value will reload search results.
@Published public var searchType: ChannelListSearchType {
didSet {
guard searchType != oldValue else { return }
performSearch()
}
}

/// The channel search controller which should be created only by ``performChannelSearch()``.
public var channelListSearchController: ChatChannelListController?
/// The message search controller which should be created only by ``performMessageSearch()``.
Expand All @@ -116,7 +125,7 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
@Published public var searchText = "" {
didSet {
if searchText != oldValue {
handleSearchTextChange()
performSearch()
}
}
}
Expand Down Expand Up @@ -337,7 +346,7 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
.filter { $0.id != chatClient.currentUserId }
}

private func handleSearchTextChange() {
private func performSearch() {
if searchText.isEmpty {
clearSearchResults()
return
Expand Down Expand Up @@ -393,10 +402,11 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
/// Creates a new message search controller, sets its delegate, and triggers the search operation.
open func performMessageSearch() {
messageSearchController = chatClient.messageSearchController()
messageSearchController?.delegate = self
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it because otherwise delegate makes a callback with 0 results before sync finishes and this makes the UI to flash.

loadingSearchResults = true
messageSearchController?.search(text: searchText) { [weak self] _ in
self?.loadingSearchResults = false
self?.messageSearchController?.delegate = self
self?.updateMessageSearchResults()
}
}

Expand All @@ -420,7 +430,7 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
}

private func updateMessageSearchResults() {
guard let messageSearchController = messageSearchController else {
guard let messageSearchController, searchType == .messages else {
return
}

Expand All @@ -433,7 +443,7 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
return ChannelSelectionInfo(
channel: channel,
message: message,
searchType: .channels
searchType: .messages
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UI was flickering when changing the search type because incorrect search type was set here.

)
}
DispatchQueue.main.async {
Expand All @@ -443,7 +453,7 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
}

private func updateChannelSearchResults() {
guard let channelListSearchController = self.channelListSearchController else {
guard let channelListSearchController, searchType == .channels else {
return
}

Expand Down
Loading