Skip to content

Commit a8ba727

Browse files
Factory method for customizing the search results view (#428)
1 parent c6acfce commit a8ba727

File tree

6 files changed

+70
-3
lines changed

6 files changed

+70
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
33

44
# Upcoming
55

6-
### 🔄 Changed
6+
### ✅ Added
7+
- Factory method for customizing the search results view
78

89
# [4.47.1](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.47.1)
910
_January 29, 2024_

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListView.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ public struct ChatChannelListContentView<Factory: ViewFactory>: View {
208208
)
209209

210210
if viewModel.isSearching {
211-
SearchResultsView(
212-
factory: viewFactory,
211+
viewFactory.makeSearchResultsView(
213212
selectedChannel: $viewModel.selectedChannel,
214213
searchResults: viewModel.searchResults,
215214
loadingSearchResults: viewModel.loadingSearchResults,

Sources/StreamChatSwiftUI/DefaultViewFactory.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,29 @@ extension ViewFactory {
145145
EmptyView()
146146
}
147147

148+
public func makeSearchResultsView(
149+
selectedChannel: Binding<ChannelSelectionInfo?>,
150+
searchResults: [ChannelSelectionInfo],
151+
loadingSearchResults: Bool,
152+
onlineIndicatorShown: @escaping (ChatChannel) -> Bool,
153+
channelNaming: @escaping (ChatChannel) -> String,
154+
imageLoader: @escaping (ChatChannel) -> UIImage,
155+
onSearchResultTap: @escaping (ChannelSelectionInfo) -> Void,
156+
onItemAppear: @escaping (Int) -> Void
157+
) -> some View {
158+
SearchResultsView(
159+
factory: self,
160+
selectedChannel: selectedChannel,
161+
searchResults: searchResults,
162+
loadingSearchResults: loadingSearchResults,
163+
onlineIndicatorShown: onlineIndicatorShown,
164+
channelNaming: channelNaming,
165+
imageLoader: imageLoader,
166+
onSearchResultTap: onSearchResultTap,
167+
onItemAppear: onItemAppear
168+
)
169+
}
170+
148171
public func makeChannelListSearchResultItem(
149172
searchResult: ChannelSelectionInfo,
150173
onlineIndicatorShown: Bool,

Sources/StreamChatSwiftUI/ViewFactory.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,29 @@ public protocol ViewFactory: AnyObject {
148148
/// Creates the view always visible at the bottom of the channel list.
149149
/// - Returns: view shown at the bottom of the channel list.
150150
func makeChannelListStickyFooterView() -> ChannelListStickyFooterViewType
151+
152+
associatedtype ChannelListSearchResultsViewType: View
153+
/// Creates the view shown when the user is searching the channel list.
154+
/// - Parameters:
155+
/// - selectedChannel - binding of the selected channel.
156+
/// - searchResults - the search results matching the user query.
157+
/// - loadingSearchResults - whether search results are being loaded.
158+
/// - onlineIndicatorShown - whether the online indicator is shown.
159+
/// - channelNaming - closure for determining the channel name.
160+
/// - imageLoader - closure for loading images for channels.
161+
/// - onSearchResultTap - call when a search result is tapped.
162+
/// - onItemAppear - call when an item appears on the screen.
163+
/// - Returns: view shown in the channel list search results slot.
164+
func makeSearchResultsView(
165+
selectedChannel: Binding<ChannelSelectionInfo?>,
166+
searchResults: [ChannelSelectionInfo],
167+
loadingSearchResults: Bool,
168+
onlineIndicatorShown: @escaping (ChatChannel) -> Bool,
169+
channelNaming: @escaping (ChatChannel) -> String,
170+
imageLoader: @escaping (ChatChannel) -> UIImage,
171+
onSearchResultTap: @escaping (ChannelSelectionInfo) -> Void,
172+
onItemAppear: @escaping (Int) -> Void
173+
) -> ChannelListSearchResultsViewType
151174

152175
associatedtype ChannelListSearchResultItem: View
153176
/// Creates the search result item in the channel list.

StreamChatSwiftUITests/Infrastructure/TestTools/ChatClient_Mock.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public extension ChatClient {
1515
config.customCDNClient = customCDNClient
1616
config.isLocalStorageEnabled = isLocalStorageEnabled
1717
config.isClientInActiveMode = false
18+
config.maxAttachmentCountPerMessage = 10
1819

1920
return .init(
2021
config: config,

StreamChatSwiftUITests/Tests/Utils/ViewFactory_Tests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ class ViewFactory_Tests: StreamChatTestCase {
9999
// Then
100100
XCTAssert(view is MoreChannelActionsView)
101101
}
102+
103+
func test_viewFactory_makeSearchResultsView() {
104+
// Given
105+
let viewFactory = DefaultViewFactory.shared
106+
107+
// When
108+
let view = viewFactory.makeSearchResultsView(
109+
selectedChannel: .constant(nil),
110+
searchResults: [],
111+
loadingSearchResults: false,
112+
onlineIndicatorShown: { _ in true },
113+
channelNaming: { _ in "Test" },
114+
imageLoader: { _ in UIImage(systemName: "person")! },
115+
onSearchResultTap: { _ in },
116+
onItemAppear: { _ in }
117+
)
118+
119+
// Then
120+
XCTAssert(view is SearchResultsView<DefaultViewFactory>)
121+
}
102122

103123
func test_viewFactory_makeMessageAvatarView() {
104124
// Given

0 commit comments

Comments
 (0)