From 3e722f7c7fc5851b50da4ab31dc37e602dec154f Mon Sep 17 00:00:00 2001
From: Toomas Vahter <toomas.vahter@getstream.io>
Date: Wed, 18 Dec 2024 13:42:15 +0200
Subject: [PATCH] Allow setting searchType dynamically in
 ChatChannelListViewModel

---
 CHANGELOG.md                                  |  1 +
 .../ChatChannelListViewModel.swift            | 24 +++++++++++++------
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e5cf3b68..d7b4e275 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@ 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)
+- 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` [#XYZ](ADD)
   - Allow overriding `ChatChannelListViewModel.performChannelSearch` and `ChatChannelListViewModel.performMessageSearch`
   - Make `ChatChannelListViewModel.channelListSearchController` and `ChatChannelListViewModel.messageSearchController` public
diff --git a/Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListViewModel.swift b/Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListViewModel.swift
index c0e09056..ca892b58 100644
--- a/Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListViewModel.swift
+++ b/Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListViewModel.swift
@@ -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()``.
@@ -116,7 +125,7 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
     @Published public var searchText = "" {
         didSet {
             if searchText != oldValue {
-                handleSearchTextChange()
+                performSearch()
             }
         }
     }
@@ -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
@@ -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
         loadingSearchResults = true
         messageSearchController?.search(text: searchText) { [weak self] _ in
             self?.loadingSearchResults = false
+            self?.messageSearchController?.delegate = self
+            self?.updateMessageSearchResults()
         }
     }
 
@@ -420,7 +430,7 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
     }
 
     private func updateMessageSearchResults() {
-        guard let messageSearchController = messageSearchController else {
+        guard let messageSearchController, searchType == .messages else {
             return
         }
 
@@ -433,7 +443,7 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
                 return ChannelSelectionInfo(
                     channel: channel,
                     message: message,
-                    searchType: .channels
+                    searchType: .messages
                 )
             }
             DispatchQueue.main.async {
@@ -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
         }