Skip to content

Commit 4592170

Browse files
Added possibility to inject custom footer view in the channel list (#35)
1 parent 138bb96 commit 4592170

File tree

6 files changed

+82
-0
lines changed

6 files changed

+82
-0
lines changed

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelList.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ struct ChannelsLazyVStack<Factory: ViewFactory>: View {
163163

164164
factory.makeChannelListDividerItem()
165165
}
166+
167+
factory.makeChannelListFooterView()
166168
}
167169
}
168170
}

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public struct ChatChannelListView<Factory: ViewFactory>: View {
9797
leadingSwipeButtonTapped: { _ in }
9898
)
9999
}
100+
101+
viewFactory.makeChannelListStickyFooterView()
100102
}
101103
}
102104
}

Sources/StreamChatSwiftUI/DefaultViewFactory.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ extension ViewFactory {
136136
SearchBar(text: searchText)
137137
}
138138

139+
public func makeChannelListFooterView() -> some View {
140+
EmptyView()
141+
}
142+
143+
public func makeChannelListStickyFooterView() -> some View {
144+
EmptyView()
145+
}
146+
139147
public func makeChannelListSearchResultItem(
140148
searchResult: ChannelSelectionInfo,
141149
onlineIndicatorShown: Bool,

Sources/StreamChatSwiftUI/ViewFactory.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ public protocol ViewFactory: AnyObject {
139139
searchText: Binding<String>
140140
) -> ChannelListTopViewType
141141

142+
associatedtype ChannelListFooterViewType: View
143+
/// Creates the view shown at the bottom of the channel list.
144+
/// - Returns: view shown at the bottom of the channel list.
145+
func makeChannelListFooterView() -> ChannelListFooterViewType
146+
147+
associatedtype ChannelListStickyFooterViewType: View
148+
/// Creates the view always visible at the bottom of the channel list.
149+
/// - Returns: view shown at the bottom of the channel list.
150+
func makeChannelListStickyFooterView() -> ChannelListStickyFooterViewType
151+
142152
associatedtype ChannelListSearchResultItem: View
143153
/// Creates the search result item in the channel list.
144154
/// - Parameters:

StreamChatSwiftUITests/Tests/Utils/ViewFactory_Tests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,28 @@ class ViewFactory_Tests: StreamChatTestCase {
556556
// Then
557557
XCTAssert(view is ReactionsUsersView)
558558
}
559+
560+
func test_viewFactory_makeChannelListFooterView() {
561+
// Given
562+
let viewFactory = DefaultViewFactory.shared
563+
564+
// When
565+
let view = viewFactory.makeChannelListFooterView()
566+
567+
// Then
568+
XCTAssert(view is EmptyView)
569+
}
570+
571+
func test_viewFactory_makeChannelListStickyFooterView() {
572+
// Given
573+
let viewFactory = DefaultViewFactory.shared
574+
575+
// When
576+
let view = viewFactory.makeChannelListStickyFooterView()
577+
578+
// Then
579+
XCTAssert(view is EmptyView)
580+
}
559581
}
560582

561583
extension ChannelAction: Equatable {

docusaurus/docs/iOS/swiftui/components/helper-views.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,44 @@ public func makeChannelListDividerItem() -> some View {
131131

132132
If you want your list to not have a divider whatsoever, you can simply return an `EmptyView` here.
133133

134+
## Changing the Top Bar
135+
136+
By default, the SwiftUI SDK shows a search bar at the top of the channel list. This component lets you search through messages matching the search term inside the channels. When you tap on a search result, the corresponding channel is opened, automatically scrolling to the searched message.
137+
138+
In order to replace this component with your own (or completely remove it by returning an `EmptyView`), you need to implement the `makeChannelListTopView` method:
139+
140+
```swift
141+
func makeChannelListTopView(
142+
searchText: Binding<String>
143+
) -> some View {
144+
SearchBar(text: searchText)
145+
}
146+
```
147+
148+
In this method, a binding of the search text is provided, in case you want to implement your custom search bar.
149+
150+
## Changing the Footer View
151+
152+
You can add a view at the bottom of the channel list. There are two options here - a footer shown when you scroll to the end of the channel list and a sticky footer that's always visible.
153+
154+
To add a footer at the bottom of the channel list, you need to implement the `makeChannelListFooterView` method:
155+
156+
```swift
157+
public func makeChannelListFooterView() -> some View {
158+
SomeFooterView()
159+
}
160+
```
161+
162+
To add a sticky footer, always visible at the bottom of the channel list, you need to implement the `makeChannelListStickyFooterView` method:
163+
164+
```swift
165+
func makeChannelListStickyFooterView() -> some View {
166+
SomeStickyFooterView()
167+
}
168+
```
169+
170+
Both methods return an `EmptyView` by default.
171+
134172
Remember to always inject your custom view factory in your view hierarchy:
135173

136174
```swift

0 commit comments

Comments
 (0)