Skip to content

Commit

Permalink
Factory method for customizing the search results view (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmitrevski authored Feb 7, 2024
1 parent c6acfce commit a8ba727
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming

### 🔄 Changed
### ✅ Added
- Factory method for customizing the search results view

# [4.47.1](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.47.1)
_January 29, 2024_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ public struct ChatChannelListContentView<Factory: ViewFactory>: View {
)

if viewModel.isSearching {
SearchResultsView(
factory: viewFactory,
viewFactory.makeSearchResultsView(
selectedChannel: $viewModel.selectedChannel,
searchResults: viewModel.searchResults,
loadingSearchResults: viewModel.loadingSearchResults,
Expand Down
23 changes: 23 additions & 0 deletions Sources/StreamChatSwiftUI/DefaultViewFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,29 @@ extension ViewFactory {
EmptyView()
}

public func makeSearchResultsView(
selectedChannel: Binding<ChannelSelectionInfo?>,
searchResults: [ChannelSelectionInfo],
loadingSearchResults: Bool,
onlineIndicatorShown: @escaping (ChatChannel) -> Bool,
channelNaming: @escaping (ChatChannel) -> String,
imageLoader: @escaping (ChatChannel) -> UIImage,
onSearchResultTap: @escaping (ChannelSelectionInfo) -> Void,
onItemAppear: @escaping (Int) -> Void
) -> some View {
SearchResultsView(
factory: self,
selectedChannel: selectedChannel,
searchResults: searchResults,
loadingSearchResults: loadingSearchResults,
onlineIndicatorShown: onlineIndicatorShown,
channelNaming: channelNaming,
imageLoader: imageLoader,
onSearchResultTap: onSearchResultTap,
onItemAppear: onItemAppear
)
}

public func makeChannelListSearchResultItem(
searchResult: ChannelSelectionInfo,
onlineIndicatorShown: Bool,
Expand Down
23 changes: 23 additions & 0 deletions Sources/StreamChatSwiftUI/ViewFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,29 @@ public protocol ViewFactory: AnyObject {
/// Creates the view always visible at the bottom of the channel list.
/// - Returns: view shown at the bottom of the channel list.
func makeChannelListStickyFooterView() -> ChannelListStickyFooterViewType

associatedtype ChannelListSearchResultsViewType: View
/// Creates the view shown when the user is searching the channel list.
/// - Parameters:
/// - selectedChannel - binding of the selected channel.
/// - searchResults - the search results matching the user query.
/// - loadingSearchResults - whether search results are being loaded.
/// - onlineIndicatorShown - whether the online indicator is shown.
/// - channelNaming - closure for determining the channel name.
/// - imageLoader - closure for loading images for channels.
/// - onSearchResultTap - call when a search result is tapped.
/// - onItemAppear - call when an item appears on the screen.
/// - Returns: view shown in the channel list search results slot.
func makeSearchResultsView(
selectedChannel: Binding<ChannelSelectionInfo?>,
searchResults: [ChannelSelectionInfo],
loadingSearchResults: Bool,
onlineIndicatorShown: @escaping (ChatChannel) -> Bool,
channelNaming: @escaping (ChatChannel) -> String,
imageLoader: @escaping (ChatChannel) -> UIImage,
onSearchResultTap: @escaping (ChannelSelectionInfo) -> Void,
onItemAppear: @escaping (Int) -> Void
) -> ChannelListSearchResultsViewType

associatedtype ChannelListSearchResultItem: View
/// Creates the search result item in the channel list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public extension ChatClient {
config.customCDNClient = customCDNClient
config.isLocalStorageEnabled = isLocalStorageEnabled
config.isClientInActiveMode = false
config.maxAttachmentCountPerMessage = 10

return .init(
config: config,
Expand Down
20 changes: 20 additions & 0 deletions StreamChatSwiftUITests/Tests/Utils/ViewFactory_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ class ViewFactory_Tests: StreamChatTestCase {
// Then
XCTAssert(view is MoreChannelActionsView)
}

func test_viewFactory_makeSearchResultsView() {
// Given
let viewFactory = DefaultViewFactory.shared

// When
let view = viewFactory.makeSearchResultsView(
selectedChannel: .constant(nil),
searchResults: [],
loadingSearchResults: false,
onlineIndicatorShown: { _ in true },
channelNaming: { _ in "Test" },
imageLoader: { _ in UIImage(systemName: "person")! },
onSearchResultTap: { _ in },
onItemAppear: { _ in }
)

// Then
XCTAssert(view is SearchResultsView<DefaultViewFactory>)
}

func test_viewFactory_makeMessageAvatarView() {
// Given
Expand Down

0 comments on commit a8ba727

Please sign in to comment.